Tuesday 27 December 2016

java - Convert string date to ISO format date




I need to convert some string date to another format string date



String current = "Tue Apr 16 10:59:11 EDT 2019";


I want to get result date String in format ISO-8601 in accordance with the pattern - "yyyy-MM-dd'T'HH:mm:ss.SSSXX"




Could you please help me to implement this?


Answer



Using Java8 DateTime API you can do something like following:



     //your input
String date = "Tue Apr 16 10:59:11 EDT 2019";
//create new formatter for parsing your input
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss zzz yyyy", Locale.ENGLISH);
//create new zonedDateTime from parsing an input with provided formatter
ZonedDateTime zonedDateTime = ZonedDateTime.parse(date,formatter);



Get it in the format of your own:



     //format your zonedDateTime with a new provided pattern
String test = zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXX"));
//print it
System.out.println(test);

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...