I have a JSP which has data picker and the output returns datastring in dd-MM-yyyy format.
now I want the output exactly in the date time format -> 2019-01-01T01:01:59Z.
I guess this is from simple date format to date time format . I tried and its throwing error. here is my code. formatedate2 is failing.
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse("2018-04-10T04:00:00.000Z", inputFormatter);
String formattedDate = outputFormatter.format(date);
LocalDate date2 = LocalDate.parse("12-10-2019", outputFormatter);
String formattedDate2 = inputFormatter.format(date2);
System.out.println(formattedDate2);
can someone guide me on ho to accomplish this. Appreciate your great help
This takes your LocalDate and converts it to a LocalDateTime using LocalTime.MIN which is 00:00. We then convert the LocalDateTime to a ZonedDateTime using atZone with a ZoneId of UTC. We then format that ZonedDateTime to the format you want using format(). I hope this helps.
LocalDate date = LocalDate.parse("2018-04-10");
LocalDateTime localDateTime = date.atStartOfDay();
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("UTC"));
System.out.println(zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")));
Output: 2018-04-10T00:00:00.000Z