13
13
14
14
package io .nats .json ;
15
15
16
+ import org .jetbrains .annotations .NotNull ;
17
+ import org .jetbrains .annotations .Nullable ;
18
+
16
19
import java .time .Duration ;
17
20
import java .time .Instant ;
18
21
import java .time .ZoneId ;
@@ -29,31 +32,36 @@ private DateTimeUtils() {} /* ensures cannot be constructed */
29
32
/**
30
33
* The ZoneId for GMT
31
34
*/
35
+ @ NotNull
32
36
public static final ZoneId ZONE_ID_GMT = ZoneId .of ("GMT" );
33
37
34
38
/**
35
39
* The ZoneDateTime uses as a default, can be used instead of null
36
40
*/
41
+ @ NotNull
37
42
public static final ZonedDateTime DEFAULT_TIME = ZonedDateTime .of (1 , 1 , 1 , 0 , 0 , 0 , 0 , ZONE_ID_GMT );
38
43
39
44
/**
40
45
* The formatter to crate RFC 3339 strings from dates.
41
46
*/
47
+ @ NotNull
42
48
public static final DateTimeFormatter RFC3339_FORMATTER = DateTimeFormatter .ofPattern ("yyyy-MM-dd'T'HH:mm:ss.nnnnnnnnn'Z'" );
43
49
44
50
/**
45
51
* Convert a ZoneDateTime to a GMT version of the ZoneDateTime
46
52
* @param zonedDateTime the input
47
53
* @return the output
48
54
*/
49
- public static ZonedDateTime toGmt (ZonedDateTime zonedDateTime ) {
55
+ @ NotNull
56
+ public static ZonedDateTime toGmt (@ NotNull ZonedDateTime zonedDateTime ) {
50
57
return zonedDateTime .withZoneSameInstant (ZONE_ID_GMT );
51
58
}
52
59
53
60
/**
54
61
* Get an instance of ZonedDateTime.now(), but for the GMT timezone
55
62
* @return the current date-time using the system clock in GMT
56
63
*/
64
+ @ NotNull
57
65
public static ZonedDateTime gmtNow () {
58
66
return ZonedDateTime .now ().withZoneSameInstant (ZONE_ID_GMT );
59
67
}
@@ -64,7 +72,7 @@ public static ZonedDateTime gmtNow() {
64
72
* @param zdt2 the second ZonedDateTime
65
73
* @return true if they are equal
66
74
*/
67
- public static boolean equals (ZonedDateTime zdt1 , ZonedDateTime zdt2 ) {
75
+ public static boolean equals (@ Nullable ZonedDateTime zdt1 , @ Nullable ZonedDateTime zdt2 ) {
68
76
if (zdt1 == zdt2 ) return true ;
69
77
if (zdt1 == null || zdt2 == null ) return false ;
70
78
return zdt1 .withZoneSameInstant (ZONE_ID_GMT ).equals (zdt2 .withZoneSameInstant (ZONE_ID_GMT ));
@@ -75,7 +83,8 @@ public static boolean equals(ZonedDateTime zdt1, ZonedDateTime zdt2) {
75
83
* @param zonedDateTime the input
76
84
* @return the formatted string
77
85
*/
78
- public static String toRfc3339 (ZonedDateTime zonedDateTime ) {
86
+ @ NotNull
87
+ public static String toRfc3339 (@ NotNull ZonedDateTime zonedDateTime ) {
79
88
return RFC3339_FORMATTER .format (toGmt (zonedDateTime ));
80
89
}
81
90
@@ -84,7 +93,8 @@ public static String toRfc3339(ZonedDateTime zonedDateTime) {
84
93
* @param dateTime - date time from the server.
85
94
* @return a Zoned Date time.
86
95
*/
87
- public static ZonedDateTime parseDateTime (String dateTime ) {
96
+ @ NotNull
97
+ public static ZonedDateTime parseDateTime (@ NotNull String dateTime ) {
88
98
return parseDateTime (dateTime , DEFAULT_TIME );
89
99
}
90
100
@@ -94,7 +104,8 @@ public static ZonedDateTime parseDateTime(String dateTime) {
94
104
* @param dflt - the default ZoneDateTime to use if the input string exceptions while parsing
95
105
* @return a ZonedDateTime.
96
106
*/
97
- public static ZonedDateTime parseDateTime (String dateTime , ZonedDateTime dflt ) {
107
+ @ NotNull
108
+ public static ZonedDateTime parseDateTime (@ NotNull String dateTime , @ NotNull ZonedDateTime dflt ) {
98
109
try {
99
110
return toGmt (ZonedDateTime .parse (dateTime ));
100
111
}
@@ -108,7 +119,8 @@ public static ZonedDateTime parseDateTime(String dateTime, ZonedDateTime dflt) {
108
119
* @param dateTime - date time from the server.
109
120
* @return a ZonedDateTime.
110
121
*/
111
- public static ZonedDateTime parseDateTimeThrowParseError (String dateTime ) {
122
+ @ NotNull
123
+ public static ZonedDateTime parseDateTimeThrowParseError (@ NotNull String dateTime ) {
112
124
return toGmt (ZonedDateTime .parse (dateTime ));
113
125
}
114
126
@@ -117,6 +129,7 @@ public static ZonedDateTime parseDateTimeThrowParseError(String dateTime) {
117
129
* @param millis the millis from now value
118
130
* @return a ZonedDateTime.
119
131
*/
132
+ @ NotNull
120
133
public static ZonedDateTime fromNow (long millis ) {
121
134
return ZonedDateTime .ofInstant (Instant .now ().plusMillis (millis ), ZONE_ID_GMT );
122
135
}
@@ -126,7 +139,8 @@ public static ZonedDateTime fromNow(long millis) {
126
139
* @param dur the duration to use the millis for from now
127
140
* @return a ZonedDateTime.
128
141
*/
129
- public static ZonedDateTime fromNow (Duration dur ) {
142
+ @ NotNull
143
+ public static ZonedDateTime fromNow (@ NotNull Duration dur ) {
130
144
return ZonedDateTime .ofInstant (Instant .now ().plusMillis (dur .toMillis ()), ZONE_ID_GMT );
131
145
}
132
146
}
0 commit comments