Skip to content

Commit 8c24e35

Browse files
committed
Some basic date time api usage (introduced in Java 8)
1 parent e4db572 commit 8c24e35

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.rampatra.java8;
2+
3+
import java.time.Instant;
4+
import java.time.LocalDateTime;
5+
import java.time.ZoneId;
6+
import java.util.Date;
7+
8+
/**
9+
* @author rampatra
10+
* @since 2019-05-15
11+
*/
12+
public class DateTime {
13+
14+
private static long getCurrentTimestampFromInstant() {
15+
return Instant.now().toEpochMilli();
16+
}
17+
18+
private static String addTwoDays() {
19+
LocalDateTime now = LocalDateTime.ofInstant(Instant.now(), ZoneId.of("UTC"));
20+
LocalDateTime afterTwoDays = now.plusDays(2);
21+
return afterTwoDays.getDayOfMonth() + "-" + afterTwoDays.getMonthValue() + "-" + afterTwoDays.getYear();
22+
}
23+
24+
public static void main(String[] args) {
25+
System.out.println("Timestamp from Instant: " + getCurrentTimestampFromInstant() +
26+
"\nTimestamp from Legacy Date: " + new Date().getTime());
27+
System.out.println("Add Two days: " + addTwoDays());
28+
}
29+
}

0 commit comments

Comments
 (0)