-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeDemo.java
More file actions
58 lines (47 loc) · 1.89 KB
/
DateTimeDemo.java
File metadata and controls
58 lines (47 loc) · 1.89 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
package DateTime;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.stream.Stream;
public class DateTimeDemo {
private static Long fact(Long n) {
long fact = 1L;
for (long i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
System.out.println(localDate.getYear());
System.out.println(localDate.getMonth());
LocalTime nowTime = LocalTime.now();
System.out.println(nowTime);
System.out.println(nowTime.getHour());
LocalDateTime nowDateTime = LocalDateTime.now();
System.out.println(nowDateTime);
System.out.println(nowDateTime.minusMonths(100));
ZonedDateTime nowZonedDateTime = ZonedDateTime.now();
System.out.println(nowZonedDateTime);
ZoneId zone = nowZonedDateTime.getZone();
System.out.println(zone);
ZoneId.getAvailableZoneIds().forEach(System.out::println);
Instant start = Instant.now();
Stream.iterate(1L, x -> x + 1).limit(20000).map(DateTimeDemo::fact).toList();
Instant end = Instant.now();
Duration duration = Duration.between(start, end);
System.out.println(duration);
Duration d = Duration.of(1, ChronoUnit.MINUTES);
System.out.println(d);
LocalDate now = LocalDate.now();
LocalDate then = LocalDate.of(1983, 2, 2);
Period period = Period.between(now, then);
System.out.println(period);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MMM/yyyy");
System.out.println(localDate.format(formatter));
String date = "25/Jan/1983";
LocalDate date1 = LocalDate.parse(date, formatter);
System.out.println(date1);
}
}