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

12
  • In which timezone? Commented Mar 5, 2018 at 15:06
  • Possible duplicate of How to get time and Date from datetime stamp Commented Mar 5, 2018 at 15:07
  • 4
    You can get rid of the date part by taking the modulo with the number of millis seconds in a one day, System.currentTimeMillisd() % 86400000L Commented Mar 5, 2018 at 15:17
  • 1
    @DavidZimmerman Much easier to use the java.time class, and more obvious with self-documenting code. Commented Mar 5, 2018 at 17:36
  • 4
    Dear down-voters, please leave a criticism along with your vote. If you think this is a duplicate, find the original. I tried but could not find any other. So I am up-voting here. Commented Mar 5, 2018 at 17:39

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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); 

5 Comments

Not a complete Answer without explaining time zone.
Does not account for anomalies such as Daylight Saving Time (DST).
@BasilBourque Timezone and DST are irrelevant. Local time is local time regardless of its offset from GMT.
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.
@BasilBourque Right you are. Updated answer.
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); 

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.