0
# Time & Date Operations
1
2
Time unit conversions and ISO 8601 date formatting utilities for consistent time handling across microservices. Provides standardized time operations and date formatting for distributed systems.
3
4
## Capabilities
5
6
### Time Utilities
7
8
Time unit conversion utilities for working with different time units and timestamp calculations.
9
10
```java { .api }
11
/**
12
* Time unit conversion utilities
13
*/
14
public class TimeUtil {
15
/**
16
* Converts time unit to milliseconds
17
* @param timeUnit - TimeUnit to convert
18
* @return Milliseconds for one unit of the specified time unit
19
*/
20
public static long oneTimeUnitMillisecond(TimeUnit timeUnit);
21
22
/**
23
* Gets next rounded timestamp for the given time unit
24
* @param timeUnit - Time unit for rounding
25
* @param start - Starting timestamp
26
* @return Next timestamp rounded to the time unit boundary
27
*/
28
public static long nextStartTimestamp(TimeUnit timeUnit, long start);
29
}
30
```
31
32
### Date Utilities
33
34
ISO 8601 date formatting and parsing utilities for standardized date handling.
35
36
```java { .api }
37
/**
38
* ISO 8601 date formatting and parsing
39
*/
40
public class DateUtil {
41
/**
42
* Formats date as ISO 8601 string
43
* @param date - Instant to format
44
* @return ISO 8601 formatted date string
45
*/
46
public static String formatIso8601Date(Instant date);
47
48
/**
49
* Parses ISO 8601 date string to Instant
50
* @param dateString - ISO 8601 date string
51
* @return Parsed Instant
52
*/
53
public static Instant parseIso8601Date(String dateString);
54
55
/**
56
* Parses ISO 8601 date string to milliseconds
57
* @param dateString - ISO 8601 date string
58
* @return Milliseconds since epoch
59
*/
60
public static long parseIso8601DateToMillis(String dateString);
61
}
62
```
63
64
**Usage Examples:**
65
66
```java
67
import com.networknt.utility.TimeUtil;
68
import com.networknt.utility.DateUtil;
69
import java.time.Instant;
70
import java.util.concurrent.TimeUnit;
71
72
// Time unit conversions
73
long secondInMillis = TimeUtil.oneTimeUnitMillisecond(TimeUnit.SECONDS); // 1000
74
long minuteInMillis = TimeUtil.oneTimeUnitMillisecond(TimeUnit.MINUTES); // 60000
75
long hourInMillis = TimeUtil.oneTimeUnitMillisecond(TimeUnit.HOURS); // 3600000
76
77
// Timestamp rounding
78
long currentTime = System.currentTimeMillis();
79
long nextMinute = TimeUtil.nextStartTimestamp(TimeUnit.MINUTES, currentTime);
80
long nextHour = TimeUtil.nextStartTimestamp(TimeUnit.HOURS, currentTime);
81
82
// ISO 8601 date formatting
83
Instant now = Instant.now();
84
String isoDate = DateUtil.formatIso8601Date(now);
85
// Result: "2023-12-25T10:30:45.123Z"
86
87
// ISO 8601 date parsing
88
String dateString = "2023-12-25T10:30:45.123Z";
89
Instant parsed = DateUtil.parseIso8601Date(dateString);
90
long millis = DateUtil.parseIso8601DateToMillis(dateString);
91
92
// Microservice timestamp coordination
93
public class EventLogger {
94
public void logEvent(String event) {
95
Instant timestamp = Instant.now();
96
String isoTimestamp = DateUtil.formatIso8601Date(timestamp);
97
System.out.println(isoTimestamp + " - " + event);
98
}
99
100
public void scheduleNextExecution(TimeUnit interval, long currentTime) {
101
long nextExecution = TimeUtil.nextStartTimestamp(interval, currentTime);
102
// Schedule task at nextExecution
103
}
104
}
105
106
// Time-based cache expiration
107
public class TimeBasedCache {
108
private final long ttlMillis;
109
110
public TimeBasedCache(long amount, TimeUnit unit) {
111
this.ttlMillis = amount * TimeUtil.oneTimeUnitMillisecond(unit);
112
}
113
114
public boolean isExpired(long creationTime) {
115
return System.currentTimeMillis() - creationTime > ttlMillis;
116
}
117
}
118
```