Java Datetimeformatter Class
DateTimeFormatter is an important class in the Java 8 Date-Time API (java.time package), used for formatting and parsing date-time objects. This class provides powerful functionality for displaying and converting date-time values.\\n\\nIn simple terms, DateTimeFormatter has two main purposes:\\n\\n* **Formatting**: Converting date-time objects (such as LocalDate, LocalDateTime) into strings of specific formats\\n* **Parsing**: Converting strings that match a format into date-time objects\\n\\n* * *\\n\\n## Why DateTimeFormatter is Needed\\n\\nBefore Java 8, we used SimpleDateFormat to handle date formatting and parsing, but it had several drawbacks:\\n\\n* Not thread-safe\\n* Outdated design\\n* Unfriendly error handling\\n\\nDateTimeFormatter solves these problems:\\n\\n* **Thread-safe**: Instances can be safely shared in multi-threaded environments\\n* **Immutable**: Cannot be modified once created\\n* **Richer predefined formats**: Provides a large number of built-in formats\\n* **Better error handling**: Provides more detailed error information when parsing fails\\n\\n* * *\\n\\n## Basic Usage\\n\\n### Predefined Formatters\\n\\nDateTimeFormatter provides many predefined formatting constants:\\n\\n## Example\\n\\nLocalDateTime now = LocalDateTime.now();\\n\\n// Using predefined formatters\\n\\nSystem.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE));// 2023-11-15\\n\\nSystem.out.println(now.format(DateTimeFormatter.ISO_LOCAL_TIME));// 14:30:45.123\\n\\nSystem.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));// 2023-11-15T14:30:45.123\\n\\n### Custom Patterns\\n\\nYou can create custom formatters using pattern strings:\\n\\n## Example\\n\\nDateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");\\n\\nString formattedDateTime = now.format(formatter);// Formatting\\n\\nSystem.out.println(formattedDateTime);// 2023-11-15 14:30:45\\n\\n// Parsing\\n\\n LocalDateTime parsedDateTime = LocalDateTime.parse("2023-11-15 14:30:45", formatter);\\n\\n// Common Chinese date format\\n\\n DateTimeFormatter chineseFormatter = DateTimeFormatter.ofPattern("yyyyYearMM Monthdd Day");\\n\\nString formattedDate = today.format(chineseFormatter);\\n\\nSystem.out.println("Chinese format date: "+ formattedDate);\\n\\n// Parsing Chinese date string\\n\\nString chineseDateStr ="2023Year05 Month20 Day";\\n\\n LocalDate parsedDate = LocalDate.parse(chineseDateStr, chineseFormatter);\\n\\nSystem.out.println("Parsed date: "+ parsedDate);\\n\\n// Other common formats\\n\\n DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");\\n\\n DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy");\\n\\n DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyyYearMM Monthdd Day EEEE", Locale.CHINA);\\n\\nSystem.out.println("Format 1: "+ today.format(formatter1));\\n\\nSystem.out.println("Format 2: "+ today.format(formatter2));\\n\\nSystem.out.println("Format 3 (with weekday): "+ today.format(formatter3));\\n\\n### Localized Formats\\n\\nFormatters can be created for different locales:\\n\\n## Example\\n\\nDateTimeFormatter frenchFormatter =\\n\\n DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)\\n\\n.withLocale(Locale.FRENCH);\\n\\nString frenchDate = now.format(frenchFormatter);\\n\\nSystem.out.println(frenchDate);// 15 novembre 2023\\n\\n* * *\\n\\n## Pattern Letter Reference\\n\\nWhen creating custom formats, specific pattern letters are used:\\n\\n| Letter | Meaning | Example |\\n| --- | --- | --- |\\n| y | Year | yyyy β 2023 |\\n| M | Month | MM β 11 |\\n| d | Day | dd β 15 |\\n| H | Hour (0-23) | HH β 14 |\\n| h | Hour (1-12) | hh β 02 |\\n| m | Minute | mm β 30 |\\n| s | Second | ss β 45 |\\n| S | Millisecond | SSS β 123 |\\n| E | Day of week | E β Wed |\\n| a | AM/PM | a β PM |\\n\\n* * *\\n\\n## Best Practices\\n\\n### Reuse Formatters\\n\\nSince DateTimeFormatter is thread-safe, it is recommended to define commonly used formatters as constants:\\n\\n## Example\\n\\npublic class DateUtils {\\n\\npublic static final DateTimeFormatter STANDARD_FORMATTER =\\n\\n DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");\\n\\n// Usage\\n\\nString formatted = LocalDateTime.now().format(STANDARD_FORMATTER);\\n\\n}\\n\\nComprehensive Example:\\n\\n## Example\\n\\nimport java.time.LocalDate;\\n\\nimport java.time.LocalTime;\\n\\nimport java.time.LocalDateTime;\\n\\nimport java.time.format.DateTimeFormatter;\\n\\nimport java.util.Locale;\\n\\npublic class TutorialTest {\\n\\npublic static void main(String[] args){\\n\\n// 1. LocalDate example\\n\\n LocalDate currentDate = LocalDate.now();\\n\\nSystem.out.println("Current date: "+ currentDate);\\n\\nLocalDate specificDate = LocalDate.of(2023, 5, 20);\\n\\nSystem.out.println("Specific date: "+ specificDate);\\n\\n// 2. LocalTime example\\n\\n LocalTime currentTime = LocalTime.now();\\n\\nSystem.out.println("Current time: "+ currentTime);\\n\\nLocalTime specificTime = LocalTime.of(14, 30, 15);\\n\\nSystem.out.println("Specific time: "+ specificTime);\\n\\n// 3. LocalDateTime example\\n\\n LocalDateTime currentDateTime = LocalDateTime.now();\\n\\nSystem.out.println("Current date-time: "+ currentDateTime);\\n\\nLocalDateTime specificDateTime = LocalDateTime.of(2023, 5, 20, 14, 30, 15);\\n\\nSystem.out.println("Specific date-time: "+ specificDateTime);\\n\\n// 4. DateTimeFormatter example\\n\\n// Basic formatting\\n\\n DateTimeFormatter basicFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");\\n\\nSystem.out.println("Basic format: "+ currentDate.format(basicFormatter));\\n\\n// Chinese formatting\\n\\n DateTimeFormatter chineseFormatter = DateTimeFormatter.ofPattern("yyyyYearMM Monthdd Day", Locale.CHINA);\\n\\nSystem.out.println("Chinese date: "+ currentDate.format(chineseFormatter));\\n\\n// Chinese formatting with weekday\\n\\n DateTimeFormatter weekFormatter = DateTimeFormatter.ofPattern("yyyyYearMM Monthdd Day EEEE", Locale.CHINA);\\n\\nSystem.out.println("Chinese date with weekday: "+ currentDate.format(weekFormatter));\\n\\n// Time formatting\\n\\n DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HHHourmm Minutess Second");\\n\\nSystem.out.println("Chinese time: "+ currentTime.format(timeFormatter));\\n\\n// Date-time formatting\\n\\n DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");\\n\\nSystem.out.println("Standard date-time: "+ currentDateTime.format(dateTimeFormatter));\\n\\n// Parsing date\\n\\nString dateStr ="2023-05-20";\\n\\n LocalDate parsedDate = LocalDate.parse(dateStr, basicFormatter);\\n\\nSystem.out.println("Parsed date: "+ parsedDate);\\n\\n// Parsing Chinese date\\n\\nString chineseDateStr ="2023Year05 Month20 Day";\\n\\n LocalDate parsedChineseDate = LocalDate.parse(chineseDateStr, chineseFormatter);\\n\\nSystem.out.println("Parsed Chinese date: "+ parsedChineseDate);\\n\\n}\\n\\n}\\n\\nOutput:\\n\\nCurrent date.: 2025-05-01Specific Date: 2023-05-20Current time: 10:46:38.591747Specific time: 14:30:15Current date.Time: 2025-05-01T10:46:38.592002Specific DateTime: 2023-05-20T14:30:15Basic Formatting: 2025-05-01Chinese Date: 2025Year05Month01DayChinese Date with Day of Week: 2025Year05Month01Day Thursday Chinese Time: 10Hour46Minute38Seconds Standard Day Date Time: 2025-05-01 10:46:38Parsed Date: 2023-05-20Parsed Chinese Date: 2023-05-20\\n### Handling Parse Exceptions\\n\\nWhen parsing strings, possible exceptions should be handled:\\n\\n## Example\\n\\ntry{\\n\\n LocalDateTime dateTime = LocalDateTime.parse("2023-11-15", formatter);\\n\\n}catch(DateTimeParseException e){\\n\\nSystem.err.println("Unable to parse date-time: "+ e.getMessage());\\n\\n}\\n\\n### Considering Time Zones\\n\\nIf time zone handling is needed, (#) and specific time zone patterns can be used:\\n\\n## Example\\n\\nDateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");\\n\\n ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));\\n\\nSystem.out.println(zonedDateTime.format(formatter));// 2023-11-15 14:30:45 CST\\n\\n* * *\\n\\n## Summary\\n\\nDateTimeFormatter is a powerful tool in the Java 8 Date-Time API for handling formatting and parsing. Compared to the old SimpleDateFormat, it is safer, more flexible, and easier to use. Mastering DateTimeFormatter allows you to handle various date-time format requirements more efficiently.\\n\\nRemember the key points:\\n\\n* Use predefined formatters for common formats\\n* Use ofPattern() to create custom formats\\n* Consider localization and time zone requirements\\n* Reuse thread-safe formatter instances\\n* Handle parse exceptions properly
YouTip