Is it possible to do that? If yes, then how do I do the conversion from Joda-Time to Date and vice versa?
3 Answers
To convert Java Date
to Joda DateTime
:-
Date date = new Date();
DateTime dateTime = new DateTime(date);
And vice versa:-
Date dateNew = dateTime.toDate();
With TimeZone
, if required:-
DateTime dateTimeNew = new DateTime(date.getTime(), timeZone);
Date dateTimeZone = dateTime.toDateTimeAtStartOfDay(timeZone).toDate();

- 44,383
- 11
- 84
- 103
-
9Your conversion to `DateTime` uses the system default time zone - you should say that explicitly. And your conversion which *does* use a time zone takes the start of the day, which is an odd choice IMO. A `DateTime` already *knows* the time zone, so it's odd to want to change it - it makes much more sense to specify the zone when converting *from* `Date` *to* `DateTime`. – Jon Skeet Mar 11 '13 at 07:39
-
1`java.utiL.Date` does not actually know about a `java.util.TimeZone`. So you'll have to get the `TimeZone` in a separate variable if you need it too, like this : `TimeZone zone = dateTime.getZone().toTimeZone();` – bowmore Mar 11 '13 at 08:09
You haven't specified which type within Joda Time you're interested in, but:
Instant instant = ...;
Date date = instant.toDate();
instant = new Instant(date);
// Or...
instant = new Instant(date.getTime());
Neither Date
nor Instant
are related to time zones, so there's no need to specify one here.
It doesn't make sense to convert from LocalDateTime
/ LocalDate
/ LocalTime
to Date
(or vice versa) as that would depend on the time zone being applied.
With DateTime
you can convert to a Date
without specifying the time zone, but to convert from Date
to DateTime
you should specify the time zone, or it will use the system default time zone. (If you really want that, I'd specify it explicitly to make it clear that it's a deliberate choice.)
For example:
DateTimeZone zone = DateTimeZone.forID("Europe/London");
Date date = ...;
DateTime dateTime = new DateTime(date.getTime(), zone);

- 1,421,763
- 867
- 9,128
- 9,194
-
`LocalTime` really helps with simplifying code when interacting with `java.util.Date` and when doing tests. Thanks. – Alexandre Martins Jul 07 '16 at 17:22
To Convert from Java Date to Joda Time of Date:
To convert from Date to DateTime time zone needed to be specified.
To convert from java.util Date to Joda Time of Date you just need to pass the java.util Date and time zone to the constructor of Joda Time of Date.
java.util.Date date = new java.util.Date(System.currentTimeMillis());
DateTimeZone dtz = DateTimeZone.getDefault();// Gets the default time zone.
DateTime dateTime = new DateTime(date.getTime(), dtz);
To Convert from Joda Time of Date to Java Date:
For the reverse case Joda DateTime has a method toDate()
which will return the java.util Date.
DateTime jodaDate = new DateTime();
java.util.Date date = jodaDate.toDate();
For More Details Visit Here

- 12,348
- 19
- 73
- 82