forked from arafkarsh/JavaExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeExample2.java
More file actions
135 lines (111 loc) · 4.67 KB
/
Copy pathDateTimeExample2.java
File metadata and controls
135 lines (111 loc) · 4.67 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Copyright (c) 2018 Araf Karsh Hamid
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
* or (per the licensee's choosing)
* under the terms of the Apache 2 License version 2.0
* as published by the Apache Software Foundation.
*/
package java08.time;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author Araf Karsh Hamid
* @version 1.0
* @date
*/
public class DateTimeExample2 {
public static void main (String[] args) {
example1();
example2();
}
/**
* How to create an OffsetDateTime object in Java
*/
public static void example1() {
// Current date time with an offset
OffsetDateTime dateTime = OffsetDateTime.now();
System.out.println(dateTime);
// Current date time from specified clock with an offset
OffsetDateTime dateTime2 = OffsetDateTime.now(Clock.systemDefaultZone());
System.out.println(dateTime2);
// Current date time from specified time-zone with an offset
OffsetDateTime dateTime3 = OffsetDateTime.now(ZoneId.systemDefault());
System.out.println(dateTime3);
// Specific date time from LocalDateTime with an offset
OffsetDateTime dateTime4 = OffsetDateTime.of(LocalDateTime.of(2017, 05, 12, 05, 45),
ZoneOffset.ofHoursMinutes(6, 30));
System.out.println(dateTime4);
}
/**
* How to get LocalDateTime, LocalDate, LocalTime, ZonedDateTime,
* Instant and OffsetTime from OffsetDateTime
*/
public static void example2() {
// Current date time with an offset
OffsetDateTime dateTime = OffsetDateTime.now();
System.out.println(dateTime);
// To LocalDateTime
LocalDateTime localDateTime = dateTime.toLocalDateTime();
System.out.println(localDateTime);
// To LocalDate
LocalDate localDate = dateTime.toLocalDate();
System.out.println(localDate);
// To LocalTime
LocalTime localTime = dateTime.toLocalTime();
System.out.println(localTime);
// To ZonedDateTime
ZonedDateTime zonedDateTime = dateTime.toZonedDateTime();
System.out.println(zonedDateTime);
// To Instant
Instant instant=dateTime.toInstant();
System.out.println(instant);
// To OffsetTime
OffsetTime offsetTime=dateTime.toOffsetTime();
System.out.println(offsetTime);
}
/**
* How to convert or parse String to OffsetDateTime in java
*/
public static void example3() {
// Parsing ISO offset date time
OffsetDateTime dateTime = OffsetDateTime.parse("2017-07-07T21:36:10.598+05:30",
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(dateTime);
// Parsing 'yyyy-MMM-dd HH:mm:ss Z' pattern
OffsetDateTime dateTime1 = OffsetDateTime.parse("2017-May-02 23:35:05 +0530",
DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss Z"));
System.out.println(dateTime1);
// Parsing 'yyyy-MM-dd KK:mm:ss a x' pattern
OffsetDateTime dateTime2 = OffsetDateTime.parse("2017-05-30 10:20:30 AM +0530",
DateTimeFormatter.ofPattern("yyyy-MM-dd KK:mm:ss a x"));
System.out.println(dateTime2);
// Formatting 'cccc, MMMM dd, yyyy KK:mm a X' pattern
OffsetDateTime dateTime3 = OffsetDateTime.parse("Wednesday, May 31, 2017 10:21 PM +0530",
DateTimeFormatter.ofPattern("cccc, MMMM dd, yyyy KK:mm a X"));
System.out.println(dateTime3);
}
}