-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalDateTimeExamples.java
More file actions
64 lines (58 loc) · 2.44 KB
/
LocalDateTimeExamples.java
File metadata and controls
64 lines (58 loc) · 2.44 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
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
public class LocalDateTimeExamples {
public static void main(String[] args) {
System.out.println("Local Date Time Formatting Example");
localDateTimeFormattingExample();
System.out.println("Local Date Time Now Example");
localDateTimeNowExample();
System.out.println("Local Date Time Get Example");
localDateTimeGetExample();
System.out.println("Local Date Time Minus Days Example");
localDateTimeMinusDaysExample();
System.out.println("Local Date Time Plus Days Example");
localDateTimePlusDays();
}
public static void localDateTimeFormattingExample() {
LocalDateTime now = LocalDateTime.now();
System.out.println("Before Formatting: " + now);
DateTimeFormatter format = DateTimeFormatter
.ofPattern("dd-MM-yyyy HH:mm:ss");
String formatDateTime = now.format(format);
System.out.println("After Formatting: " + formatDateTime);
}
public static void localDateTimeNowExample() {
LocalDateTime datetime1 = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter
.ofPattern("dd-MM-yyyy HH:mm:ss");
String formatDateTime = datetime1.format(format);
System.out.println(formatDateTime);
}
public static void localDateTimeGetExample() {
LocalDateTime a = LocalDateTime.of(2017, 2, 13, 15, 56);
System.out.println(a.get(ChronoField.DAY_OF_WEEK));
System.out.println(a.get(ChronoField.DAY_OF_YEAR));
System.out.println(a.get(ChronoField.DAY_OF_MONTH));
System.out.println(a.get(ChronoField.HOUR_OF_DAY));
System.out.println(a.get(ChronoField.MINUTE_OF_DAY));
}
public static void localDateTimeMinusDaysExample() {
LocalDateTime datetime1 = LocalDateTime.of(2017, 1, 14, 10, 34);
LocalDateTime datetime2 = datetime1.minusDays(100);
System.out.println("Before Formatting: " + datetime2);
DateTimeFormatter format = DateTimeFormatter
.ofPattern("dd-MM-yyyy HH:mm");
String formatDateTime = datetime2.format(format);
System.out.println("After Formatting: " + formatDateTime);
}
public static void localDateTimePlusDays() {
LocalDateTime datetime1 = LocalDateTime.of(2017, 1, 14, 10, 34);
LocalDateTime datetime2 = datetime1.plusDays(120);
System.out.println("Before Formatting: " + datetime2);
DateTimeFormatter format = DateTimeFormatter
.ofPattern("dd-MM-yyyy HH:mm");
String formatDateTime = datetime2.format(format);
System.out.println("After Formatting: " + formatDateTime);
}
}