java.time Best Practices


Types in Java.time

No time zone

  • java.time.DayOfWeek/Month/Year
  • java.time.YearMonth/MonthDay
  • LocalDate, LocalDate, LocalDateTime

With time zone

  • ZonedDateTime: a date-time with a time-zone
  • OffsetDateTime
  • ZoneId: a time-zone ID

APIs

Traps

  • Duration.get() only works with SECONDS or NANOS

Avoid any APIs that use the default system time zone

  • If client need use the system default time-zone, pass ZoneId.systemDefault()

Avoid offset-based “time zones”

  • do not support daylight savings changes.
  • They do not respect other changes in a region’s offset over time.

Don’t use primitives to represent dates or times

  • Unit confusion: easy to make mistake, difficult when debugging and logging
  • Use Instant instead of the number of milliseconds to represent a point in time.
  • Use Duration to represent an elapsed amount of time
  • If you must use primitives, include the unit in the methods name, parameter names.
  • Measure elapsed time with Guava’s Stopwatch

Converting between time units

  • Using Duration
    • Duration.ofSeconds(seconds).toMillis();
    • throw ArithmeticException correctly if an overflow occurs.
  • Use Period to get the difference in days/months/years
    • Period.between(startDate, endDate).getMonths()
  • Use java.time.temporal.ChronoUnit example to know the difference in days/months/years
    • ChronoUnit.DAYS.between(startDate, endDate)
  • Using java.util.concurrent.TimeUnit
    • TimeUnit.SECONDS.toMillis(seconds);
    • TimeUnit.toUnit/convert silently saturates to Long.MIN_VALUE/MAX_VALUE on overflows!

Misc Tips

  • Avoid java.util.Date/Calendar
    • Use ‘date.toInstant()’ to convert to new java.time APIs.
  • Avoid Joda in new API
  • the Clock class is intended for testing only
  • Avoid any APIs that use the default system clock like ZonedDateTime.now(), Instant.now()
  • use date-time types in database to track date-time values

Use Timestamp and Duration in protocolbuffers

From Error Prone

  • JavaTimeDefaultTimeZone
    • java.time APIs that silently use the default system time-zone are not allowed.
  • Use of java.time.Duration.withSeconds(long) is not allowed. > Duration’s withSeconds(long) method is often a source of bugs because it returns a copy of the current Duration instance, but only the seconds field is mutated (the nanos field is copied directly). Use Duration.ofSeconds(seconds, duration.getNano()) instead.

Date-Time Design Principles

  • Immutable, Fluent,
  • Domain-driven design
  • Separation of chronologies

Problems with old Date/Calendar

  • Date/Calendar/DateFormat are mutable
  • Not intuitive
    • month are 0 based, years are 1900 based.
  • Timezones: uses String to represent TimeZone
  • DateFormat is mutable, only works with Date, not Calendar
    • We can pass a Calendar, but it doesn’t work.
    • DateFormat is not thread-safe <!– https://www.threeten.org/threeten-extra/ org.threeten.extra ThreeTen-Extra contains additional date-time classes that complement those in java.time. These include:

Days, Weeks, Months and Years - a whole-number amount of time for various units (e.g., “7 weeks”) DayOfMonth - a day-of-month without month or year (e.g., “the 14th day of the month”) DayOfYear - a day-of-year without year (e.g., “the 51st day of the year”) AmPm - before or after midday Quarter - the four quarters of the year (e.g., “the 3rd quarter of the year”) YearQuarter - combines a year and quarter, (e.g., “the 3rd quarter of 2014”) YearWeek - combines a week-based-year and a week, (e.g., “the 6th week of 2014”) –>

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)