How can I get the count of milliseconds since midnight for the current?

后端 未结 11 683
生来不讨喜
生来不讨喜 2021-01-30 03:45

Note, I do NOT want millis from epoch. I want the number of milliseconds currently on the clock.

So for example, I have this bit of code.

Date date2 = ne         


        
相关标签:
11条回答
  • 2021-01-30 04:03

    Use Calendar

    Calendar.getInstance().get(Calendar.MILLISECOND);
    

    or

    Calendar c=Calendar.getInstance();
    c.setTime(new Date()); /* whatever*/
    //c.setTimeZone(...); if necessary
    c.get(Calendar.MILLISECOND);
    

    In practise though I think it will nearly always equal System.currentTimeMillis()%1000; unless someone has leap-milliseconds or some calendar is defined with an epoch not on a second-boundary.

    0 讨论(0)
  • 2021-01-30 04:04

    Do you mean?

    long millis = System.currentTimeMillis() % 1000;
    

    BTW Windows doesn't allow timetravel to 1969

    C:\> date
    Enter the new date: (dd-mm-yy) 2/8/1969
    The system cannot accept the date entered.
    
    0 讨论(0)
  • 2021-01-30 04:10

    In Java 8 you can simply do

    ZonedDateTime.now().toInstant().toEpochMilli()
    

    returns : the number of milliseconds since the epoch of 1970-01-01T00:00:00Z

    0 讨论(0)
  • 2021-01-30 04:11

    Joda-Time

    I think you can use Joda-Time to do this. Take a look at the DateTime class and its getMillisOfSecond method. Something like

    int ms = new DateTime().getMillisOfSecond() ;
    
    0 讨论(0)
  • 2021-01-30 04:14

    tl;dr

    You ask for the fraction of a second of the current time as a number of milliseconds (not count from epoch).

    Instant.now()                               // Get current moment in UTC, then…
           .get( ChronoField.MILLI_OF_SECOND )  // interrogate a `TemporalField`.
    

    2017-04-25T03:01:14.113Z → 113

    1. Get the fractional second in nanoseconds (billions).
    2. Divide by a thousand to truncate to milliseconds (thousands).

    See this code run live at IdeOne.com.

    Using java.time

    The modern way is with the java.time classes.

    Capture the current moment in UTC.

    Instant.now()
    

    Use the Instant.get method to interrogate for the value of a TemporalField. In our case, the TemporalField we want is ChronoField.MILLI_OF_SECOND.

    int millis = Instant.now().get( ChronoField.MILLI_OF_SECOND ) ;  // Get current moment in UTC, then interrogate a `TemporalField`.
    

    Or do the math yourself.

    More likely you are asking this for a specific time zone. The fraction of a second is likely to be the same as with Instant but there are so many anomalies with time zones, I hesitate to make that assumption.

    ZonedDateTime zdt = ZonedDateTime.now( ZoneId.of( "America/Montreal" ) ) ;
    

    Interrogate for the fractional second. The Question asked for milliseconds, but java.time classes use a finer resolution of nanoseconds. That means the number of nanoseconds will range from from 0 to 999,999,999.

    long nanosFractionOfSecond = zdt.getNano();
    

    If you truly want milliseconds, truncate the finer data by dividing by one million. For example, a half second is 500,000,000 nanoseconds and also is 500 milliseconds.

    long millis = ( nanosFractionOfSecond / 1_000_000L ) ;  // Truncate nanoseconds to milliseconds, by a factor of one million.
    

    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.

    Where to obtain the java.time classes?

    • Java SE 8 and SE 9 and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

    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.

    0 讨论(0)
  • 2021-01-30 04:17

    Java 8:

    LocalDateTime toDate = LocalDateTime.now();
    LocalDateTime fromDate = LocalDateTime.of(toDate.getYear(), toDate.getMonth(), 
    toDate.getDayOfMonth(), 0, 0, 0);
    long millis = ChronoUnit.MILLIS.between(fromDate, toDate);
    
    0 讨论(0)
提交回复
热议问题