-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvJava_Ch17_13_FormatDateTime.java
More file actions
59 lines (44 loc) · 2.4 KB
/
AdvJava_Ch17_13_FormatDateTime.java
File metadata and controls
59 lines (44 loc) · 2.4 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
package AdvancedJava;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class AdvJava_Ch17_13_FormatDateTime {
public static void main(String[] args) {
System.out.println();
/* Fomat class within time api is made to create out prescribed Date-Time format.
Syntax to import-
import java.time.format.<Class>;
<Classes>:-
1. DateTimeFormatter
2. DateTimeFormatterBuilder
Exception Class -
DateTimeParseException etc. @ https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/time/format/package-summary.html#class-summary */
// The Default Format is yyyy - mm - dd T hh:mm:ss:ns (ns - nanosecond)
LocalDateTime defaultTime = LocalDateTime.now();
System.out.println("Default Time Format is: " + defaultTime); System.out.println();
/* To print date in our prescribes format 2 steps are needed:
1. setting of format- Using ofPattern() method of DateTimeFormatter
DateTimeFormatter <formatName> = DateTimeFormatter.ofPattern("Specify Pattern");
~ Symbols -
a. d = date
b. M = month
c. y = year
d. h = hour / H = 24 hour
e. m = minutes
f. s = seconds
g. a = AM/PM
h. E = day_of_Week (note multiple - E will give full Day Name , {4 Es} )
more @ https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/time/format/DateTimeFormatter.html#patterns
**** Some PreDefined Formats: https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/time/format/DateTimeFormatter.html#predefined
2. Telling the System to parse the date in Specified Format
String <name> = Date_To_Format.format(<formatName>);
Then Print. */
DateTimeFormatter formattedDate = DateTimeFormatter.ofPattern("|| dd-MM-yyyy - LLL || hh-mm-ss a || EEEE ||");
String myDateTime = defaultTime.format(formattedDate);
System.out.println("Formatted Date and Time: " + myDateTime);
System.out.println();
// Printing date in a predefined Format:
DateTimeFormatter predefinedTime = DateTimeFormatter.ISO_LOCAL_TIME;
String definedTime = defaultTime.format(predefinedTime);
System.out.println("Pre-Defined Time Format: " + definedTime);
}
}