forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
66 lines (53 loc) · 2.96 KB
/
Main.java
File metadata and controls
66 lines (53 loc) · 2.96 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
package modern.challenge;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
// get an Instant
Instant timestamp = Instant.now();
System.out.println("Timestamp: " + timestamp);
// String to Instant
Instant timestampFromString = Instant.parse("2019-02-24T14:31:33.197021300Z");
System.out.println("\nTimestamp from string: " + timestampFromString);
// Plus two hours
Instant twoHourLater = Instant.now().plus(2, ChronoUnit.HOURS);
System.out.println("\nTwo hours later: " + twoHourLater);
// Minus 10 minutes
Instant tenMinutesEarlier = Instant.now().minus(10, ChronoUnit.MINUTES);
System.out.println("Ten minutes earlier: " + tenMinutesEarlier);
// check if one Instant is after another Instant
Instant timestamp1 = Instant.now();
Instant timestamp2 = timestamp1.plusSeconds(10);
boolean isAfter = timestamp1.isAfter(timestamp2);
System.out.println("\n" + timestamp1 + " is " + (isAfter ? "" : "not ") + "after " + timestamp2);
// check if one Instant is before another Instant
boolean isBefore = timestamp1.isBefore(timestamp2);
System.out.println(timestamp1 + " is " + (isBefore ? "" : "not ") + "before " + timestamp2);
// difference between two Instants
long difference = timestamp1.until(timestamp2, ChronoUnit.SECONDS);
System.out.println("Between " + timestamp1 + " and " + timestamp2 + " there are " + difference + " seconds");
// convert Instant to LocalDateTime
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC);
System.out.println("\nInstant to LocaleDateTime: " + ldt);
// convert LocalDateTime to Instant
Instant instantLDT = LocalDateTime.now().toInstant(ZoneOffset.UTC);
System.out.println("LocaleDateTime to Instant: " + instantLDT);
// convert Instant to ZonedDateTime
ZonedDateTime zdt = Instant.now().atZone(ZoneId.of("Europe/Paris"));
System.out.println("Instant to ZonedDateTime: " + zdt + " (offset: " + zdt.getOffset() + ")");
// convert ZonedDateTime to Instant
Instant instantZDT = LocalDateTime.now().atZone(ZoneId.of("Europe/Paris")).toInstant();
System.out.println("ZonedDateTime to Instant: " + instantZDT);
// convert Instant to OffsetDateTime
OffsetDateTime odt = Instant.now().atOffset(ZoneOffset.of("+02:00"));
System.out.println("Instant to OffsetDateTime: " + odt + " (offset: " + odt.getOffset() + ")");
// convert OffsetDateTime to Instnat
Instant instantODT = LocalDateTime.now().atOffset(ZoneOffset.of("+02:00")).toInstant();
System.out.println("OffsetDateTime to Instant: " + instantODT);
}
}