6

I want to get the current time in milliseconds. I'm using System.currentTimeMillis() but this returns the date as well as the time. I simply want "15:03" in milliseconds, not the date too.

Note that I want an integer and not a formatted string. If it was 08:30, this is the equivalent to 30600 seconds, which is in turn equivalent to 30600000 milliseconds. This is the value I want

Yash Morar
  • 159
  • 2
  • 14

3 Answers3

3

Use the LocalTime class:

long millis = LocalTime.now().toNanoOfDay() / 1_000_000;

Basil Bourque correctly points out that it isn’t always this simple: a Daylight Saving Time change (such as will occur in most of the US this Sunday) can mean that, for example, there may not be eight hours between midnight and 8 AM.

You can account for this by using a ZonedDateTime, which accounts for all calendar information, including DST changeovers:

ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime start = now.truncatedTo(ChronoUnit.DAYS);
long millis = ChronoUnit.MILLIS.between(start, now);
VGR
  • 40,506
  • 4
  • 48
  • 63
  • Not a complete Answer without explaining time zone. – Basil Bourque Mar 05 '18 at 17:33
  • Does not account for anomalies such as Daylight Saving Time (DST). – Basil Bourque Mar 05 '18 at 17:40
  • @BasilBourque Timezone and DST are irrelevant. Local time is local time regardless of its offset from GMT. – VGR Mar 05 '18 at 17:43
  • 2
    Nope. Very much relevant. For example, in some places Daylight Saving Time (DST) cutover takes place at first moment of the day. So at the stroke of midnight, the clock jumps to 1 AM. The hour between 00:00 and 01:00 never existed. Your code would incorrectly report *two* hours of milliseconds having elapsed for a `LocalTime` of 2 AM when in fact only *one* hour of milliseconds elapsed. – Basil Bourque Mar 05 '18 at 18:00
  • 1
    @BasilBourque Right you are. Updated answer. – VGR Mar 05 '18 at 18:20
3

tl;dr

Duration.between( todayStart , now ).toMillis()

Details

Get the current moment in the wall-clock time used by the people of a certain region (a time zone).

ZoneId z = ZoneId.of( “Africa/Tunis” ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;

Get the first moment of the day. Do not assume this is 00:00:00. Let java.time determine.

ZonedDateTime todayStart = now.toLocalDate().atStartOfDay( z ) ;

Represent the delta between them, the span of time unattached to the timeline, as a Duration.

Duration d = Duration.between( todayStart , now ) ;

A Duration has a resolution of nanoseconds. That is finer than the milliseconds you desire. A convenience method will ignore any microseconds or nanoseconds for you.

long millisSinceStartOfToday = d.toMillis() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Since you are interested in the millis since midnight in the GMT timezone, the easiest approach is probably:

int millis = LocalTime.now(ZoneOffset.UTC).get(ChronoField.MILLI_OF_DAY);
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    I like the `.get(ChronoField.MILLI_OF_DAY)` very much, it’s so clear to read the intention. Other date-time classes can be used instead of `OffsetTime` including `LocalTime`. – Ole V.V. Mar 06 '18 at 07:54