Skip to content

Commit b1c7ee3

Browse files
committed
✨ feat: add unify functions for Time4j #4
1 parent 86ee356 commit b1c7ee3

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

plugin/src/main/groovy/org/unify4j/common/Time4j.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5+
import org.unify4j.model.enums.TimezoneType;
56
import org.unify4j.text.TimeFormatText;
67

78
import java.sql.Timestamp;
@@ -1010,6 +1011,64 @@ public static String format(Date date) {
10101011
return format(date, TimeFormatText.BIBLIOGRAPHY_EPOCH_PATTERN);
10111012
}
10121013

1014+
/**
1015+
* Formats a Date object into a string representation using the specified timezone.
1016+
*
1017+
* @param date The Date object to be formatted.
1018+
* @param timezone The timezone.
1019+
* @param format The format string used to format the date.
1020+
* @return A string representation of the formatted date with the specified timezone.
1021+
* @throws IllegalArgumentException if the format is null or invalid.
1022+
*/
1023+
public static String format(Date date, TimeZone timezone, String format) {
1024+
if (date == null || timezone == null || String4j.isEmpty(format)) {
1025+
return "";
1026+
}
1027+
SimpleDateFormat sdf = new SimpleDateFormat(format);
1028+
sdf.setTimeZone(timezone);
1029+
return sdf.format(date);
1030+
}
1031+
1032+
/**
1033+
* Formats a Date object into a string representation using the specified timezone.
1034+
*
1035+
* @param date The Date object to be formatted.
1036+
* @param timezone The timezone enum type from TimezoneText enum.
1037+
* @param format The format string used to format the date.
1038+
* @return A string representation of the formatted date with the specified timezone.
1039+
* @throws IllegalArgumentException if the format is null or invalid.
1040+
*/
1041+
public static String format(Date date, TimezoneType timezone, String format) {
1042+
if (date == null || timezone == null || String4j.isEmpty(format)) {
1043+
return "";
1044+
}
1045+
return format(date, TimeZone.getTimeZone(timezone.getTimeZoneId()), format);
1046+
}
1047+
1048+
/**
1049+
* Formats a Date object into a string representation using the specified timezone.
1050+
*
1051+
* @param date The Date object to be formatted.
1052+
* @param timezone The timezone.
1053+
* @return A string representation of the formatted date with the specified timezone.
1054+
* @throws IllegalArgumentException if the format is null or invalid.
1055+
*/
1056+
public static String format(Date date, TimeZone timezone) {
1057+
return format(date, timezone, TimeFormatText.BIBLIOGRAPHY_EPOCH_PATTERN);
1058+
}
1059+
1060+
/**
1061+
* Formats a Date object into a string representation using the specified timezone.
1062+
*
1063+
* @param date The Date object to be formatted.
1064+
* @param timezone The timezone.
1065+
* @return A string representation of the formatted date with the specified timezone.
1066+
* @throws IllegalArgumentException if the format is null or invalid.
1067+
*/
1068+
public static String format(Date date, TimezoneType timezone) {
1069+
return format(date, timezone, TimeFormatText.BIBLIOGRAPHY_EPOCH_PATTERN);
1070+
}
1071+
10131072
/**
10141073
* Parses a date with the specified year, month, and day using the system default time zone.
10151074
*
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.unify4j.model.enums;
2+
3+
import java.util.TimeZone;
4+
5+
// @formatter:off
6+
@SuppressWarnings({"SpellCheckingInspection"})
7+
public enum TimezoneType {
8+
DefaultTimezoneVietnam("Asia/Ho_Chi_Minh"),
9+
DefaultTimezoneNewYork("America/New_York"),
10+
DefaultTimezoneLondon("Europe/London"),
11+
DefaultTimezoneTokyo("Asia/Tokyo"),
12+
DefaultTimezoneSydney("Australia/Sydney"),
13+
DefaultTimezoneParis("Europe/Paris"),
14+
DefaultTimezoneMoscow("Europe/Moscow"),
15+
DefaultTimezoneLosAngeles("America/Los_Angeles"),
16+
DefaultTimezoneManila("Asia/Manila"),
17+
DefaultTimezoneKualaLumpur("Asia/Kuala_Lumpur"),
18+
DefaultTimezoneJakarta("Asia/Jakarta"),
19+
DefaultTimezoneYangon("Asia/Yangon"),
20+
DefaultTimezoneAuckland("Pacific/Auckland"),
21+
DefaultTimezoneBangkok("Asia/Bangkok"),
22+
DefaultTimezoneDelhi("Asia/Kolkata"), // Note: India standard time
23+
DefaultTimezoneDubai("Asia/Dubai"),
24+
DefaultTimezoneCairo("Africa/Cairo"),
25+
DefaultTimezoneAthens("Europe/Athens"),
26+
DefaultTimezoneRome("Europe/Rome"),
27+
DefaultTimezoneJohannesburg("Africa/Johannesburg"),
28+
DefaultTimezoneStockholm("Europe/Stockholm"),
29+
DefaultTimezoneOslo("Europe/Oslo"),
30+
DefaultTimezoneHelsinki("Europe/Helsinki"),
31+
DefaultTimezoneKiev("Europe/Kiev"),
32+
DefaultTimezoneBeijing("Asia/Shanghai"),
33+
DefaultTimezoneSingapore("Asia/Singapore"),
34+
DefaultTimezoneIslamabad("Asia/Karachi"),
35+
DefaultTimezoneColombo("Asia/Colombo"),
36+
DefaultTimezoneDhaka("Asia/Dhaka"),
37+
DefaultTimezoneKathmandu("Asia/Kathmandu"),
38+
DefaultTimezoneBrisbane("Australia/Brisbane"),
39+
DefaultTimezoneWellington("Pacific/Auckland"), // Note: Wellington uses the same as Auckland
40+
DefaultTimezonePortMoresby("Pacific/Port_Moresby"),
41+
DefaultTimezoneSuva("Pacific/Fiji");
42+
43+
private final String timeZoneId;
44+
45+
TimezoneType(String timeZoneId) {
46+
this.timeZoneId = timeZoneId;
47+
}
48+
49+
public String getTimeZoneId() {
50+
return timeZoneId;
51+
}
52+
53+
public static TimeZone getTimeZone(TimezoneType timezone) {
54+
return TimeZone.getTimeZone(timezone.getTimeZoneId());
55+
}
56+
}
57+
// @formatter:on

0 commit comments

Comments
 (0)