How to get the number of milliseconds elapsed so far today

后端 未结 4 733
攒了一身酷
攒了一身酷 2021-02-13 07:02

I want to get the current time and date in milliseconds. How can I get this?

I tried this:

Date date=new Date() ;  
System.out.println(\"Today is \" +dat         


        
4条回答
  •  囚心锁ツ
    2021-02-13 07:45

    java.time

    The modern approach uses java.time classes that supplant the troublesome old legacy date-time classes.

    Getting the span of time from the beginning of today to the current moment is more complicated that you might expect.

    Current moment

    First, determining “today” requires a time zone. A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

    Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter pseudo-zones such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

    ZoneId z = ZoneId.of( "America/Montreal" ) ;
    

    If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit, as the default may be changed at any moment during runtime by any code in any thread of any app within the JVM.

    ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.
    

    The ZonedDateTime class represents a moment (a date & time-of-day) as seen by the people of a particular region (a time zone, a ZoneId).

    ZonedDateTime zdt = ZonedDateTime.now( z ) ;
    

    Start of day

    Next we need to determine the first moment of the day. Do not assume the day starts at 00:00:00. Because of anomalies such as Daylight Saving Time (DST), the day may start at another time such as 01:00:00. Let java.time determine the first moment of the day in a particular zone on a particular date.

    First extract the date-only portion of our ZonedDateTime object above.

    LocalDate ld = zdt.toLocalDate() ;
    

    Ask for first moment of the day in our desired zone.

    ZonedDateTime zdtStartOfDay = ld.atStartOfDay( z ) ;
    

    Elapsed time

    Now we have the pair of pieces we need: first moment of the day, and the current moment. We ask the ChronoUnit enum object MILLISECONDS to calculate the time elapsed between that pair. Note that this may involve data loss, as the ZonedDateTime objects may hold microseconds or nanoseconds being ignored in this calculation of milliseconds.

    long millisElapsedToday = ChronoUnit.MILLISECONDS.between( zdtStartOfDay , zdt ) ;
    

    You may also be interested in representing that span-of-time as unattached to the timeline in a Duration object.

    Duration d = Duration.between( zdtStartOfDay , zdt ) ;
    

    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.

    With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings or java.sql* classes.

    Where to obtain the java.time classes?

    • Java SE 8, Java 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 Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). 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.

提交回复
热议问题