Last active
November 12, 2020 08:02
-
-
Save armaandhir/fbe09093ecb7c416ce7bbd6750a2b746 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void testDates() throws ParseException { | |
// Desired Format in all scenarios | |
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | |
//Sample string we recieve | |
String runTime = "2020-10-26T00:07:35.965030-04:00[America/Toronto]"; | |
//converting to LocalDateTime for usage | |
LocalDateTime localDateTimeOut = ZonedDateTime.parse(runTime).toLocalDateTime(); | |
System.out.println("LocalDateTime to String without format: " + localDateTimeOut.toString()); | |
// Now convert LocalDateTime to String of format "yyyy-MM-dd hh:mm:ss" | |
String timeString = localDateTimeOut.format(fmt); | |
System.out.println("LocalDateTime to String with format: " + localDateTimeOut.format(fmt)); | |
// We dont need this | |
Date date = java.util.Date.from(localDateTimeOut.atZone(ZoneId.systemDefault()).toInstant()); | |
System.out.println("LocalDateTime to Date without format: " + date); | |
// Converting string in format to Date | |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
System.out.println(format.parse(timeString)); | |
//LocalDateTime dateTime = LocalDateTime.parse(localDateTimeOut, fmt); | |
//String dateAsString = dateTime.format(fmt); | |
//System.out.println(dateAsString); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment