How to get the current date and time

后端 未结 10 446
无人共我
无人共我 2020-11-29 16:59

How do I get the current date and time in Java?

I am looking for something that is equivalent to DateTime.Now from C#.

相关标签:
10条回答
  • 2020-11-29 17:55

    tl;dr

    Instant.now()
    

    java.time

    The java.util.Date class has been outmoded by the new java.time package (Tutorial) in Java 8 and later. The old java.util.Date/.Calendar classes are notoriously troublesome, confusing, and flawed. Avoid them.

    ZonedDateTime

    Get the current moment in java.time.

    ZonedDateTime now = ZonedDateTime.now();
    

    A ZonedDateTime encapsulates:

    • Date.
    • Time-of-day, with a fraction of a second to nanosecond resolution.
    • Time zone.

    If no time zone is specified, your JVM’s current default time zone is assigned silently. Better to specify your desired/expected time zone than rely implicitly on default.

    ZoneId z = ZoneId.of( "Pacific/Auckland" );
    ZonedDateTime zdt = ZonedDateTime.now( z );
    

    UTC

    Generally better to get in the habit of doing your back-end work (business logic, database, storage, data exchange) all in UTC time zone. The code above relies implicitly on the JVM’s current default time zone.

    The Instant class represents a moment in the timeline in UTC with a resolution of nanoseconds.

    Instant instant = Instant.now();
    

    The Instant class is a basic building-block class in java.time and may be used often in your code.

    When you need more flexibility in formatting, transform into an OffsetDateTime. Specify a ZoneOffset object. For UTC use the handy constant for UTC.

    OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );
    

    Time Zone

    You easily adjust to another time zone for presentation to the user. Use a proper time zone name, never the 3-4 letter codes such as EST or IST.

    ZoneId z = ZoneId.of( "America/Montreal" );
    ZonedDateTime nowMontreal = instant.atZone( z );
    

    Generate a String representation of that date-time value, localized.

    String output = DateTimeFormatter
        .ofLocalizedDate( FormatStyle.FULL )
        .withLocale( Locale.CANADA_FRENCH )
        .format ( nowMontreal );
    

    Instant

    Or, to stay in UTC, use Instant. An Instant object represents a moment on the timeline, to nanosecond resolution, always in UTC. This provides the building block for a zoned date-time, along with a time zone assignment. You can think of it conceptually this way:

    ZonedDateTime = Instant + ZoneId

    You can extract an Instant from a ZonedDateTime.

    Instant instantNow = zdt.toInstant();
    

    You can start with an Instant. No need to specify a time zone here, as Instant is always in UTC.

    Instant now = Instant.now();
    
    0 讨论(0)
  • 2020-11-29 17:57

    If you create a new Date object, by default it will be set to the current time:

    import java.util.Date;
    Date now = new Date();
    
    0 讨论(0)
  • 2020-11-29 17:58

    Java has always got inadequate support for the date and time use cases. For example, the existing classes (such as java.util.Date and SimpleDateFormatter) aren’t thread-safe which can lead to concurrency issues. Also there are certain flaws in API. For example, years in java.util.Date start at 1900, months start at 1, and days start at 0—not very intuitive. These issues led to popularity of third-party date and time libraries, such as Joda-Time. To address a new date and time API is designed for Java SE 8.

    LocalDateTime timePoint = LocalDateTime.now();
    System.out.println(timePoint);
    

    As per doc:

    The method now() returns the current date-time using the system clock and default time-zone, not null. It obtains the current date-time from the system clock in the default time-zone. This will query the system clock in the default time-zone to obtain the current date-time. Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.

    0 讨论(0)
  • 2020-11-29 17:59

    I prefer using the Calendar object.

    Calendar now = GregorianCalendar.getInstance()
    

    I find it much easier to work with. You can also get a Date object from the Calendar.

    http://java.sun.com/javase/6/docs/api/java/util/GregorianCalendar.html

    0 讨论(0)
提交回复
热议问题