Converting a Date String to a Datetime Object Using Joda Time Library

Converting a date string to a DateTime object using Joda Time library

Use DateTimeFormat:

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);

Joda Time String to Date and time convertion

joda-time does not support microseconds.

You can parse it as in below:

String date = "01-12-2015";
DateTime dateTime = DateTime.parse(date, DateTimeFormat.forPattern("dd-MM-yyyy"));

System.out.println(dateTime);
//2015-12-01T00:00:00.000+02:00

Convert date string to datetime object in Joda-Time?

I believe the correct syntax for the date pattern is "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'". That way the Z is literally used.

Converting Time Zones from a String to UTC (JODA TIME)

To show that Andreas in the comment has hit the nail right on: I ran the following snippet in America/Coral_Harbour time zone (since I didn’t know your exact time zone, Eastern Standard Time is used in several (though fewer after 8 March when Eastern Daylight Time began)).

    String date = "2020-03-12T01:23:45.678+0000";

System.out.println("This is the string: " + date);

DateTime dt = new DateTime(date);
DateTime dt2 = new DateTime(dt).toDateTime(DateTimeZone.getDefault());

System.out.println("This is the first time: " + dt);
System.out.println("This is the second time: " + dt2);

Output is:

This is the string:      2020-03-12T01:23:45.678+0000
This is the first time: 2020-03-11T20:23:45.678-05:00
This is the second time: 2020-03-11T20:23:45.678-05:00

Compare the first two lines and notice that the conversion from UTC to EST has already happened when parsing the string.

As an aside, since your string is in ISO 8601 format, you don’t need to specify any formatter for parsing it. The DateTime(Object) constructor accepts it. But the same conversion happened in your parsing.

What happened in your code?

Repeating the quote from Andreas’ comment:

If the withOffsetParsed() has been called, then the resulting
DateTime will have a fixed offset based on the parsed time zone.
Otherwise the resulting DateTime will have the zone of this formatter,
but the parsed zone may have caused the time to be adjusted.

So your formatter has the default time zone of your device, and therefore also the DateTime object that you get from parsing.

So when creating dt2 you were converting from Eastern Standard Time to Eastern Standard Time and therefore got the same date-time again.

Link: Documentation of DateTimeFormatter.parseDateTime()

Joda Time - Convert a String into a DateTime with a particular timezone and in a particular format

Use DateTimeFormatterBuilder to build a formatter that is able to parse/format multiple DateTimeFormats, and set the resulting DateTimeFormatter to use a specified DateTimeZone:

DateTimeParser[] parsers = { 
DateTimeFormat.forPattern("MM-dd-yyyy").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd").getParser(),
DateTimeFormat.forPattern("MM/dd/yyyy").getParser(),
DateTimeFormat.forPattern("yyyy/MM/dd").getParser()
};

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(null, parsers)
.toFormatter()
.withZone(DateTimeZone.UTC);

DateTime dttm1 = formatter.parseDateTime("01-31-2012");
DateTime dttm2 = formatter.parseDateTime("01/31/2012");
DateTime dttm3 = formatter.parseDateTime("2012-01-31");

To format a given DateTime you can just use dttm1.toString("yyyy-MM-dd")).

Scala date time convertion using joda datetime

As far as I can tell, the easiest would be to just use DateTimeFormatter's print() method:

val dateFormatGeneration: DateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");

def convertDateTime(dt:DateTime):String={
dateFormatGeneration.print(dt)
}


Related Topics



Leave a reply



Submit