GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Date & Time — java.evolved

Home / Topics / Date & Time
🏷 Date & Time 5 patterns

Topic: Date & Time

All Java patterns related to Date & Time — java.evolved

Date/Time

java.time API basics

Old // Mutable, confusing, zero-indexed months Calendar cal = Calendar.getInstance(); cal.set(2025, 0, 15); // January = 0! Date date = cal.getTime(); // not thread-safe
Modern LocalDate date = LocalDate.of( 2025, Month.JANUARY, 15); LocalTime time = LocalTime.of(14, 30); Instant now = Instant.now(); // immutable, thread-safe
hover to see modern →
JDK 8+ learn more →
Date/Time

Duration and Period

Old // How many days between two dates? long diff = date2.getTime() - date1.getTime(); long days = diff / (1000 * 60 * 60 * 24); // ignores DST, leap seconds
Modern long days = ChronoUnit.DAYS .between(date1, date2); Period period = Period.between( date1, date2); Duration elapsed = Duration.between( time1, time2);
hover to see modern →
JDK 8+ learn more →
Date/Time

Date formatting

Old // Not thread-safe! SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String formatted = sdf.format(date); // Must synchronize for concurrent use
Modern DateTimeFormatter fmt = DateTimeFormatter.ofPattern( "uuuu-MM-dd"); String formatted = LocalDate.now().format(fmt); // Thread-safe, immutable
hover to see modern →
JDK 8+ learn more →
Date/Time

Instant with nanosecond precision

Old // Millisecond precision only long millis = System.currentTimeMillis(); // 1708012345678
Modern // Microsecond/nanosecond precision Instant now = Instant.now(); // 2025-02-15T20:12:25.678901234Z long nanos = now.getNano();
hover to see modern →
JDK 9+ learn more →
Date/Time

Locale constructors to Locale.of()

Old Locale brazilianPortuguese = new Locale("pt", "BR");
Modern Locale brazilianPortuguese = Locale.of("pt", "BR");
hover to see modern →
JDK 19+ learn more →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.