Rectangle 27 0

Equivalent of C 's DateTime.Now in Java?


Calendar now = GregorianCalendar.getInstance()
Calendar.getInstance()

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

Note
Rectangle 27 0

Equivalent of C 's DateTime.Now in Java?


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

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) arent 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 0not 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.

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.

Note
Rectangle 27 0

Equivalent of C 's DateTime.Now in Java?


import java.util.Date;

Date d = new Date();

initializes it so that it represents the time at which it was allocated

Also note that GregorianCalendar and many similar objects work the same way. So whatever type of date/calendar object you are working with, the zero-argument constructor usually initializes the object to the current date/time.

From Date doc: As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings.

Just construct a new Date object without any arguments; this will assign the current date and time to the new object.

Make sure you're using java.util.Date and not java.sql.Date -- the latter doesn't have a zero-arg constructor, and has somewhat different semantics that are the topic of an entirely different conversation. :)

Note
Rectangle 27 0

Equivalent of C 's DateTime.Now in Java?


Instant
Instant instant = Instant.now();
Instant instantNow = zdt.toInstant();
Instant now = Instant.now();
Instant.now()
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );
String output = DateTimeFormatter
    .ofLocalizedDate( FormatStyle.FULL )
    .withLocale( Locale.CANADA_FRENCH )
    .format ( nowMontreal );
ZoneId
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime nowMontreal = instant.atZone( z );
ZoneId z = ZoneId.of( "Pacific/Auckland" );
ZonedDateTime zdt = ZonedDateTime.now( z );
ZonedDateTime
ZonedDateTime now = ZonedDateTime.now();
  • Time-of-day, with a fraction of a second to nanosecond resolution.

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 JVMs current default time zone.

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

Get the current moment in java.time.

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

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:

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

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

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.

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

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

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.

Note
Rectangle 27 0

Equivalent of C 's DateTime.Now in Java?


Instant
Instant instant = Instant.now();
Instant instantNow = zdt.toInstant();
Instant now = Instant.now();
Instant.now()
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );
String output = DateTimeFormatter
    .ofLocalizedDate( FormatStyle.FULL )
    .withLocale( Locale.CANADA_FRENCH )
    .format ( nowMontreal );
ZoneId
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime nowMontreal = instant.atZone( z );
ZoneId z = ZoneId.of( "Pacific/Auckland" );
ZonedDateTime zdt = ZonedDateTime.now( z );
ZonedDateTime
ZonedDateTime now = ZonedDateTime.now();
  • Time-of-day, with a fraction of a second to nanosecond resolution.

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 JVMs current default time zone.

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

Get the current moment in java.time.

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

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:

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

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

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.

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

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

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.

Note
Rectangle 27 0

Equivalent of C 's DateTime.Now in Java?


import java.util.Date;

Date d = new Date();

initializes it so that it represents the time at which it was allocated

Also note that GregorianCalendar and many similar objects work the same way. So whatever type of date/calendar object you are working with, the zero-argument constructor usually initializes the object to the current date/time.

From Date doc: As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings.

Just construct a new Date object without any arguments; this will assign the current date and time to the new object.

Make sure you're using java.util.Date and not java.sql.Date -- the latter doesn't have a zero-arg constructor, and has somewhat different semantics that are the topic of an entirely different conversation. :)

Note
Rectangle 27 0

Equivalent of C 's DateTime.Now in Java?


import java.util.Date;   
Date now = new Date();

Note that the Date object is mutable and if you want to do anything sophisticated, use jodatime.

Note
Rectangle 27 0

Equivalent of C 's DateTime.Now in Java?


Calendar now = GregorianCalendar.getInstance()
Calendar.getInstance()

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

Note
Rectangle 27 0

Equivalent of C 's DateTime.Now in Java?


DateTime dt = DateTime.now();
DateTime dt = new DateTime();

As noted in the comments, the latest versions of Joda Time have a DateTime.now() method, so:

Note that this answer was posted in 2010. Java 8 has absorbed many of Joda Time's best features. See the official documentation and many other online articles for more information.

The Java Date and Calendar classes are considered by many to be poorly designed. You should take a look at Joda Time, a library commonly used in lieu of Java's built-in date libraries.

The current(2.1) version of Joda seems to have a static DateTime.now method() (optionally accepts DateTimeZone or Chronology). joda-time.sourceforge.net/apidocs/org/joda/time/

The equivalent of DateTime.Now in Joda Time is:

Note
Rectangle 27 0

Equivalent of C 's DateTime.Now in Java?


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

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) arent 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 0not 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.

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.

Note