fork download
  1. import java.time.Instant;
  2. import java.time.LocalTime;
  3. import java.time.ZoneId;
  4. import java.time.format.DateTimeFormatter;
  5. import java.time.format.FormatStyle;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. // ZoneId.systemDefault() returns the system default time zone.
  10. // Replace it as per your requirement e.g. ZoneId.of("Europe/London").
  11. LocalTime time = LocalTime.now(ZoneId.systemDefault());
  12. System.out.println(time);
  13.  
  14. // Formatted string value of time
  15. System.out.println(time.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM)));
  16. System.out.println(time.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
  17.  
  18. // If you want to get values of time units e.g. hour from this object
  19. System.out.println(time.getHour());
  20.  
  21. // If you want to get the epoch milliseconds and epoch seconds
  22. Instant instant = Instant.now();
  23. System.out.println(instant.toEpochMilli());
  24. System.out.println(instant.getEpochSecond());
  25. }
  26. }
Success #stdin #stdout 0.14s 59640KB
stdin
Standard input is empty
stdout
17:16:11.916501
5:16:11 PM
17:16:11
17
1729790171993
1729790171