How to convert from "2014-06-16T07:00:00.000Z" to "16-JUN-14 07:00:00" using joda time API?
The below code is throwing the exception
java.lang.IllegalArgumentException: Illegal pattern component: T
at org.joda.time.format.DateTimeFormat.parsePatternTo(DateTimeFormat.java:570)
at org.joda.time.format.DateTimeFormat.createFormatterForPattern(DateTimeFormat.java:693)
at org.joda.time.format.DateTimeFormat.forPattern(DateTimeFormat.java:181)
at com.joda.JodaTimeTest.convertJodaTimezone(JodaTimeTest.java:59)
at com.joda.JodaTimeTest.main(JodaTimeTest.java:50)
This is the code:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-ddTHH:mm:ssZ");
DateTime dt = formatter.parseDateTime(dstDateTime.toString());
You need to enclose the literal T within single quotes. Also the milliseconds are not properly patterned. You need to include SSS for the milliseconds. Have a look at the patterns here for more info.
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Update: To format the DateTime into a String representation of your choice, you need to do this.
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DateTime dt = formatter.parseDateTime(dstDateTime.toString()); // You get a DateTime object
// Create a new formatter with the pattern you want
DateTimeFormatter formatter2 = DateTimeFormat.forPattern("dd-MMM-yy HH:mm:ss");
String dateStringInYourFormat = formatter2.print(dt); // format the DateTime to that pattern
System.out.println(dateStringInYourFormat); // Prints 16-Jun-14 12:30:00 because of the TimeZone I'm in
Either specify the timezone yourself or your default system timezone would be taken.
You need to change
"yyyy-MM-ddTHH:mm:ssZ"
To
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z"
Now
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z");
DateTime dt = formatter.parseDateTime("2014-06-16T07:00:00.000Z");
System.out.println(dt);
Output:
2014-06-16T07:00:00.000+05:30
Related
I have this code and I want to print out the time as String without the 'T' character between date and time.
String datetime4 =new StringBuilder().append(date4).append(time4).toString();
DateTime newdt=new DateTime(datetime4);
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
newdt = formatter.parseDateTime(datetime4);
System.out.println(newdt);
Notice that date4 and time4 are String variables.
It will print:
2017-11-04T11:23:00.000+02:00
One way of doing it:
String date4 = "2017-02-02";
String time4 = "12:00:00";
//To parse it to Temporal object
DateTime dateTime = DateTime.parse(date4 +"T"+ time4);
// to output it as String in a prefered format (Thanks #Hugo)
System.out.println(dateTime.toString("yyyy-MM-dd HH:mm:ss"));
If you prefer Java 8 you will need to use formatter I think, LocalDateTime doesn't overload toString in the same way as JodaTime.
But not sure why you want to do this? seems like just appending both date and time is enough? Anyway if you want to parse to the date you need to put T as is needed to pass it as a valid date time format to DateTime as well as LocalDateTime if using Java8, then you can reformat it as you wish.
String date4 = "2017-02-02";
String time4 = "12:00:00";
LocalDateTime dateTime = LocalDateTime.parse(date4 +"T"+ time4);
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
Using Java 8 LocalDateTime;
LocalDateTime dateTime;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss");
DateTimeFormatter desiredFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
dateTime = LocalDateTime.parse("2017-06-01T12:10:10", formatter);
System.out.println(desiredFormat.format(dateTime));
When you do:
System.out.println(newdt);
You're printing the newdt variable, and internally println calls the toString() method on the object.
As this variable's type is DateTime, this code outputs the result of newdt.toString(). And Datetime.toString() method uses a default format that contains the "T".
If you want the output String to have a different format, you can do something like this:
System.out.println(newdt.toString("yyyy-MM-dd HH:mm:ss"));
The output will be:
2017-11-04 11:23:00
(without the "T")
You can use this version of toString with any pattern accepted by DateTimeFormatter.
You can also create another DateTimeFormatter for the format you want:
DateTimeFormatter withoutT = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(withoutT.print(newdt));
The output will be the same, it's up to you to choose.
I'm trying to get the current DateTime with my DateTimeFormat pattern, but i'm getting the exception...
//sets the current date
DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
DateTime now = dtf.parseDateTime(currentDate.toString());
I'm getting this exception, I cannot understand who is giving the malformed format
java.lang.IllegalArgumentException: Invalid format: "2017-01-04T14:24:17.674+01:00" is malformed at "17-01-04T14:24:17.674+01:00"
This line DateTime now = dtf.parseDateTime(currentDate.toString()); isn't correct because you try parse date with default toSring format. You have to parse string which formatted the same way as pattern:
DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
String formatedDate = dtf.print(currentDate);
System.out.println(formatedDate);
DateTime now = dtf.parseDateTime(formatedDate);
System.out.println(now);
You are using the wrong format to parse the date. If you print out the date you are trying to parse after converting it to a String with toString you get:
2017-01-04T14:24:17.674+01:00
This date string does not conform to the pattern dd/MM/YYYY HH:mm. To parse the to a string converted currentDate to a DateTime object again, you have to use the following pattern:
DateTimeFormatter dtf = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSZ")
.withLocale(locale);
Parsing with this DateTimeFormatter will get you another instance that represents the same time as the original currentDate.
For more details on the DateTimeFormatter and it's parsing options check out the JavaDoc
If I have code such as this:
DateTimeParser[] parsers = {
DateTimeFormat.forPattern("yyyyMM").getParser(),
DateTimeFormat.forPattern("yyyyMMdd").getParser(),
DateTimeFormat.forPattern("yyyyMMddHHmm").getParser(),
DateTimeFormat.forPattern("yyyyMMddHHmmZ").getParser(),
DateTimeFormat.forPattern("yyyyMMddHHmmss.SSSSZ").getParser()
};
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
final DateTime dateTime = formatter
.parseDateTime(dateString);
return new Timestamp(dateTime.getMillis());
How can I determine which pattern was used? I want to force UTC timezone only in situation when no timezone is explicitly provided. ie, when Z is missing
Alternatively, how can I simply determine if a timezone was provided when parsing a joda datetime?
You can always get the zone from the code below with withOffsetParsed() and compare with your default zone
String dateString = "2016052914346.2354+0300";
DateTimeParser[] parsers = {DateTimeFormat.forPattern("yyyyMM").getParser(),DateTimeFormat.forPattern("yyyyMMddHHmmss.SSSSZ").getParser()};
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
DateTime dateTime = formatter.withOffsetParsed().parseDateTime(dateString);
DateTimeZone zone = dateTime.getZone();//+03.00
I have the following date:
2011-10-07T08:51:52.006Z
Now I want to parse it into a GregorianCalendar. Is there an easier way to do it than using substrings and parsing them to Integers?
And what is the Z in the time string?
I tried to parse it using SimpleDateFormat, but I canĀ“t find a explanation for the T in the date String.
DateFormat format = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" )
Date date = format.parse( "2011-10-07T08:51:52.006Z" );
Calendar calendar = new GregorianCalendar();
calendar.setTime( date );
I would take a look at DateTimeFormatter
DateTimeFormatter formatter =
DateTimeFormat.forPattern("<custom_pattern>").withOffsetParsed();
DateTime dateTime = formatter.parseDateTime("<your_input>");
GregorianCalendar cal = dateTime.toGregorianCalendar();
The T in your string acts as a separator between the date and the time and the Z is the time-zone information both as per ISO-8601 format.
You could use the SimpleDateFormatter to parse the String. Please read the javadoc for the aforementioned class to know what could be the format string. 'Z' indicates the timezone information.
I am trying to use Joda Time both for formatting DateTime objects to String and than parse these strings back to DateTime. But I am failing to so when the pattern includes z:
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MM-yyyy HH:mm:ss.SSS z");
String dts = dtf.print(System.currentTimeMillis());
System.out.println(dts);
DateTime dt = dtf.parseDateTime(dts);
The above code is throwing exception when the parsing the String to DateTime takes occurred.
Do you have any idea?
Yosi
You can do:
DateTime dt = new DateTime();
System.out.println(dt.toString("dd-MM-yyyy HH:mm:ss.SSS z"));
Have a look in the user guide
The Pattern is not correct, maybe try this one
DateTimeFormatter dtf = DateTimeFormat.forPattern( "dd-MM-yyyy HH:mm:ss.SSS'z" );
this worked for me