-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaTime.java
More file actions
41 lines (26 loc) · 1.58 KB
/
JavaTime.java
File metadata and controls
41 lines (26 loc) · 1.58 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
import java.time.*; // LocalDateTime, LocalTime, LocalDate
import java.time.format.*; // FormatStyle(enum), DateTimeFormatter
class JavaTime {
public static void main(String... sth) {
System.out.println(LocalDateTime.now() + "\n" + LocalTime.now() + '\n' + LocalDate.now());
// convert from LocalDateTime instance
var ldt = LocalDateTime.now();
LocalDate ld = ldt.toLocalDate();
LocalTime lt = ldt.toLocalTime();
System.out.println("Date: " + ld + "\nTime: " + lt);
// format date and time
var time = LocalTime.now();
String timeFormatted = time.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT));
String dateFormatted = LocalDateTime.now().format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT));
System.out.println(timeFormatted + '\n' + dateFormatted);
//ISO_DATE and ISO_TIME
//var iso = LocalDateTime.now().format(DateTimeFormatter.ofLocalizedDateTime(DateTimeFormatter.ISO_DATE, DateTimeFormatter.ISO_TIME));
//System.out.println(iso);
// custom format: static DateTimeFormatter ofPattern(String p)
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy'-'dd'-'M HH':'MM':'s")));
// obtaining various date and time components
System.out.println(ldt.getHour() + "\n" + ldt.getMonth() + "\n" + ldt.getYear());
// parsing date and time strings: static ldt/ld/lt parse(CharSequence s) { iso format } || static ldt/ld/lt parse(CharSequence c, DateTimeFormatter d) {custom}
System.out.println(ldt.parse("10-28-2021 12:00 AM", DateTimeFormatter.ofPattern("MM'-'dd'-'yyyy hh':'mm a")));
}
}