parse String to 'yyyy-mm-ddThh:mm:ss.SSSZ' ISODate java [duplicate] - java

This question already has answers here:
Java / convert ISO-8601 (2010-12-16T13:33:50.513852Z) to Date object
(4 answers)
Closed 6 years ago.
How to convert String to ISODate format using SimpleDateFormat, which means that i want to finally get Date in java.util.Date format.
My string will look like 2017-02-17T09:28:03.000Z, i want to convert it to date formt. I can do this in Joda date format, but since i am using mongoDB, it does not accept joda format.
String startDateString1 = "2017-02-17T04:23:17.452Z";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date startDate = df.parse(startDateString1);
String newDateString = df.format(startDate);
above code is not working.

Your code is not working since Z is a reserved character used to interpret RFC-822 time zones :
RFC 822 time zone: For formatting, the RFC 822 4-digit time zone format is used:
RFC822TimeZone:
Sign TwoDigitHours Minutes
TwoDigitHours:
Digit Digit
Since Java 7, you can use X to interpret ISO-8601 time zones https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html . The following works :
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
However, on my computer,
System.out.println(newDateString);
results in the following output :
2017-02-17T05:23:17.452+01
Alternatively, if you are sure to have only UTC dates, you could escape the Z letter with simple quotes :
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
And here is the displayed result :
2017-02-17T04:23:17.452Z

You can do it in Java 8 like below.
Instant instant = Instant.parse("2017-02-17T09:28:03.000Z");
Date date = Date.from(instant);

You could use javax.xml.bind.DatatypeConverter.parseDateTime("2017-02-17T04:23:17.452Z") which will return a Calendar object. You can call getTime() on it to get a Date object.

Related

Unable to parse with DateTimeFormatter [duplicate]

This question already has answers here:
How to format LocalDate object to MM/dd/yyyy and have format persist
(4 answers)
Unable to Convert String to localDate with custom pattern [duplicate]
(1 answer)
Closed 1 year ago.
I am trying to parse the date in a specific pattern but every time it is showing date in only format. Here what I am doing. Output is written in comments in front of the line.
String dat = "20-06-2021";
String newFormatLocaleDate = "MMM dd yyyy";
String newFormattedDate = convertToNewFormat(dat,currentFormat,newFormatLocaleDate); //Jun 20 2021
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern(newFormatLocaleDate);
LocalDate parse1 = java.time.LocalDate.parse(newFormattedDate,formatter1); //2021-06-20
System.out.println("Using parse1 Pattern Only "+parse1); //2021-06-20
Date date1 = java.sql.Date.valueOf(parse1);
System.out.println("Using Date Pattern Only "+date1); //2021-06-20
The pattern in which date is required is given and in the string form it giving the correct value but when I am trying to parse it with LocaleDate it is changing the format for all dates to the above mentioned (yyyy-MM-dd) format irrespective of the newFormatLocaleDate pattern.
Thanks
You should not expect the line
System.out.println("Using parse1 Pattern Only "+parse1);
to output something different than 2021-06-20 it literally is exactly what the docs say what will be out putted.
The output will be in the ISO-8601 format uuuu-MM-dd.
So regardless of the format you used while parsing the LocalDate value the toString method will always output a data in the format uuuu-MM-dd.
If you want a different format use the instance method format.
Please note that you are printing the LocalDate and Date instances (using their toString() implementation), therefore not formatted according to your desired template. If you want to print out your parsed date in a particular format, you need to apply a formatter again, for example like this:
System.out.println(parse1.format(DateTimeFormatter.ofPattern("MMM dd yyyy")))

Java iso 8601 universal date time format [duplicate]

This question already has an answer here:
in java I need define date in this format 1999-05-31T13:20:00-05:00 [duplicate]
(1 answer)
Closed 5 years ago.
I want this result on date time: 2008-10-31T15:07:38.6875000-05:00, please help me how i get this result?
I am using following code but unable to get required response.
TimeZone tzone = TimeZone.getTimeZone("UTC");
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
String nowAsISO = dateformat.format(new Date());
You are quite right:
Your format expression is missing seconds :ss, millisecond .SSS (many S for how many digit you want) and the Z timezone tag without '
Using single quote ' in the expression will exclude everything included in them from parsing, ergo you will have it printed like a string.
TimeZone tzone = TimeZone.getTimeZone("UTC");
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ");
String nowAsISO = dateformat.format(new Date());
You can see every possible pattern here

2014-02-09T00:00:00+05:30 my date coming in this string format.how can i convert into java.util.date object

I am receiving the string in this format "2014-02-09T00:00:00+05:30" then how can i convert into java.util.date object.
final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS zzz";
dateInString="2014-02-09T00:00:00+05:30";
final SimpleDateFormat sdf = new SimpleDateFormat(ISO_FORMAT);
final TimeZone utc =TimeZone.getTimeZone("GMT");
Date date = formatter.parse(dateInString);
System.out.println(date);
I have tried this way.Its giving java.text.ParseException:
try this format "yyyy-MM-dd'T'HH:mm:ssXXX" for more information Class SimpleDateFormat
Your format is wrong, I think your looking for yyyy-MM-dd'T'HH:mm:ssXXX.
S is for millisecondes, you don't need this.
Note that XXX will work with Java 7 (see documentation : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html), it's the ISO 8601 time zone format.
With Java 6 you can only use ZZZ but it won't match +05:30 because ZZZ match RFC 822 time zone format
If you're using Java 6, please refer to this answer : https://stackoverflow.com/a/2202300/1140748

Convert ical time to milliseconds

I am trying to parse an iCal-file and write the info to a database for my Android application.
I am using the ical4j-library to parse the data, and as output I get, for example, a date formatted like this: 20140116T121500Z.
I want to convert that date into milliseconds. I tried using Joda, but couldn't get it to work:
Caused by: java.lang.IllegalArgumentException: Invalid format: "20140110T091500Z" is malformed at "1500Z"
Using Joda-Time API for DateTimeFormat:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMdd'T'HHmmssZ");
System.out.println(formatter.parseDateTime("20140116T121500Z").getMillis());
Output:
1389874500000
I am able to parse your date through java.text.SimpleDateFormat by escaping 'Z' representing the timezone (or omit Z altogether):
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
System.out.println(dateFormat.parse("20140116T121500Z").getTime());
Output:
1389854700000
Note: The difference in the 2 times is introduced by diluting the timezone in java.text.SimpleDateFormat, which can be overcome by setting an explicit GMT timezone below:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(dateFormat.parse("20140116T121500Z").getTime());
Output:
1389874500000

JodaTime: How hours are represented

I am working with JodaTime now and have a question about how to parse String into DateTime.
I have got a date String in the format:
"2013-05-14T11:36:08+0000"
I tried to convert it into JodaTime's DateTime object:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
DateTime dateTime = fmt.parseDateTime("2013-05-14T11:36:08+0000");
It works fine except that if I call
dateTime.getHourOfDay()
It returns me 12 instead of 11.
Surprisingly, if I use the Java's SimpleDateFormat:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = df.parse("2013-05-14T11:36:08+0000");
The date contains exactly the same result, the hour is 12 instead of 11.
I am based in London. I started to think whether this is because the summer saving time? Or did I make an mistake on how the String should be parsed?
Please help. Many thanks.

Categories