How to get Date in java.util.Date instead of Unix Timestamp in bash script?

212 views Asked by At

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.

2

There are 2 answers

1
ikegami On BEST ANSWER

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

1
Diego Torres Milano On

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.