-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsingInstant.java
More file actions
47 lines (37 loc) · 1.56 KB
/
Copy pathUsingInstant.java
File metadata and controls
47 lines (37 loc) · 1.56 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
package dateAndTime;
import lombok.extern.slf4j.Slf4j;
import org.testng.annotations.Test;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@Slf4j
public class UsingInstant {
@Test
public void testTimeInstant(){
Instant instant = Instant.now(); // UTC time
System.out.println("Simple instant:"+instant);
}
@Test
public void testInstantUsingTimeZone(){
Instant instant = Instant.now(); // UTC time
System.out.println("Simple instant:"+instant);
ZoneId utc = ZoneId.of("UTC");
ZoneId indiaZoneId = ZoneId.of("Asia/Kolkata");
ZoneId nyZoneId = ZoneId.of("America/New_York");
ZonedDateTime indiaTime = instant.atZone(indiaZoneId);
ZonedDateTime nyTime = instant.atZone(nyZoneId);
System.out.println("indiaTime:"+indiaTime);
System.out.println("nyTime:"+nyTime);
}
@Test
public void testInstantUsingTimeZoneAndDateFormatter() {
// best practise is to always follow this flow: Instant →ZonedDateTime →DateTimeFormatter
Instant instant = Instant.now();
System.out.println("Simple instant:"+instant);
var standardDateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
ZoneId indiaZone = ZoneId.of("Asia/Kolkata");
String standardTimeFormatInIndiaTimeZone = standardDateTimeFormat.withZone(indiaZone).format(instant);
System.out.println("standardTimeFormatInIndiaTimeZone:"+ standardTimeFormatInIndiaTimeZone);
}
}