-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeFormatterDemo.java
More file actions
120 lines (107 loc) · 4.73 KB
/
Copy pathDateTimeFormatterDemo.java
File metadata and controls
120 lines (107 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package Phase8_PracticalAPIs.DateAndTime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.time.format.SignStyle;
import java.time.temporal.ChronoField;
import java.util.Locale;
/**
* DateTimeFormatter
* -----------------
* Parse strings into date/time values and format them back. THREAD-SAFE
* and IMMUTABLE — share instances freely. Replaces the legacy (and
* broken) SimpleDateFormat.
* <p>
*
* Three ways to get a formatter
* -----------------------------
* 1. CONSTANTS — DateTimeFormatter.ISO_LOCAL_DATE etc. for ISO-8601.
* 2. PATTERN — DateTimeFormatter.ofPattern("dd/MM/yyyy")
* 3. BUILDER — new DateTimeFormatterBuilder()... for full control.
* <p>
*
* Common pattern letters
* ----------------------
* y / u year (u allows negative years)
* M month-of-year (MM=07, MMM=Jul, MMMM=July)
* d day-of-month
* E day-of-week (E=Mon, EEEE=Monday)
* H / h hour 0-23 / 1-12
* m minute
* s second
* S / n fraction-of-second / nano
* a AM/PM
* z zone abbreviation
* V zone id
* x / X offset (+0530 / +05:30)
* <p>
*
* Locale matters
* --------------
* Month and day names depend on locale.
* ofPattern("MMM d", Locale.US) -> Jul 4
* ofPattern("MMM d", Locale.GERMAN) -> Jul 4
* <p>
*
* Tips
* ----
* - Always pin the Locale for output that crosses borders.
* - For text that humans see, prefer FormatStyle (LONG/MEDIUM/SHORT).
* - For machine interchange, prefer ISO constants.
*/
public class DateTimeFormatterDemo {
public static void main(String[] args) {
section("1) ISO constants — machine-friendly");
LocalDate today = LocalDate.now();
LocalDateTime now = LocalDateTime.now();
System.out.println(today.format(DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
System.out.println(ZonedDateTime.now().format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
System.out.println(today.format(DateTimeFormatter.BASIC_ISO_DATE)); // YYYYMMDD
section("2) ofPattern — custom");
DateTimeFormatter eu = DateTimeFormatter.ofPattern("dd/MM/yyyy");
DateTimeFormatter readable = DateTimeFormatter.ofPattern("EEE, MMM d, yyyy");
System.out.println(today.format(eu));
System.out.println(today.format(readable));
section("3) Locale matters");
DateTimeFormatter en = DateTimeFormatter.ofPattern("EEEE d MMMM", Locale.ENGLISH);
DateTimeFormatter fr = DateTimeFormatter.ofPattern("EEEE d MMMM", Locale.FRENCH);
DateTimeFormatter hi = DateTimeFormatter.ofPattern("EEEE d MMMM", Locale.forLanguageTag("hi"));
System.out.println("en: " + today.format(en));
System.out.println("fr: " + today.format(fr));
System.out.println("hi: " + today.format(hi));
section("4) Localized FormatStyle");
DateTimeFormatter shortFmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(Locale.US);
DateTimeFormatter longFmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(Locale.US);
System.out.println("SHORT US = " + today.format(shortFmt));
System.out.println("LONG US = " + today.format(longFmt));
section("5) parse() — strings to date types");
LocalDate parsedIso = LocalDate.parse("2026-05-20");
LocalDate parsedEu = LocalDate.parse("20/05/2026", eu);
System.out.println("parsedIso = " + parsedIso);
System.out.println("parsedEu = " + parsedEu);
section("6) Strict parsing via builder");
// Year must be exactly 4 digits, no surprise width.
DateTimeFormatter strict = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 4, 4, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.toFormatter();
System.out.println("strict = " + LocalDate.parse("2026-05-20", strict));
section("7) ZonedDateTime with offset / zone in the pattern");
ZonedDateTime z = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
DateTimeFormatter zoned = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss zzz XXX");
System.out.println(z.format(zoned));
section("done");
}
private static void section(String title) {
System.out.println();
System.out.println("=== " + title + " ===");
}
}