LocalDateTime and DateTimeFormatter.ofPattern together simplify custom date-time formatting:
String formattedDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"));
System.out.println(formattedDate); // e.g., "2099-12-31 23:59:59" Tomorrow? I don't even know 'er!
Basics of Date Time Classes and Formatting
Java's date time API gives you different classes designed for different purposes. Each supports a specific set of date/time fields, so select the appropriate class for your requirements. Misusing classes and patterns can cause an UnsupportedTemporalTypeException:
// Oops! LocalDate does not have a TimeLord's time information, hence the exception.String formattedDate = LocalDate.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
For your everyday date-time formatting job without time zone, stick to LocalDateTime:
Time zone awareness is a catch-22; necessary but can cause confusion. If you deal with time zones, turn to ZonedDateTime. It's the sonic screwdriver of date-time classes:
String formattedDate = ZonedDateTime.now().format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss z"));
System.out.println(formattedDate); // 2099 never looked so good!
Converting and Customizing Dates
Ever faced the need to convert epoch time to a human-friendly format? Java's got you covered:
long epochMilli = System.currentTimeMillis();
String epochFormattedDate = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")
.withZone(ZoneId.systemDefault())
.format(Instant.ofEpochMilli(epochMilli));
System.out.println(epochFormattedDate); // Because who doesn’t love a good old Unix timestamp?
Navigate international dates effortlessly. Use DateTimeFormatter's withLocale to display dates based on specific locales:
// Who knew ZonedDateTime could also be a language tutor?String esDate = ZonedDateTime.now().format(DateTimeFormatter.ofPattern("EEEE, MMMM d, uuuu", new Locale("es", "ES")));
System.out.println(esDate);
Handling the exceptions, you say? Let's catch `em all!
try {
String formattedDate = LocalDate.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
} catch (UnsupportedTemporalTypeException e) {
e.printStackTrace();
// Because Java likes its patterns just as Sherlock Holmes does - perfect!}
Date Parsing and Pattern Mapping
Parsing dates need not be a sorcery. Understand how the pattern symbols map and it will seem more of a charm:
String inputDate = "2099-12-31T23:59:59"; // Ah! The last minute of 2099.DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(inputDate, formatter);
Handling Time Zones like a Pro
You're already a pro at handling LocalDateTime. Now, go a level up. Work with different time zones efficiently using ZonedDateTime. It's like LocalDateTime with a timezone superpower:
Get into the nitty-gritty details with date-time precision and arbitrary ChronoFields,
LocalDateTime localDateTime = LocalDateTime.now();
String detailedFormat = localDateTime.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSSSS"));
System.out.println(detailedFormat); // Will work unless Java develops a Y10K bug!
To check if a specific field is supported programmatically, call the field-servant interface TemporalAccessor
boolean isSupported = localDateTime.isSupported(ChronoField.SECOND_OF_DAY);
//When in doubt, ask TemporalAccessor. The silent guardian of date-time fields!