I looked at Joda-Time source, and the fromDateFields
method uses the getXXX
methods from java.util.Date
to get the values for the date/time fields. Something like that (I'm using Joda-Time 2.9.9):
return new LocalDateTime(
date.getYear() + 1900,
date.getMonth() + 1,
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds(),
(((int) (date.getTime() % 1000)) + 1000) % 1000
);
If you look at the javadoc, you'll see that all these methods return the correspondent value interpreted in the local time zone. Which means that it's implicity using the JVM default timezone.
Actually, looking at Date
source code, all the getters call normalize()
, which is a method that uses the default timezone to convert the fields.
You said that Personally I don't like the zone part, but in Joda-Time, it's implicity/indirectly using the default timezone.
And it makes sense to use a zone, because a java.util.Date
represents a point in the timeline (a specific instant), without any timezone information. The same instant can correspond to a different date and time in different parts of the world, so you need a timezone to do this conversion.
Joda-Time uses the Date::getXXX
methods, which implicity uses the default timezone. But in java.time
, things must be more explicit, so you must use a ZoneId
object.
By the way, your solution (LocalDateTime.from(date.toInstant())
) didn't work for me. In Java 8, it gives an error:
java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: 2018-02-01T10:50:16.882Z of type java.time.Instant
So you'll need to convert the Date
to Instant
, then tell in what timezone you want that, and then get the local part, just as you're already doing:
LocalDateTime dt = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
Or:
// the result is the same
LocalDateTime dt = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
Although java.time
forces you to use a timezone, I believe it's a good thing, because it makes you think about it, instead of doing some "magic" behind the scenes.
And you can even change it to another timezones if needed (such as ZoneId.of("America/New_York")
). Even if you end up using the JVM's default, it was a conscient choice - ideally, because some people just use the default without thinking about it.