How to convert Joda-Time to java 8 date with many formats?

As we all know, Joda-Time is the most widely used date and time processing library before the release of Java 8. To address the shortcomings of the older java.util.Date and java.util.Calendar Java 8 introduced new APIs for Date and Time.

As we all know, Joda-Time is the most widely used date and time processing library before the release of Java 8. To address the shortcomings of the older java.util.Date and java.util.Calendar Java 8 introduced new APIs for Date and Time.

So, it’s about time our projects make a switch to using Java 8 new APIs. The process should be easy but not always. So, having Utils class that can do the work for you will be helpful and your team can do the migration based on its priorities.

You should be able to use this Utility (JodaToJavaDateUtils) class code below to leverage your needs based on your projects. Also make sure to check out test (JodaToJavaDateUtilsTest) class as well.

Utility Class below.

/**

@author Bishow Pandey

This Util class can be used to convert different formats of deprecated Joda Date API to Java Date API and viceversa.

NOTE: We are using Namespace Management mechanism by using the fully-qualified class name -

"package name plus class name" so that they can be fully distinguished below. */

public class JodaToJavaDateUtils {

private static final int MILLI_TO_NANO_SECONDS = 1000000;

/**

 * Making this constructor private to make sure only this class is able to construct this class object.

 */

private JodaToJavaDateUtils() {

}

/**

 * Converts an org.joda.time.DateMidnight to java.time.LocalDateTime

 *

 * @param dateMidnight The org.joda.time.DateMidnight to convert

 * @return See above

 */

public static java.time.LocalDateTime convertJodaDateMidnightToJavaLocalDateTime(

        org.joda.time.DateMidnight dateMidnight) {

    return java.time.LocalDateTime.of(dateMidnight.getYear(), dateMidnight.getMonthOfYear(),

            dateMidnight.getDayOfMonth(), dateMidnight.getHourOfDay(),

            dateMidnight.getMinuteOfHour(), dateMidnight.getSecondOfMinute(),

            dateMidnight.getMillisOfSecond()

                    * MILLI_TO_NANO_SECONDS);

}

/**

 * Converts an org.joda.time.DateMidnight to java.time.LocalDate

 *

 * @param dateMidnight The org.joda.time.DateMidnight tp convert

 * @return See above

 */

public static java.time.LocalDate convertJodaDateMidnightToJavaLocalDate(org.joda.time.DateMidnight dateMidnight) {

    return java.time.LocalDate.of(dateMidnight.getYear(), dateMidnight.getMonthOfYear(),

            dateMidnight.getDayOfMonth());

}

/**

 * Converts an org.joda.time.LocalDateTime to java.time.LocalDateTime

 *

 * @param localDateTime The org.joda.time.LocalDateTime to convert

 * @return See above

 */

public static java.time.LocalDateTime convertJodaLocalDateTimeToJavaLocalDateTime(

        org.joda.time.LocalDateTime localDateTime) {

    return java.time.LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonthOfYear(),

            localDateTime.getDayOfMonth(), localDateTime.getHourOfDay(),

            localDateTime.getMinuteOfHour(), localDateTime.getSecondOfMinute(),

            localDateTime.getMillisOfSecond()

                    * MILLI_TO_NANO_SECONDS);

}

/**

 * Converts an org.joda.time.LocalDate to java.time.LocalDate

 *

 * @param localDate The org.joda.time.LocalDate to convert

 * @return See above

 */

public static java.time.LocalDate convertJodaLocalDateToJavaLocalDate(org.joda.time.LocalDate localDate) {

    return java.time.LocalDate.of(localDate.getYear(), localDate.getMonthOfYear(),

            localDate.getDayOfMonth());

}

/**

 * Converts a string date format to an org.joda.time.DateMidnight

 *

 * @param date       To convert String format to org.joda.time.DateMidnight

 * @param dateFormat String date format

 * @return See above

 */

public static org.joda.time.DateMidnight convertStringDateFormatToJodaDateMidnight(String date, String dateFormat) {

    return org.joda.time.DateTime.parse(date, dateTimeFormatter(dateFormat)).toDateMidnight();

}

/**

 * Converts a string date format to an org.joda.time.DateTime

 *

 * @param date       To convert String format to org.joda.time.DateTime

 * @param dateFormat String date format

 * @return See above

 */

