-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewDateTimeAPI.java
More file actions
55 lines (42 loc) · 1.57 KB
/
Copy pathNewDateTimeAPI.java
File metadata and controls
55 lines (42 loc) · 1.57 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
package java8_features;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.Month;
/**
* With Java 8, a new Date-Time API is introduced
* to cover the following drawbacks of old date-time API:
* Not thread safe
* Poor design
* Difficult time zone handling
*
* Created by nector on 28/02/17.
*/
public class NewDateTimeAPI {
public static void main(String args[]) {
NewDateTimeAPI java8tester = new NewDateTimeAPI();
java8tester.testLocalDateTime();
}
public void testLocalDateTime() {
// Get the current date and time
LocalDateTime currentTime = LocalDateTime.now();
System.out.println("Current DateTime: " + currentTime);
LocalDate date1 = currentTime.toLocalDate();
System.out.println("date 1: " + date1);
Month month = currentTime.getMonth();
int day = currentTime.getDayOfMonth();
int seconds = currentTime.getSecond();
System.out.println("Month: " + month + "; day: " + day + "; seconds: " + seconds);
LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
System.out.println("date 2: " + date2);
//12 december 2014
LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12);
System.out.println("date 3: " + date3);
//22 hour 15 minutes
LocalTime date4 = LocalTime.of(22, 15);
System.out.println("date 4: " + date4);
//parse a string
LocalTime date5 = LocalTime.parse("20:15:30");
System.out.println("date 5: " + date5);
}
}