// Before Java 8 (obsolete) Date date = new Date(); System.out.println("java util date now is: "+date); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // date to string System.out.println("formatted: "+sdf.format(date)); // string to date try { Date date911 = sdf.parse("2001-09-11"); System.out.println("911 date: "+date911); } catch (ParseException e) { System.out.println("Can't parse that date"); } // Java 8 LocalDateTime now = LocalDateTime.now(); System.out.println("LocalDateTime now is: "+now); // date to string DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); System.out.println("formatted: "+now.format(formatter)); // string to date LocalDate date911 = LocalDate.of(2001, 9, 11); System.out.println("911 date: "+date911);This is just part of the story... Java 8 can also do timezone a lot easier than previously.
Wednesday, January 15, 2020
Java dates old and new
Java developers put up with the arcane Gregorian Calendar, Date, SimpleDateFormat until Java 8 (which itself is a few years old now).
There certainly are old code who still use the old way. Your favorite IDE can figure out your imports.
Subscribe to:
Post Comments (Atom)
3 comments:
Post a Comment