public static org.joda.time.DateTime convertStringDateFormatToJodaDateTime(String date, String dateFormat) {

    return org.joda.time.DateTime.parse(date, dateTimeFormatter(dateFormat));

}

/**

 * Converts a string date format to an org.joda.time.LocalDate

 *

 * @param date       To convert String date format to org.joda.time.LocalDate

 * @param dateFormat String date format

 * @return See above

 */

public static org.joda.time.LocalDate convertStringDateFormatToJodaLocalDate(String date, String dateFormat) {

    return org.joda.time.LocalDate.parse(date, dateTimeFormatter(dateFormat));

}

/**

 * Converts a string date format to java.time.LocalDateTime

 *

 * @param date       To convert String format to java.time.LocalDate

 * @param dateFormat String date format

 * @return See above

 */

public static java.time.LocalDateTime convertStringDateFormatToJavaLocalDateTime(String date, String dateFormat) {

    return convertJodaDateMidnightToJavaLocalDateTime(convertStringDateFormatToJodaDateMidnight(date, dateFormat));

}

/**

 * Converts a string date format to java.time.LocalDate

 *

 * @param date       To convert String format to java.time.LocalDate

 * @param dateFormat String date format

 * @return See above

 */

public static java.time.LocalDate convertStringDateFormatToJavaLocalDate(String date, String dateFormat) {

    return convertJodaDateMidnightToJavaLocalDate(convertStringDateFormatToJodaDateMidnight(date, dateFormat));

}

/**

 * Converts a java.time.LocalDateTime to an org.joda.time.LocalDateTime

 *

 * @param localDateTime The java.time.LocalDateTime to convert

 * @return See above

 */

public static org.joda.time.LocalDateTime convertJavaLocalDateTimeToJodaLocalDateTime(

        java.time.LocalDateTime localDateTime) {

    return new org.joda.time.LocalDateTime(localDateTime.getYear(),

            localDateTime.getMonthValue(), localDateTime.getDayOfMonth(), localDateTime.getHour(),

            localDateTime.getMinute(), localDateTime.getSecond(), localDateTime.getNano()

            / MILLI_TO_NANO_SECONDS);

}

/**

 * Private DateFormatter helper method

 *

 * @param dateFormat String date format

 */

private static org.joda.time.format.DateTimeFormatter dateTimeFormatter(String dateFormat) {

    return org.joda.time.format.DateTimeFormat

            .forPattern(dateFormat);

}
}

Test Class Below.

import org.apache.commons.lang.StringUtils;

import org.junit.Assert;

import org.junit.Test;

/**

@author Bishow Pandey

NOTE: You will find some methods not following the correct camelCase naming convention.

I am doing this here, as I do not know the expertise of readers and want

them to understand the differt formats easily.

Example : convertStringDateFormatYYYY_MM_DD_HH_MM_NO_DASHES_FORMATToJodaDateTimeTest and likewise

FYI : Also just Asserting test cases are not enough sometimes. (I got lazy here :-) ) */

public class JodaToJavaDateUtilsTes


//ISO 8601 no T

private static final String YY_MM_DD_NO_DASHES_FORMAT = "yyMMdd";

private static final String YYYY_MM_DD_NO_DASHES_FORMAT = "yyyyMMdd";

private static final String YYYY_MM_DD_HH_MM_NO_DASHES_FORMAT = "yyyyMMddHHmm";

private static final String YYYY_MM_DD_HH_MM_SS_NO_DASHES_FORMAT = "yyMMddHHmmss";

private static final String YYYY_MM_DD_HH_MM_NO_DASHES_WITH_COLON_FORMAT = "yyyyMMdd:HHmm";

private static final String YY_MM_DD_HH_MM_NO_DASHES_WITH_COLON_FORMAT = "yyMMdd:HHmm";

private static final String YYYY_MM_DD_WITH_DASHES_FORMAT = "yyyy-MM-dd";

//ISO 8601 :

private static final String YYYY_MM_DD_T_HH_MM_WITH_DASHES_FORMAT = "yyyy-MM-dd'T'HH:mm";

private static final String YYYY_MM_DD_T_HH_MM_SS_WITH_DASHES_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

private static final String YYYY_MM_DD_T_HH_MM_SS_SS_WITH_DASHES_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SS";

private static final String YYYY_MM_DD_T_HH_MM_SS_SSS_WITH_DASHES_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";

private static final String YYYY_MM_DD_T_HH_MM_SS_SSS_Z_WITH_DASHES_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

private static final String YYYY_MM_DD_T_HH_MM_SS_ZZ_WITH_DASHES_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZZ";

private static final String YYYY_MM_DD_T_HH_MM_SS_ZZZ_WITH_DASHES_FORMAT = "yyyy-MM-dd'T'HH:mm:ss ZZZ";

private static final String YYYY_MM_DD_T_HH_MM_SS_SSS_WITH_DASHES_FORMAT_WITH_UTC_4_HOURS = "yyyy-MM-dd'T'HH:mm:ss.SSS-04:00";

private static final String YYYY_MM_DD_T_HH_MM_SS_SSS_WITH_DASHES_FORMAT_WITH_UTC_5_HOURS = "yyyy-MM-dd'T'HH:mm:ss.SSS-05:00";

private static final String ASSERT_DATE = "2021-04-21T00:00:00.000-04:00";

private static final org.joda.time.DateMidnight jodaDateMidnight = new org.joda.time.DateMidnight(2021, 5, 21, org.joda.time.DateTimeZone.UTC);

private static final java.time.LocalDateTime javaLocalDateTime = java.time.LocalDateTime.of(2021, 04, 04, 07, 27, 20, 2000000);

private static final org.joda.time.LocalDateTime jodaLocalDateTime = new org.joda.time.LocalDateTime(2021, 04, 04, 07, 27, 20,

        2);

//You could also use this ISO8601_DATE_FORMATTER to test your methods.

private static final java.text.DateFormat ISO8601_DATE_FORMATTER = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZ");

@Test

public void convertJavaLocalDateTimeToJodaLocalDateTimeTest() {

    org.joda.time.LocalDateTime localDateTime = JodaToJavaDateUtils.convertJavaLocalDateTimeToJodaLocalDateTime(javaLocalDateTime);

    Assert.assertEquals(new org.joda.time.LocalDateTime(2021, 04, 04, 07, 27, 20, 2), localDateTime);

}

@Test

public void convertJodaLocalDateTimeToJavaLocalDateTimeTest() {

    java.time.LocalDateTime localDateTime = JodaToJavaDateUtils.convertJodaLocalDateTimeToJavaLocalDateTime(jodaLocalDateTime);

    Assert.assertEquals(java.time.LocalDateTime.of(2021, 04, 04, 07, 27, 20, 2000000), localDateTime);

}

@Test

