Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
96 lines (77 loc) · 4.54 KB
/
Copy pathMain.java
File metadata and controls
96 lines (77 loc) · 4.54 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
package modern.challenge;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
Duration t = Duration.ofSeconds(65);
System.out.println("Seconds part: " + t.toSecondsPart());
Duration fromHours = Duration.ofHours(10);
System.out.println("\nDuration from hours: " + fromHours);
Duration fromMinutes = Duration.of(3, ChronoUnit.MINUTES);
System.out.println("Duration from minutes: " + fromMinutes);
LocalDateTime localDateTime = LocalDateTime.of(2018, 3, 12, 4, 14, 20, 670);
Duration fromLocalDateTime = Duration.ofMinutes(localDateTime.getMinute());
System.out.println("Duration from minutes of a LocalDateTime: " + fromLocalDateTime);
LocalTime localTime = LocalTime.of(4, 14, 20, 670);
Duration fromLocalTime = Duration.ofNanos(localTime.getNano());
System.out.println("Duration from nanos of a LocalTime: " + fromLocalTime);
Duration durationFromString = Duration.parse("P2DT3H4M");
System.out.println("Duration from String: " + durationFromString);
Instant startInstant = Instant.parse("2015-11-03T12:11:30.00Z");
Instant endInstant = Instant.parse("2016-12-06T15:17:10.00Z");
Duration durationBetweenInstant = Duration.between(startInstant, endInstant);
System.out.println("\nDuration between two Instant: " + durationBetweenInstant);
System.out.println("Between " + startInstant + " and "
+ endInstant + " there are " + durationBetweenInstant.getSeconds() + " second(s)");
System.out.println("Expressed in days and hours: "
+ durationBetweenInstant.toDays() + "d:" + durationBetweenInstant.toHoursPart() + "h");
System.out.println("Expressed as d:h:m:s:n: " + durationToDHMSN(durationBetweenInstant));
LocalDateTime startLocalDateTime = LocalDateTime.of(2018, 3, 12, 4, 14, 20, 670);
LocalDateTime endLocalDateTime = LocalDateTime.of(2019, 7, 20, 6, 10, 10, 720);
Duration durationBetweenLDT = Duration.between(startLocalDateTime, endLocalDateTime);
System.out.println("\nDuration between two LocalDateTime: " + durationBetweenLDT);
System.out.println("Between " + startLocalDateTime + " and "
+ endLocalDateTime + " there are " + durationBetweenLDT.getSeconds() + " second(s)");
System.out.println("Expressed in days and hours: "
+ durationBetweenLDT.toDays() + "d:" + durationBetweenLDT.toHoursPart() + "h");
System.out.println("Expressed as d:h:m:s:n: " + durationToDHMSN(durationBetweenLDT));
LocalTime startLocalTime = LocalTime.of(4, 14, 20, 670);
LocalTime endLocalTime = LocalTime.of(6, 10, 10, 720);
Duration durationBetweenLT = Duration.between(startLocalTime, endLocalTime);
System.out.println("\nDuration between two LocalTime: " + durationBetweenLT);
System.out.println("Between " + startLocalTime + " and "
+ endLocalTime + " there are " + durationBetweenLT.getSeconds() + " second(s)");
System.out.println("Expressed in hours and minutes: "
+ durationBetweenLT.toMinutes() + "m:" + durationBetweenLT.toSecondsPart() + "s");
System.out.println("Expressed as d:h:m:s:n: " + durationToDHMSN(durationBetweenLT));
System.out.println(startLocalTime + " is after "
+ endLocalTime + " ? " + durationBetweenLT.isNegative());
Duration durationBetweenPlus5Hours = durationBetweenLT.plusHours(5);
System.out.println("\nDuration between plus 5 hours: " + durationBetweenPlus5Hours);
Duration d1 = Duration.ofMinutes(20);
Duration d2 = Duration.ofHours(2);
Duration d1d2 = d1.plus(d2);
System.out.println(d1 + "+" + d2 + "=" + d1d2);
}
private static String durationToDHMSN(Duration duration) {
if (duration == null) {
// or throw IllegalArgumentException
return "";
}
StringBuilder sb = new StringBuilder();
sb.append(duration.toDays())
.append("d:")
.append(duration.toHoursPart())
.append("h:")
.append(duration.toMinutesPart())
.append("m:")
.append(duration.toSecondsPart())
.append("s:")
.append(duration.toNanosPart())
.append("n");
return sb.toString();
}
}