How do I parse this date "Wednesday, 12 June 2019 14:23:39" which comes as a String to a date format like this "2019-03-05T11:56:13Z" in JAVA?
Asked
Active
Viewed 2,335 times
0
-
1What have you tried so far? – Jason Aug 27 '19 at 14:16
-
Welcome to Stack Overflow. I know we’re picky here: one of the things we expect from questioner’s is you search an research before posting your question. You’re likely to find many answers (good and bad ones) faster than anyone can type an answer here. [I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/). – Ole V.V. Aug 27 '19 at 15:15
-
1You tagged your question simpledateformat. You don’t want to use `SimpleDateFormat`. That class is poorly designed and notoriously troublesome. Instead use `DateTimeFormatter` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 27 '19 at 15:16
-
What is the exact result? The trainling `z` in `2019-03-05T11:56:13Z` indicates UTC, but your source string doesn’t inform us of a time zone or offset. Which is it? Without knowing we cannot make the conversion correctly. – Ole V.V. Aug 27 '19 at 15:18
2 Answers
2
You should use the java.time
API for this:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEEE, dd MMMM yyyy HH:mm:ss", Locale.ENGLISH);
LocalDateTime localDateTime = LocalDateTime.parse("Wednesday, 12 June 2019 14:23:39", dateTimeFormatter);
String isoDateTime = localDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(isoDateTime);
which results in:
2019-06-12T14:23:39

whitebrow
- 2,015
- 21
- 24
0
attached example of how you can parsar your date
greetings
public static void main(String[] args) throws ParseException {
String date_s = "2011-01-18 00:00:00.0";
// *** note that it's "yyyy-MM-dd hh:mm:ss" not "yyyy-mm-dd hh:mm:ss"
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = dt.parse(date_s);
// *** same for the format String below
SimpleDateFormat dt1 = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss");
System.out.println(dt1.format(date));
}

Patricio Fernandez
- 101
- 3
-
Please 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. Aug 27 '19 at 15:12