forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaInstantEx.java
More file actions
26 lines (18 loc) · 760 Bytes
/
JavaInstantEx.java
File metadata and controls
26 lines (18 loc) · 760 Bytes
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
package com.zetcode;
import java.time.Instant;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
public class JavaInstantEx {
public static void main(String[] args) {
var timestamp = Instant.now();
System.out.println("The current timestamp: " + timestamp);
System.out.printf("Unix time: %d%n", timestamp.toEpochMilli());
//Now minus five days
var minusFive = timestamp.minus(5, ChronoUnit.DAYS);
System.out.println("Now minus five days:" + minusFive);
var atZone = timestamp.atZone(ZoneId.of("GMT"));
System.out.printf("GMT: %s%n", atZone);
var yesterday = Instant.now().minus(24, ChronoUnit.HOURS);
System.out.println("Yesterday: " + yesterday);
}
}