-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava8LocalDateExample.java
More file actions
39 lines (31 loc) · 1.21 KB
/
Copy pathJava8LocalDateExample.java
File metadata and controls
39 lines (31 loc) · 1.21 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
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
/**
* Created by john on 2016/1/16.
*/
public class Java8LocalDateExample {
public static void main(String[] args) {
// Get the current date and time
LocalDateTime currentTime = LocalDateTime.now();
System.out.println("Current DateTime: " + currentTime);
LocalDate date1 = currentTime.toLocalDate();
System.out.println("Local Date: " + date1);
Month month = currentTime.getMonth();
int day = currentTime.getDayOfMonth();
int second = currentTime.getSecond();
System.out.println("Month: "+month+" ,day: "+day+",second: "+second);
LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
System.out.println("Date2: "+date2);
//12 December 2014
LocalDate date3 = LocalDate.of(2014,Month.DECEMBER,12);
System.out.println("Date3: "+date3);
//22 hour 15 minutes
LocalTime date4 = LocalTime.of(22,15);
System.out.println("Date4: "+date4);
//parse a string
LocalTime date5 = LocalTime.parse("20:15:30");
System.out.println("Date4: "+date5);
}
}