Skip to content

Commit 9f52200

Browse files
authored
Merge pull request #82 from beneite/feature
adding Instant for date and time
2 parents 978dcfe + a6363de commit 9f52200

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@
150150
<version>1.0.2</version>
151151
</dependency>
152152

153+
<dependency>
154+
<groupId>ch.qos.logback</groupId>
155+
<artifactId>logback-classic</artifactId>
156+
<version>1.4.14</version>
157+
<scope>test</scope>
158+
</dependency>
159+
153160

154161
</dependencies>
155162
<build>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dateAndTime;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.testng.annotations.Test;
5+
6+
import java.time.Instant;
7+
import java.time.ZoneId;
8+
import java.time.ZonedDateTime;
9+
import java.time.format.DateTimeFormatter;
10+
11+
@Slf4j
12+
public class UsingInstant {
13+
14+
@Test
15+
public void testTimeInstant(){
16+
Instant instant = Instant.now(); // UTC time
17+
System.out.println("Simple instant:"+instant);
18+
}
19+
20+
@Test
21+
public void testInstantUsingTimeZone(){
22+
Instant instant = Instant.now(); // UTC time
23+
System.out.println("Simple instant:"+instant);
24+
25+
ZoneId utc = ZoneId.of("UTC");
26+
ZoneId indiaZoneId = ZoneId.of("Asia/Kolkata");
27+
ZoneId nyZoneId = ZoneId.of("America/New_York");
28+
29+
ZonedDateTime indiaTime = instant.atZone(indiaZoneId);
30+
ZonedDateTime nyTime = instant.atZone(nyZoneId);
31+
32+
System.out.println("indiaTime:"+indiaTime);
33+
System.out.println("nyTime:"+nyTime);
34+
}
35+
36+
@Test
37+
public void testInstantUsingTimeZoneAndDateFormatter() {
38+
// best practise is to always follow this flow: Instant →ZonedDateTime →DateTimeFormatter
39+
Instant instant = Instant.now();
40+
System.out.println("Simple instant:"+instant);
41+
42+
var standardDateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
43+
ZoneId indiaZone = ZoneId.of("Asia/Kolkata");
44+
String standardTimeFormatInIndiaTimeZone = standardDateTimeFormat.withZone(indiaZone).format(instant);
45+
System.out.println("standardTimeFormatInIndiaTimeZone:"+ standardTimeFormatInIndiaTimeZone);
46+
}
47+
}

0 commit comments

Comments
 (0)