I want to convert 2019-05-14T17:21:04+0000 to normal date and time format using JAVA and then want to store it in Mysql database table with data type as datetime.
Asked
Active
Viewed 71 times
0
-
5Welcome to stackoverflow. This is not a coding service website. Show us, what you have done so far in a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and you'll get help – gehbiszumeis Jun 07 '19 at 05:43
-
1What have you tried so far? What were the results of those attempts? – Jason Jun 07 '19 at 05:46
-
So you'd have a string initially? – Maurice Perry Jun 07 '19 at 05:47
-
@MauricePerry Yes – Tarishi Jain Jun 07 '19 at 05:50
-
2What do you mean by "normal date and time format"? The format that you have it in looks pretty "normal" to me! – Dawood ibn Kareem Jun 07 '19 at 05:52
-
1You’re asking at least two questions in one, try to avoid that. (1) How to parse a datetime string such as `2019-05-14T17:21:04+0000`? (It’s ISO 8601 format). (2) How to insert a datetime into MySQL. Both questions have been asked and answered many times, so your search engine will bring you good answers faster than anyone can type them here. And when you do need to ask, please remember to show your efforts. Then you will find people here *a lot* more forthcoming, welcoming, friendly – Ole V.V. Jun 07 '19 at 06:29
-
Always **search Stack Overflow** thoroughly before posting. – Basil Bourque Jun 08 '19 at 15:09
1 Answers
2
These are modern formats defined by the ISO 8601 standard.
String timestr = "2019-05-14T17:21:04+0000";
// DateTimeFormatter.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz") ;
OffsetDateTime offsetDateTime = OffsetDateTime.parse(timestr, formatter);
Localize output.
Locale locale = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM ).withLocale( locale ) ;
String output = offsetDateTime.format( f ) ;
System.out.println(output);
Same idea but with the legacy date-time classes.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZ");
try {
Date date = format.parse("2019-05-14T17:21:04+0000");
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Basil Bourque
- 303,325
- 100
- 852
- 1,154

Wing Kui Tsoi
- 474
- 1
- 6
- 16
-
1Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jun 07 '19 at 06:25
-
1
-
I corrected your use of the wrong classes. The `LocalDateTime` is entirely wrong as that class cannot represent a moment. The use of `ZonedDateTime` is incorrect as well, since there is no time zone involved. We have only a mere offset-from-UTC in the input, so `OffsetDateTime` is appropriate. Your original approach, to delete the offset from the input, is unwise and unnecessary. The offset is valuable information and should not be thrown away. – Basil Bourque Jun 08 '19 at 15:18