How do I format an ISO-8601 string 2020-02-10T22:55:13-08:00
into a custom format like 1:30pm
? I kept getting unparseable date exception. My code snippet is below:
String string = "2020-02-10T22:55:13-08:00";
SimpleDateFormat sdfSource = new SimpleDateFormat("YYYY-MM-DD'T'HH:mm:ss'Z'", Locale.getDefault());
SimpleDateFormat sdfTarget = new SimpleDateFormat("h:mma", Locale.getDefault());
try {
Date date = sdfSource.parse(string);
String createdAt = sdfTarget.format(date);
System.out.println(createdAt);
} catch (ParseException e) {
e.printStackTrace();
}
private String getZonedDateTime(String startTime,String inExpectedTimeZone){
return ZonedDateTime
.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"))
.withZoneSameInstant(ZoneId.of(inExpectedTimeZone)).format(DateTimeFormatter.ofPattern("hh:mm a"));
}
In the above method just pass the zulu string and expected Time in which you want the value it will be parsed using java 8 ZonedDateTime and DateTimeFormatter.