forked from howtoprogram/Java-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava8FormatDate.java
More file actions
154 lines (114 loc) · 4.22 KB
/
Java8FormatDate.java
File metadata and controls
154 lines (114 loc) · 4.22 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import org.junit.Test;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import static java.time.format.DateTimeFormatter.*;
import static org.junit.Assert.assertEquals;
public class Java8FormatDate {
@Test
public void testFormatDate() {
//create a date object by using Calendar
Calendar calendar = new GregorianCalendar(2020, Calendar.AUGUST, 9);
Date date = calendar.getTime();
//create a pattern
String pattern = "dd/MM/yyyy";
SimpleDateFormat dateFormatter = new SimpleDateFormat(pattern);
// format the date
String olympicDate = dateFormatter.format(date);
//Validate
assertEquals("09/08/2020", olympicDate);
}
@Test
public void testFormatDateLongPattern() {
//create a date
int year = 2020;
int month = Calendar.AUGUST;
int day = 9;
int hourOfDay = 23;
int minute = 59;
int second = 59;
Calendar calendar = new GregorianCalendar(year, month, day, hourOfDay,
minute, second);
Date date = calendar.getTime();
//create a long pattern with en_US locale
String pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSS";
SimpleDateFormat dateFormatter =
new SimpleDateFormat(pattern, new Locale("en", "US"));
//format the date
String olympicDate = dateFormatter.format(date);
//validate
assertEquals("Sunday August 2020 23:59:59.000", olympicDate);
}
@Test
public void testFormatLocalDate() {
//create a LocalDate object
LocalDate date = LocalDate.of(2020, Month.AUGUST, 9);
//create a pattern
String pattern = "dd/MM/yyyy";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
// format the date
String olympicDate = formatter.format(date);
//Validate
assertEquals("09/08/2020", olympicDate);
}
@Test
public void testFormatLocalDateUsingPreDefinedFormatter() {
//create a LocalDate object
LocalDate date = LocalDate.of(2020, Month.AUGUST, 9);
// format the date
String olympicDate = ISO_DATE.format(date);
assertEquals("2020-08-09", olympicDate);
//or use as a pattern
olympicDate = date.format(ISO_DATE);
assertEquals("2020-08-09", olympicDate);
}
@Test
public void testFormatLocalDateTime() {
//create a LocalDate object
LocalDateTime dt = LocalDateTime.of(2020, Month.AUGUST, 9, 23, 59, 59);
//create a pattern
String pattern = "yyyy/MM/dd HH:mm:ss";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
// format the date
String olympicDate = formatter.format(dt);
assertEquals("2020/08/09 23:59:59", olympicDate);
}
@Test
public void testFormatLocalDateTimeUsingPreDefinedFormatters() {
//create a LocalDate object
LocalDateTime dt = LocalDateTime.of(2020, Month.AUGUST, 9, 23, 59, 59);
// format the date
String olympicDate = ISO_LOCAL_DATE_TIME.format(dt);
assertEquals("2020-08-09T23:59:59", olympicDate);
// or using as a pattern
olympicDate = dt.format(ISO_LOCAL_DATE_TIME);
assertEquals("2020-08-09T23:59:59", olympicDate);
}
@Test
public void testFormatZonedDateTime() {
//create a LocalDate object
LocalDateTime dt = LocalDateTime.of(2020, Month.AUGUST, 9, 23, 59, 59);
ZonedDateTime japanZonedDT = ZonedDateTime.of(dt, ZoneId.of("Asia/Tokyo"));
// format the date
String pattern = "yyyy/MM/dd HH:mm:ssXXX";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
String olympicDate = formatter.format(japanZonedDT);
assertEquals("2020/08/09 23:59:59+09:00", olympicDate);
}
@Test
public void testFormatZonedDateTimeUsingPreDefinedFormatters() {
//create a LocalDate object
LocalDateTime dt = LocalDateTime.of(2020, Month.AUGUST, 9, 23, 59, 59);
ZonedDateTime japanZonedDT = ZonedDateTime.of(dt, ZoneId.of("Asia/Tokyo"));
// format the date
String olympicDate = ISO_OFFSET_DATE_TIME.format(japanZonedDT);
assertEquals("2020-08-09T23:59:59+09:00", olympicDate);
// or using as a pattern
olympicDate = japanZonedDT.format(ISO_OFFSET_DATE_TIME);
assertEquals("2020-08-09T23:59:59+09:00", olympicDate);
}
}