java.time
The Answer by Jon Skeet is correct but now outdated.
As he said, the old legacy date-time classes are a mess and should be avoided. The Joda-Time project is now in maintenance mode, with the team advising migration to java.time.
LocalDateTime
The LocalDateTime
class represents a date and a time-of-day with a resolution of nanoseconds. This value is unattached to the timeline, without any concept of offset-from-UTC nor time zone.
As such it does not represent an actual moment but a potential moment. When you place it in the context of an offset or time zone, then you imbue the new value (a OffsetDateTime
or ZonedDateTime
) with meaning as it becomes a point on the timeline.
For example, 2016-12-25T00:00:00
is when Christmas begins this year. But that stroke of midnight happens first in the Pacific such as Auckland New Zealand, which is earlier than Christmas in Kolkata India. And Christmas begins even later in Paris France, still later in Montréal Québec. That is why Santa starts in the east and works westward with his deliveries.
LocalDateTime xmas2016 = LocalDateTime.of( 2016 , Month.DECEMBER , 25 , 0 , 0 ) ;
Zoned
While your Question is not clear, I suspect what you really need is date-time values in UTC. All your various inputs in various time zones should be normalized into UTC.
Programmers should think of UTC as the One True Time. Most of your business logic, logging, data storage, and data exchange should generally be done in UTC. I suggest you keep a second clock on your desk set to UTC.
The Instant
class represents a moment on the time line in UTC with a resolution of nanoseconds.
Instant instant = Instant.now();
When you need to see the wall-clock time for some offset-from-UTC, apply a ZoneOffset
to get a OffsetDateTime
object. To see the wall-clock time for some time zone, apply a ZoneId
to get a ZonedDateTime
.
ZonedDateTime xmas2016Montréal = xmas2016.atZone( ZoneId.of( "America/Montreal" ) );
Both of these offset/zoned date-time objects can get you back to an Instant
with a call to toInstant
.
Instant instant = xmas2016Montréal.toInstant(); // Convert to UTC.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
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.