Date and Time

  • package: java.time
  • LocalDate — for date only: years, months, days
  • LocalTime — for time only: hour, minutes, seconds, nanoseconds
  • LocalDateTime — LocalDate + LocalTime: No Timezone info
  • Instant
    • represent an instantaneous moment on the timeline
    • internally long variable to store seconds counting from epoch
    • internally int variable to store nanosecond
    • useful for generating a time stamp to represent machine time
    • Doesn’t include Timezone and hence is always at GMT0
  • Period — date based
    • Period.between(startLocalDate, endLocalDate)
    • Don’t have timezone info
  • Duration — time based
    • Duration.between(startLocalTime, endLocalTime)
    • Don’t have timezone info
  • With Timezone info
    • OffsetDateTime — LocalDateTime + ZoneOffset
    • OffsetTime — LocalTime + ZoneOffset
    • ZonedDateTime
      • OffsetDateTime + ZoneRules
      • account for daylight savings and any other anomalies
  • legacy
    • java.util.Date
    • java.util.Calendar
    • java.util.TimeZone

Epoch

  • 1970-01-01T00:00:00Z (1 Jan 1970 GMT)