How to convert Joda-Time DateTime to java.util.Date and vice versa?

前端 未结 3 1604
粉色の甜心
粉色の甜心 2020-12-07 19:59

Is it possible to do that? If yes, then how do I do the conversion from Joda-Time to Date and vice versa?

3条回答
  •  悲哀的现实
    2020-12-07 20:30

    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

提交回复
热议问题