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 DateTimeFormat
s, 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
Retrofit Expected Begin_Object But Was Begin_Array
How to Get a Context in a Recycler View Adapter
Convert JSONarray to String Array
Java - Process.Destroy() Source Code for Linux
JPA Cascadetype.All Does Not Delete Orphans
Parsing an Arithmetic Expression and Building a Tree from It in Java
How to Create a Directory in Java
What Operations in Java Are Considered Atomic
Jersey /* Servlet Mapping Causes 404 Error for Static Resources
How to Call JavaScript from Android
Android: Sending Data >20 Bytes by Ble
How to Use Interface to Communicate Between Two Activities
Inputstream.Available() Is 0 Always
Java.Lang.Classnotfoundexception in Spite of Using Classpath Environment Variable