public void convertStringDateFormatToJodaDateTimeTest() {

    String stringDate = "210121";

    org.joda.time.DateTime dateTime = JodaToJavaDateUtils.convertStringDateFormatToJodaDateTime(stringDate, YY_MM_DD_NO_DASHES_FORMAT);

    Assert.assertEquals("2021-01-21T00:00:00.000-05:00", dateTime.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_HH_MM_NO_DASHES_FORMATToJodaDateTimeTest() {

    String stringDate = "202101211115";

    org.joda.time.DateTime dateTime = JodaToJavaDateUtils.convertStringDateFormatToJodaDateTime(stringDate, YYYY_MM_DD_HH_MM_NO_DASHES_FORMAT);

    Assert.assertEquals("2021-01-21T11:15:00.000-05:00", dateTime.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_NO_DASHES_FORMATToJodaDateTimeTest() {

    String stringDate = "20190121";

    org.joda.time.DateTime dateTime = JodaToJavaDateUtils.convertStringDateFormatToJodaDateTime(stringDate, YYYY_MM_DD_NO_DASHES_FORMAT);

    Assert.assertEquals("2019-01-21T00:00:00.000-05:00", dateTime.toString());

}

@Test

public void convertStringDateFormatYY_MM_DD_HH_MM_NO_DASHES_WITH_COLON_FORMATToJodaDateTimeTest() {

    String stringDate = "210121:2352";

    org.joda.time.DateTime dateTime = JodaToJavaDateUtils.convertStringDateFormatToJodaDateTime(stringDate, YY_MM_DD_HH_MM_NO_DASHES_WITH_COLON_FORMAT);

    Assert.assertEquals("2021-01-21T23:52:00.000-05:00", dateTime.toString());

}

@Test

public void convertStringDateFormatYY_MM_DD_NO_DASHES_FORMATToJodaDateTimeTest() {

    String stringDate = "210129";

    org.joda.time.DateTime dateTime = JodaToJavaDateUtils.convertStringDateFormatToJodaDateTime(stringDate, YY_MM_DD_NO_DASHES_FORMAT);

    Assert.assertEquals("2021-01-29T00:00:00.000-05:00", dateTime.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_HH_MM_SS_NO_DASHES_FORMATToJodaDateTimeTest() {

    String stringDate = "210121235210";

    org.joda.time.DateTime dateTime = JodaToJavaDateUtils.convertStringDateFormatToJodaDateTime(stringDate, YYYY_MM_DD_HH_MM_SS_NO_DASHES_FORMAT);

    Assert.assertEquals("2021-01-21T23:52:10.000-05:00", dateTime.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_HH_MM_NO_DASHES_WITH_COLON_FORMATToJodaDateTimeTest() {

    String stringDate = "20210127:0707";

    org.joda.time.DateTime dateTime = JodaToJavaDateUtils.convertStringDateFormatToJodaDateTime(stringDate, YYYY_MM_DD_HH_MM_NO_DASHES_WITH_COLON_FORMAT);

    Assert.assertEquals("2021-01-27T07:07:00.000-05:00", dateTime.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_T_HH_MM_SS_WITH_DASHES_FORMATToJodaDateTimeTest() {

    String stringDate = "2021-01-27T07:07:00";

    org.joda.time.DateTime dateTime = JodaToJavaDateUtils.convertStringDateFormatToJodaDateTime(stringDate, YYYY_MM_DD_T_HH_MM_SS_WITH_DASHES_FORMAT);

    Assert.assertEquals("2021-01-27T07:07:00.000-05:00", dateTime.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_T_HH_MM_SS_SS_WITH_DASHES_FORMATToJodaDateTimeTest() {

    String stringDate = "2021-01-27T07:13:00.45";

    org.joda.time.DateTime dateTime = JodaToJavaDateUtils.convertStringDateFormatToJodaDateTime(stringDate, YYYY_MM_DD_T_HH_MM_SS_SS_WITH_DASHES_FORMAT);

    Assert.assertEquals("2021-01-27T07:13:00.450-05:00", dateTime.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_T_HH_MM_SS_SSS_WITH_DASHES_FORMATToJodaDateTimeTest() {

    String stringDate = "2021-01-27T07:13:00.452";

    org.joda.time.DateTime dateTime = JodaToJavaDateUtils.convertStringDateFormatToJodaDateTime(stringDate, YYYY_MM_DD_T_HH_MM_SS_SSS_WITH_DASHES_FORMAT);

    Assert.assertEquals("2021-01-27T07:13:00.452-05:00", dateTime.toString());

}

@Test

public void convertStringDateFormatToJodaLocalDateTest() {

    String stringDate = "2000-01-01";

    org.joda.time.LocalDate localDate = JodaToJavaDateUtils.convertStringDateFormatToJodaLocalDate(stringDate, YYYY_MM_DD_WITH_DASHES_FORMAT);

    Assert.assertEquals(org.joda.time.LocalDate.parse(stringDate), localDate);

}

@Test

public void convertJodaLocalDateToJavaLocalDateTest() {

    org.joda.time.LocalDate localDate = new org.joda.time.LocalDate("2010-04-28");

    String stringDate = JodaToJavaDateUtils.convertJodaLocalDateToJavaLocalDate(localDate).toString();

    Assert.assertEquals("2010-04-28", stringDate);

}

@Test

public void convertJodaDateMidnightToJavaLocalDateTimeTest() {

    org.joda.time.DateMidnight dateMidnight = org.joda.time.DateMidnight.now();

    java.time.LocalDateTime yy = JodaToJavaDateUtils.convertJodaDateMidnightToJavaLocalDateTime(dateMidnight);

    Assert.assertEquals(StringUtils.removeEndIgnoreCase(dateMidnight.toString(), ":00.000-04:00"), yy.toString());

}

@Test

public void convertJodaDateMidnightToJavaLocalDateTest() {

    org.joda.time.DateMidnight dateMidnight = org.joda.time.DateMidnight.now();

    java.time.LocalDate localDate = JodaToJavaDateUtils.convertJodaDateMidnightToJavaLocalDate(dateMidnight);

    Assert.assertEquals("2021-05-04", localDate.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_T_HH_MM_WITH_DASHES_FORMATToJodaDateMidnightTest() {

    org.joda.time.DateMidnight dateMidnight = JodaToJavaDateUtils.convertStringDateFormatToJodaDateMidnight("2021-04-21T00:00", YYYY_MM_DD_T_HH_MM_WITH_DASHES_FORMAT);

    Assert.assertEquals(ASSERT_DATE, dateMidnight.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_T_HH_MM_SS_SSS_Z_WITH_DASHES_FORMATToJodaDateMidnightTest() {

    org.joda.time.DateMidnight dateMidnight = JodaToJavaDateUtils.convertStringDateFormatToJodaDateMidnight("2021-04-29T01:22:00.000Z", YYYY_MM_DD_T_HH_MM_SS_SSS_Z_WITH_DASHES_FORMAT);

    Assert.assertEquals("2021-04-28T00:00:00.000-04:00", dateMidnight.toString());

}

@Test

public void convertStringDateFormatToJavaLocalDateTest() {

    java.time.LocalDate localDate = JodaToJavaDateUtils.convertStringDateFormatToJavaLocalDate(jodaDateMidnight.toString(), YYYY_MM_DD_T_HH_MM_SS_SSS_Z_WITH_DASHES_FORMAT);

    Assert.assertEquals("2021-05-20", localDate.toString());

}

@Test

public void convertStringDateFormatToJavaLocalDateTimeTest() {

    java.time.LocalDateTime localDateTime = JodaToJavaDateUtils.convertStringDateFormatToJavaLocalDateTime(jodaDateMidnight.toString(), YYYY_MM_DD_T_HH_MM_SS_SSS_Z_WITH_DASHES_FORMAT);

    Assert.assertEquals("2021-05-20T00:00", localDateTime.toString());

}

@Test

public void convertStringDateFormatToJodaDateMidnightTest() {

    org.joda.time.DateMidnight dateMidnight = JodaToJavaDateUtils.convertStringDateFormatToJodaDateMidnight(ASSERT_DATE, YYYY_MM_DD_T_HH_MM_SS_SSS_Z_WITH_DASHES_FORMAT);

    Assert.assertEquals(ASSERT_DATE, dateMidnight.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_T_HH_MM_SS_ZZ_WITH_DASHES_FORMATToJodaDateMidnightTest() {

    org.joda.time.DateMidnight dateMidnight = JodaToJavaDateUtils.convertStringDateFormatToJodaDateMidnight("2021-04-21T14:25:30Z", YYYY_MM_DD_T_HH_MM_SS_ZZ_WITH_DASHES_FORMAT);

    Assert.assertEquals(ASSERT_DATE, dateMidnight.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_T_HH_MM_SS_ZZZ_WITH_DASHES_FORMATToJodaDateMidnightTest() {

    org.joda.time.DateMidnight dateMidnight = JodaToJavaDateUtils.convertStringDateFormatToJodaDateMidnight("2021-04-21T14:25:30 GMT", YYYY_MM_DD_T_HH_MM_SS_ZZZ_WITH_DASHES_FORMAT);

    Assert.assertEquals("2021-04-21T00:00:00.000Z", dateMidnight.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_T_HH_MM_SS_SSS_WITH_DASHES_FORMAT_WITH_UTC_4_HOURSToJodaDateMidnightTest() {

    org.joda.time.DateMidnight dateMidnight = JodaToJavaDateUtils.convertStringDateFormatToJodaDateMidnight(ASSERT_DATE, YYYY_MM_DD_T_HH_MM_SS_SSS_WITH_DASHES_FORMAT_WITH_UTC_4_HOURS);

    Assert.assertEquals(ASSERT_DATE, dateMidnight.toString());

}

@Test

public void convertStringDateFormatYYYY_MM_DD_T_HH_MM_SS_SSS_WITH_DASHES_FORMAT_WITH_UTC_5_HOURSToJodaDateMidnightTest() {

    org.joda.time.DateMidnight dateMidnight = JodaToJavaDateUtils.convertStringDateFormatToJodaDateMidnight("2021-04-21T00:00:00.000-05:00", YYYY_MM_DD_T_HH_MM_SS_SSS_WITH_DASHES_FORMAT_WITH_UTC_5_HOURS);

    Assert.assertEquals(ASSERT_DATE, dateMidnight.toString());

}

}


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *