-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetime.java
467 lines (463 loc) · 25.7 KB
/
datetime.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.InputMismatchException;
import java.util.Scanner;
public class datetime {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
int year, month, day, hour, minute, second, millisecond, microsecond, nanosecond; // initialize date and
// time variables
DateTimeFormatter era1 = DateTimeFormatter.ofPattern("GGG"); // Format the date and time values with a
// specified pattern for all supported formats
DateTimeFormatter era2 = DateTimeFormatter.ofPattern("GGGG");
DateTimeFormatter era3 = DateTimeFormatter.ofPattern("GGGGG");
DateTimeFormatter year1 = DateTimeFormatter.ofPattern("uu");
DateTimeFormatter year2 = DateTimeFormatter.ofPattern("uuuu");
DateTimeFormatter yearEra = DateTimeFormatter.ofPattern("yy");
DateTimeFormatter yearEra2 = DateTimeFormatter.ofPattern("yyyy");
DateTimeFormatter dayOfYear = DateTimeFormatter.ofPattern("D");
DateTimeFormatter monthOfYear = DateTimeFormatter.ofPattern("M");
DateTimeFormatter monthOfYear2 = DateTimeFormatter.ofPattern("MMM");
DateTimeFormatter monthOfYear3 = DateTimeFormatter.ofPattern("MMMM");
DateTimeFormatter monthOfYear4 = DateTimeFormatter.ofPattern("MMMMM");
DateTimeFormatter dayOfMonth = DateTimeFormatter.ofPattern("d");
DateTimeFormatter quarter = DateTimeFormatter.ofPattern("q");
DateTimeFormatter quarter2 = DateTimeFormatter.ofPattern("qqq");
DateTimeFormatter quarter3 = DateTimeFormatter.ofPattern("qqqq");
DateTimeFormatter weekBasedYear = DateTimeFormatter.ofPattern("YY");
DateTimeFormatter weekBasedYear2 = DateTimeFormatter.ofPattern("YYYY");
DateTimeFormatter weekOfYear = DateTimeFormatter.ofPattern("w");
DateTimeFormatter weekOfMonth = DateTimeFormatter.ofPattern("W");
DateTimeFormatter dayOfWeek = DateTimeFormatter.ofPattern("E");
DateTimeFormatter dayOfWeek2 = DateTimeFormatter.ofPattern("EEEE");
DateTimeFormatter dayOfWeek3 = DateTimeFormatter.ofPattern("EEEEE");
DateTimeFormatter localDayOfWeek = DateTimeFormatter.ofPattern("e");
DateTimeFormatter localDayOfWeek2 = DateTimeFormatter.ofPattern("eee");
DateTimeFormatter localDayOfWeek3 = DateTimeFormatter.ofPattern("eeee");
DateTimeFormatter localDayOfWeek4 = DateTimeFormatter.ofPattern("eeeee");
DateTimeFormatter localWeekOfMonth = DateTimeFormatter.ofPattern("F");
DateTimeFormatter amPm = DateTimeFormatter.ofPattern("a");
DateTimeFormatter hour12 = DateTimeFormatter.ofPattern("h");
DateTimeFormatter hour0To11 = DateTimeFormatter.ofPattern("K");
DateTimeFormatter hour24 = DateTimeFormatter.ofPattern("k");
DateTimeFormatter hourOfDay = DateTimeFormatter.ofPattern("H");
DateTimeFormatter minute1 = DateTimeFormatter.ofPattern("m");
DateTimeFormatter second1 = DateTimeFormatter.ofPattern("s");
DateTimeFormatter fractionOfSecond = DateTimeFormatter.ofPattern(
"SSSSSSSSS");
DateTimeFormatter millisecondOfDay = DateTimeFormatter.ofPattern("A");
DateTimeFormatter nanosecondOfSecond = DateTimeFormatter.ofPattern("n");
DateTimeFormatter nanosecondOfDay = DateTimeFormatter.ofPattern("N");
DateTimeFormatter timezoneId = DateTimeFormatter.ofPattern("VV");
DateTimeFormatter timezoneName = DateTimeFormatter.ofPattern("z");
DateTimeFormatter timezoneName2 = DateTimeFormatter.ofPattern("zzzz");
DateTimeFormatter timezoneOffset = DateTimeFormatter.ofPattern("OOOO");
DateTimeFormatter timezoneOffsetZ = DateTimeFormatter.ofPattern("XXXXX");
DateTimeFormatter timezoneOffsetX = DateTimeFormatter.ofPattern("xxxxx");
DateTimeFormatter timezoneOffset2 = DateTimeFormatter.ofPattern("ZZZZZ");
DateTimeFormatter timezoneOffsetZ1 = DateTimeFormatter.ofPattern("ZZZZ");
try { // make sure date and time input is a vaild integer
System.out.print("Year: ");
year = sc.nextInt();
System.out.print("Month: ");
month = sc.nextInt(); // 1 is January
System.out.print("Day: ");
day = sc.nextInt();
System.out.print("Hour: ");
hour = sc.nextInt();
System.out.print("Minute: ");
minute = sc.nextInt();
System.out.print("Second: ");
second = sc.nextInt();
System.out.print("Millisecond: ");
millisecond = sc.nextInt();
System.out.print("Microsecond: ");
microsecond = sc.nextInt();
System.out.print("Nanosecond: ");
nanosecond = sc.nextInt();
LocalDateTime enteredDateAndTime = LocalDateTime
.of(year, month, day, hour, minute, second)
.withNano(millisecond * 1000000 + microsecond * 1000 + nanosecond); // Convert datetime of
// GregorianCalendar object
// to LocalDateTime object
// named enteredDateAndTime
// without a time zone and
// set nanoseconds to
// 1,000,000 (1 million) *
// milliseconds + 1,000 (1
// thousand) * microseconds
// + nanoseconds
ZonedDateTime enteredDateAndTimeTimezone = ZonedDateTime.of(
enteredDateAndTime,
ZoneId.systemDefault()); // Get time zone of entered date and time values
// Print the entered date and time values in all supported formats
System.out.println("\nEntered Date and Time Values:\n");
System.out.println(
"Era: " +
era1.format(enteredDateAndTime) +
" (" +
era2.format(enteredDateAndTime) +
")" +
" (" +
era3.format(enteredDateAndTime) +
")"); // Print the date and time values with formatted date and time pattern
System.out.println(
"Year: " +
year1.format(enteredDateAndTime) +
" (" +
year2.format(enteredDateAndTime) +
")");
System.out.println(
"Era Year: " +
yearEra.format(enteredDateAndTime) +
" (" +
yearEra2.format(enteredDateAndTime) +
")");
System.out.println(
"Day of Year: " + dayOfYear.format(enteredDateAndTime));
System.out.println(
"Month of Year: " +
monthOfYear.format(enteredDateAndTime) +
" (" +
monthOfYear2.format(enteredDateAndTime) +
")" +
" (" +
monthOfYear3.format(enteredDateAndTime) +
")" +
" (" +
monthOfYear4.format(enteredDateAndTime) +
")");
System.out.println(
"Day of Month: " + dayOfMonth.format(enteredDateAndTime));
System.out.println(
"Quarter: " +
quarter.format(enteredDateAndTime) +
" (" +
quarter2.format(enteredDateAndTime) +
")" +
" (" +
quarter3.format(enteredDateAndTime) +
")");
System.out.println(
"ISO Week-based Year: " +
weekBasedYear.format(enteredDateAndTime) +
" (" +
weekBasedYear2.format(enteredDateAndTime) +
")");
System.out.println(
"Week of Year: " + weekOfYear.format(enteredDateAndTime));
System.out.println(
"Week of Month: " + weekOfMonth.format(enteredDateAndTime));
System.out.println(
"Day of Week: " +
dayOfWeek.format(enteredDateAndTime) +
" (" +
dayOfWeek2.format(enteredDateAndTime) +
")" +
" (" +
dayOfWeek3.format(enteredDateAndTime) +
")");
System.out.println(
"Local Day of Week: " +
localDayOfWeek.format(enteredDateAndTime) +
" (" +
localDayOfWeek2.format(enteredDateAndTime) +
")" +
" (" +
localDayOfWeek3.format(enteredDateAndTime) +
")" +
" (" +
localDayOfWeek4.format(enteredDateAndTime) +
")");
System.out.println(
"Local Week of Month: " + localWeekOfMonth.format(enteredDateAndTime));
System.out.println("AM/PM: " + amPm.format(enteredDateAndTime));
System.out.println(
"12 Hour Format " + hour12.format(enteredDateAndTime));
System.out.println(
"0-11 Hour Format: " + hour0To11.format(enteredDateAndTime));
System.out.println(
"24 Hour Format: " + hour24.format(enteredDateAndTime));
System.out.println(
"Hour of Day: " + hourOfDay.format(enteredDateAndTime));
System.out.println("Minute: " + minute1.format(enteredDateAndTime));
System.out.println("Second: " + second1.format(enteredDateAndTime));
System.out.println(
"Fraction of Second: " + fractionOfSecond.format(enteredDateAndTime));
System.out.println(
"Millisecond of Day: " + millisecondOfDay.format(enteredDateAndTime));
System.out.println(
"Nanosecond: " + nanosecondOfSecond.format(enteredDateAndTime));
System.out.println(
"Nanosecond of Day: " + nanosecondOfDay.format(enteredDateAndTime));
System.out.println(
"Time Zone ID: " + timezoneId.format(enteredDateAndTimeTimezone));
System.out.println(
"Time Zone: " +
timezoneName2.format(enteredDateAndTimeTimezone) +
" (" +
timezoneName.format(enteredDateAndTimeTimezone) +
")");
System.out.println(
"Localized Time Zone Offset: " +
timezoneOffset.format(enteredDateAndTimeTimezone) +
" (" +
timezoneOffset2.format(enteredDateAndTimeTimezone) +
")");
System.out.println(
"Time Zone Offset: " +
timezoneOffsetZ.format(enteredDateAndTimeTimezone) +
" (" +
timezoneOffsetX.format(enteredDateAndTimeTimezone) +
")" +
" (" +
timezoneOffsetZ1.format(enteredDateAndTimeTimezone) +
")");
System.out.println("\nCurrent Date and Time Values:\n");
} catch (InputMismatchException ex) { // if entered, date and time values are in the wrong format (not a
// valid integer)
System.out.println(
"\nInvalid date and time input.\nPrinting current date and time.\n"); // print the error
}
// Print the current date and time values in all supported formats
ZonedDateTime dateAndTimeNow = ZonedDateTime.now(); // Get the current date and time based on local computer
// date and time with local time zone
System.out.println(
"Current Era: " +
era1.format(dateAndTimeNow) +
" (" +
era2.format(dateAndTimeNow) +
")" +
" (" +
era3.format(dateAndTimeNow) +
")"); // Print the formatted date and time values in current local date and time
System.out.println(
"Current Year: " +
year1.format(dateAndTimeNow) +
" (" +
year2.format(dateAndTimeNow) +
")");
System.out.println(
"Current Era Year: " +
yearEra.format(dateAndTimeNow) +
" (" +
yearEra2.format(dateAndTimeNow) +
")");
System.out.println(
"Current Day of Year: " + dayOfYear.format(dateAndTimeNow));
System.out.println(
"Current Month of Year: " +
monthOfYear.format(dateAndTimeNow) +
" (" +
monthOfYear2.format(dateAndTimeNow) +
")" +
" (" +
monthOfYear3.format(dateAndTimeNow) +
")" +
" (" +
monthOfYear4.format(dateAndTimeNow) +
")");
System.out.println(
"Current Day of Month: " + dayOfMonth.format(dateAndTimeNow));
System.out.println(
"Current Quarter: " +
quarter.format(dateAndTimeNow) +
" (" +
quarter2.format(dateAndTimeNow) +
")" +
" (" +
quarter3.format(dateAndTimeNow) +
")");
System.out.println(
"Current ISO Week-based Year: " +
weekBasedYear.format(dateAndTimeNow) +
" (" +
weekBasedYear2.format(dateAndTimeNow) +
")");
System.out.println(
"Current Week of Year: " + weekOfYear.format(dateAndTimeNow));
System.out.println(
"Current Week of Month: " + weekOfMonth.format(dateAndTimeNow));
System.out.println(
"Current Day of Week: " +
dayOfWeek.format(dateAndTimeNow) +
" (" +
dayOfWeek2.format(dateAndTimeNow) +
")" +
" (" +
dayOfWeek3.format(dateAndTimeNow) +
")");
System.out.println(
"Current Local Day of Week: " +
localDayOfWeek.format(dateAndTimeNow) +
" (" +
localDayOfWeek2.format(dateAndTimeNow) +
")" +
" (" +
localDayOfWeek3.format(dateAndTimeNow) +
")" +
" (" +
localDayOfWeek4.format(dateAndTimeNow) +
")");
System.out.println(
"Current Local Week of Month: " +
localWeekOfMonth.format(dateAndTimeNow));
System.out.println("Current AM/PM: " + amPm.format(dateAndTimeNow));
System.out.println(
"Current 12 Hour Format " + hour12.format(dateAndTimeNow));
System.out.println(
"Current 0-11 Hour Format: " + hour0To11.format(dateAndTimeNow));
System.out.println(
"Current 24 Hour Format: " + hour24.format(dateAndTimeNow));
System.out.println(
"Current Hour of Day: " + hourOfDay.format(dateAndTimeNow));
System.out.println("Current Minute: " + minute1.format(dateAndTimeNow));
System.out.println("Current Second: " + second1.format(dateAndTimeNow));
System.out.println(
"Current Fraction of Second: " + fractionOfSecond.format(dateAndTimeNow));
System.out.println(
"Current Millisecond of Day: " + millisecondOfDay.format(dateAndTimeNow));
System.out.println(
"Current Nanosecond: " + nanosecondOfSecond.format(dateAndTimeNow));
System.out.println(
"Current Nanosecond of Day: " + nanosecondOfDay.format(dateAndTimeNow));
System.out.println(
"Current Time Zone ID: " + timezoneId.format(dateAndTimeNow));
System.out.println(
"Current Time Zone: " +
timezoneName2.format(dateAndTimeNow) +
" (" +
timezoneName.format(dateAndTimeNow) +
")");
System.out.println(
"Current Localized Time Zone Offset: " +
timezoneOffset.format(dateAndTimeNow) +
" (" +
timezoneOffset2.format(dateAndTimeNow) +
")");
System.out.println(
"Current Time Zone Offset: " +
timezoneOffsetZ.format(dateAndTimeNow) +
" (" +
timezoneOffsetX.format(dateAndTimeNow) +
")" +
" (" +
timezoneOffsetZ1.format(dateAndTimeNow) +
")");
Instant currentUtcDateAndTime = Instant.now(); // Get the current UTC date and time using an Instant object
LocalDateTime utcDateAndTimeNow = LocalDateTime.ofInstant(
currentUtcDateAndTime,
ZoneOffset.UTC); // Convert from an Instant object to LocalDateTime object
System.out.println(
"Current UTC Era: " +
era1.format(utcDateAndTimeNow) +
" (" +
era2.format(utcDateAndTimeNow) +
")" +
" (" +
era3.format(utcDateAndTimeNow) +
")"); // Print the formatted date and time values in current local date and time
System.out.println(
"Current UTC Year: " +
year1.format(utcDateAndTimeNow) +
" (" +
year2.format(utcDateAndTimeNow) +
")");
System.out.println(
"Current UTC Era Year: " +
yearEra.format(utcDateAndTimeNow) +
" (" +
yearEra2.format(utcDateAndTimeNow) +
")");
System.out.println(
"Current UTC Day of Year: " + dayOfYear.format(utcDateAndTimeNow));
System.out.println(
"Current UTC Month of Year: " +
monthOfYear.format(utcDateAndTimeNow) +
" (" +
monthOfYear2.format(utcDateAndTimeNow) +
")" +
" (" +
monthOfYear3.format(utcDateAndTimeNow) +
")" +
" (" +
monthOfYear4.format(utcDateAndTimeNow) +
")");
System.out.println(
"Current UTC Day of Month: " + dayOfMonth.format(utcDateAndTimeNow));
System.out.println(
"Current UTC Quarter: " +
quarter.format(utcDateAndTimeNow) +
" (" +
quarter2.format(utcDateAndTimeNow) +
")" +
" (" +
quarter3.format(utcDateAndTimeNow) +
")");
System.out.println(
"Current UTC ISO Week-based Year: " +
weekBasedYear.format(utcDateAndTimeNow) +
" (" +
weekBasedYear2.format(utcDateAndTimeNow) +
")");
System.out.println(
"Current UTC Week of Year: " + weekOfYear.format(utcDateAndTimeNow));
System.out.println(
"Current UTC Week of Month: " + weekOfMonth.format(utcDateAndTimeNow));
System.out.println(
"Current UTC Day of Week: " +
dayOfWeek.format(utcDateAndTimeNow) +
" (" +
dayOfWeek2.format(utcDateAndTimeNow) +
")" +
" (" +
dayOfWeek3.format(utcDateAndTimeNow) +
")");
System.out.println(
"Current UTC Local Day of Week: " +
localDayOfWeek.format(utcDateAndTimeNow) +
" (" +
localDayOfWeek2.format(utcDateAndTimeNow) +
")" +
" (" +
localDayOfWeek3.format(utcDateAndTimeNow) +
")" +
" (" +
localDayOfWeek4.format(utcDateAndTimeNow) +
")");
System.out.println(
"Current UTC Local Week of Month: " +
localWeekOfMonth.format(utcDateAndTimeNow));
System.out.println(
"Current UTC AM/PM: " + amPm.format(utcDateAndTimeNow));
System.out.println(
"Current UTC 12 Hour Format " + hour12.format(utcDateAndTimeNow));
System.out.println(
"Current UTC 0-11 Hour Format: " + hour0To11.format(utcDateAndTimeNow));
System.out.println(
"Current UTC 24 Hour Format: " + hour24.format(utcDateAndTimeNow));
System.out.println(
"Current UTC Hour of Day: " + hourOfDay.format(utcDateAndTimeNow));
System.out.println(
"Current UTC Minute: " + minute1.format(utcDateAndTimeNow));
System.out.println(
"Current UTC Second: " + second1.format(utcDateAndTimeNow));
System.out.println(
"Current UTC Fraction of Second: " +
fractionOfSecond.format(utcDateAndTimeNow));
System.out.println(
"Current UTC Millisecond of Day: " +
millisecondOfDay.format(utcDateAndTimeNow));
System.out.println(
"Current UTC Nanosecond: " +
nanosecondOfSecond.format(utcDateAndTimeNow));
System.out.println(
"Current UTC Nanosecond of Day: " +
nanosecondOfDay.format(utcDateAndTimeNow));
}
}
}