问题
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?
回答1:
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);
回答2:
There is a SimpleDateFormat
in java, and there is a parse method in there
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateString = format.format( new Date() );
Date date = format.parse ( "2009-12-31" );
回答3:
None of Java's time classes fully implement ISO 8601 but you can try with Java 8:
ZonedDateTime zp = ZonedDateTime.parse(string);
Date date = Date.from(zp.toInstant());
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX");
System.out.println(dt1.format(date));
来源:https://stackoverflow.com/questions/55703909/convert-string-date-to-iso-format-date