A collection of Java date and time examples.

1. Java Date Time APIs

In old days, we use the following classic Date and Calendar APIs to represent and manipulate date.

  • java.util.Date – date and time, print with default time-zone.
  • java.util.Calendar – date and time, more methods to manipulate date.
  • java.text.SimpleDateFormat – formatting (date -> text), parsing (text -> date) for date and calendar.

In Java 8, a new series of date and time APIs (JSR310 and inspired by Joda-time) are created in the new java.time package.

  • java.time.LocalDate – date without time, no time-zone.
  • java.time.LocalTime – time without date, no time-zone.
  • java.time.LocalDateTime – date and time, no time-zone.
  • java.time.ZonedDateTime – date and time, with time-zone.
  • java.time.DateTimeFormatter – formatting (date -> text), parsing (text -> date) for java.time
  • java.time.Instant – date and time for machine, seconds passed since the Unix epoch time (midnight of January 1, 1970 UTC)
  • java.time.Duration – Measures time in seconds and nanoseconds.
  • java.time.Period – Measures time in years, months and days.
  • java.time.TemporalAdjuster – Adjust date.

Note
Read Oracle article – Why do we need a new date and time library?

2. Java Date and Time Examples

List of the tutorials and updated with the new Java 8 java.time examples.

  1. Java Date and Calendar examples
  2. Java – How to compare dates
  3. Java – How to get current date time
  4. Java – How to get current timestamp
  5. Java – How to convert String to a Date
  6. Java – How to calculate elapsed time
  7. Java 8 – How to convert String to LocalDate
  8. Java 8 – How to format LocalDateTime
  9. Java 8 – Convert Instant to LocalDateTime
  10. Java 8 – Convert Instant to ZoneDateTime
  11. Java 8 – Convert Date to LocalDate and LocalDateTime
  12. Java 8 – ZonedDateTime examples
  13. Java – Convert date and time between timezone
  14. Java – Display all ZoneId and its UTC offset
  15. Java – Display list of TimeZone with GMT
  16. Java 8 – Duration and Period examples
  17. Java – How to add days to current date
  18. Java 8 – TemporalAdjusters examples
  19. Java 8 – MinguoDate examples (Taiwan calendar)
  20. Java 8 – HijrahDate, How to calculate the Ramadan date (Islamic calendar)
  21. Java 8 – Difference between two LocalDate or LocalDateTime
  22. Java – Check if the date is older than 30 days or 6 months
  23. Java 8 – Convert Epoch time milliseconds to LocalDate or LocalDateTime
  24. Java 8 – How to calculate days between two dates?
  25. Java 8 – How to parse date with “dd MMM” (02 Jan), without year?
  26. Java 8 – Unable to obtain LocalDateTime from TemporalAccessor
  27. Java 8 – How to parse date with LocalDateTime
  28. Java 8 – Convert ZonedDateTime to Timestamp
  29. Java 8 – Convert LocalDateTime to Timestamp
  30. Java – How to change date format in a String

References

  1. Read Oracle article – Why do we need a new date and time library?
  2. Joda Time
  3. JSR 310: Date and Time API
  4. SimpleDateFormat JavaDoc
  5. DateTimeFormatter JavaDoc
  6. java.util.Date JavaDoc
  7. java.util.Calendar JavaDoc
  8. Read this – Java Date vs Calendar

Leave a Reply

Your email address will not be published. Required fields are marked *