Home > Back-end >  How to get Date in java.util.Date instead of Unix Timestamp in bash script?
How to get Date in java.util.Date instead of Unix Timestamp in bash script?

Time:10-27

I have an .sh script on my Linux server.

I need the date in milliseconds in Java, but everything I find on the net is giving me Unix Timestamp.

Like this: date=$(date -d 'today 00:00:00' " %s")

I need java milliseconds, like here: https://www.fileformat.info/tip/java/date2millis.htm.

How do I get that easily without writing a long java code? There has to be an easy solution.

CodePudding user response:

It's just unix epoch in milliseconds instead of seconds, so simply replace %s with %s%3N.

epoch_milli=$( date -d 'today 00:00:00'  %s%3N )
$ date  %s; date  %s%3N
1666798614
1666798614357

Conversion using your site

CodePudding user response:

Assuming your date supports this format

%N     nanoseconds (000000000..999999999)

so you can do something like

date  %s.%N

to get nanoseconds, then you can do the math if you want millies.

  • Related