forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimes.java
More file actions
58 lines (39 loc) · 1.59 KB
/
Copy pathDateTimes.java
File metadata and controls
58 lines (39 loc) · 1.59 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
package modern.challenge;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
public final class DateTimes {
private DateTimes() {
throw new AssertionError("Cannot be instantiated");
}
public static List<String> localTimeToAllTimeZones7() {
List<String> result = new ArrayList<>();
String[] zoneIds = TimeZone.getAvailableIDs();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd'T'HH:mm:ss a Z");
SimpleDateFormat zoneFormatter = new SimpleDateFormat("yyyy-MMM-dd'T'HH:mm:ss a Z");
Date date = new Date();
for (String zoneId : zoneIds) {
zoneFormatter.setTimeZone(TimeZone.getTimeZone(zoneId));
result.add(formatter.format(date) + " in "
+ zoneId + " is " + zoneFormatter.format(date));
}
return result;
}
public static List<String> localTimeToAllTimeZones8() {
List<String> result = new ArrayList<>();
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd'T'HH:mm:ss a Z");
ZonedDateTime zlt = ZonedDateTime.now();
zoneIds.forEach((zoneId) -> {
result.add(zlt.format(formatter) + " in " + zoneId + " is "
+ zlt.withZoneSameInstant(ZoneId.of(zoneId)).format(formatter));
});
return result;
}
}