Reputation: 85
I want to construct date without time zone in Java. I have tried this to ignore time zone:
Calendar time = Calendar.getInstance();
time.clear(Calendar.ZONE_OFFSET);
System.out.println("time = " + time.getTime());
But this doesn't work for me. It returns "2018-07-11 03:00:00" instead of "2018-07-10 15:00:00".
Please any idea.
Upvotes: 1
Views: 1149
Reputation: 88
As cricket_007 mentioned you want to look at the LocalDate class. This is a date without a timezone.
It has methods for getting the current time and converting it to a zoned date or date-time. This is part of the 'new' time API in Java which has the design philosophy of asking you to think about what it is that you mean with your data, because time and date is incredibly hard to get right.
Are we talking about duration? Use a Duration.
Do you want to display time to a user use a ZonedDateTime, but don't expect the values there to be sequential because of daylight time shifts and the like.
Are we talking about linear time where there is only one value for the given time? You want an Instant.
The idea was to design the API so that you cannot use the different data-types without thinking about these things. If you want to read more you can start with Joda Time which is the precursor to today's java.time
Upvotes: 2
Reputation: 338346
LocalDateTime.now(
ZoneId.of( "America/Montreal" )
)
.toString()
2018-01-23T12:34:56.123456
If you want to drop the minutes and seconds, truncate.
LocalDateTime.now(
ZoneId.of( "America/Montreal" )
)
.truncatedTo( ChronoUnit.HOURS )
.toString()
2018-01-23T12:00:00
The Calendar
class is now legacy, supplanted by the modern java.time classes. Also, Calendar
always carries a time zone, so cannot meet your needs.
In java.time, the three classes named with “Local…” purposely lack any concept of time zone or offset-from-UTC. The word local means any locality or every locality, not a particular locality.
LocalDate
A date-only value, lacking time-of-day and lacking time zone.
LocalTime
A time-of-day value, lacking a date and lacking a time zone. Limited to a single 24-hour period.
LocalDateTime
A date with time-of-day, lacking time zone.
Does not represent a moment, is not a point on the timeline. Represents potential moments along a range of about 26-27 hours (the various offsets used by various time zones). A LocalDateTime
has no real meaning until you apply a time zone or offset-from-UTC to determine a specific moment.
Capture the current moment in UTC by using the Instant
class. This class uses a resolution of nanoseconds, but the current moment may not be so fine, depending on the limitations of your JVM implementation, host OS, and host computer hardware clock.
Instant instant = Instant.now() ; // Capture current moment in UTC.
If you want to view this current moment thorough the lens of the wall-click time used by the people of a particular region, apply a time zone. Apply a ZoneId
to get a ZonedDateTime
.
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
To use just the date and time-of-day from that ZonedDateTime
, but discard the time zone, produce a LocalDateTime
object.
LocalDateTime ldt = zdt.toLocalDateTime() ; // Extract the date and time-of-day but omit the time zone.
As a shortcut you can skip the zoned objects, the Instant
and the ZonedDateTime
. You still need a time zone, a ZoneId
, as for any given the moment the date and time-of-day both vary around the globe by zone. The ZoneId
is used in determining the current moment as seen in the wall-clock time used by the people of that region. Once captured, the ZoneId
is then forgotten by the resulting LocalDateTime
object. If you wanted to keep that zone, remember that zone, you’d be using ZonedDateTime
instead of LocalDateTime
.
LocalDateTime ldt = LocalDateTime.now( z ) ;
If you want the JVM’s current default time zone at runtime, pass a ZoneId
returned by a call to ZoneId.systemDefault
. Beware that the default can be changed at any moment by any code in any thread of any app within that JVM.
If you want UTC rather than some time zone, pass the constant from the ZoneOffset
subclass of ZoneId
, ZoneOffset.UTC
.
LocalDateTime.now( ZoneOffset.UTC ) // Capture the current date and time of day as seen in UTC (an offset of zero), and then discard that offset/zone info.
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.
Upvotes: 2