For java.time.ZonedDateTime, uses ZonedDateTime.now().
1 2 3 4 5 6 7 8 9 10
// get current date-time, with system default time zone DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss"); ZonedDateTime now = ZonedDateTime.now(); System.out.println(dtf.format(now)); // 2021/03/22 16:37:15 System.out.println(now.getOffset()); // +08:00
// get current date-time, with a specified time zone ZonedDateTime japanDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo")); System.out.println(dtf.format(japanDateTime)); // 2021/03/22 17:37:15 System.out.println(japanDateTime.getOffset()); // +09:00
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); System.out.println(dateFormat.format(date)); // 2021/03/22 16:37:15
For java.util.Calendar, uses Calendar.getInstance().
1 2 3
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); System.out.println(dateFormat.format(cal.getTime())); // 2021/03/22 16:37:15
java.time.LocalDate
For the java.time.LocalDate, uses LocalDate.now() to get the current date without a time-zone, and format it with the DateTimeFormatter.
// Get default time zone System.out.println(ZoneOffset.systemDefault()); // Asia/Kuala_Lumpur System.out.println(OffsetDateTime.now().getOffset()); // +08:00
// get current date time, with +08:00 ZonedDateTime now = ZonedDateTime.now(); System.out.println(dtf.format(now)); // 2021/03/22 16:37:15 System.out.println(now.getOffset()); // +08:00
// get get current date time, with +09:00 ZonedDateTime japanDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo")); System.out.println(dtf.format(japanDateTime)); // 2021/03/22 17:37:15 System.out.println(japanDateTime.getOffset()); // +09:00 } }
java.time.Instant
For java.time.Instant, uses Instant.now() to get the seconds passed since the Unix epoch time (midnight of January 1, 1970 UTC), and later convert to other java.time.* date time classes like LocalDate, LocalDateTime and ZonedDateTime.
For the legacy java.util.Date, uses new Date() or new Date(System.currentTimeMillis() to get the current date time, and format it with the SimpleDateFormat.
publicclassDateExample{ publicstaticvoidmain(String[] args){ DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date(); System.out.println(dateFormat.format(date)); // 2021/03/22 16:37:15
// new Date() actually calls this new Date(long date) Date date2 = new Date(System.currentTimeMillis()); System.out.println(dateFormat.format(date)); // 2021/03/22 16:37:15 } }
java.util.Calendar (Legacy)
For the legacy java.util.Calendar, uses Calendar.getInstance() to get the current date time, and format it with the SimpleDateFormat.
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !