Skip to content

Commit

Permalink
✨ feat: add a lot of funcions time4j #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Aug 8, 2024
1 parent c0ab9d0 commit 8da6016
Showing 4 changed files with 75 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -2,9 +2,11 @@ HELP.md
.gradle
build/
logs/
plugin/src/main/groovy/org.unify4j/test/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**
!**/src/test/**
!**/src/main/org.unify4j/test/**
plugin/build/
plugin/bin/

59 changes: 55 additions & 4 deletions plugin/src/main/groovy/org/unify4j/common/Time4j.java
Original file line number Diff line number Diff line change
@@ -10,11 +10,10 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class Time4j {
@@ -1069,6 +1068,58 @@ public static String format(Date date, TimezoneType timezone) {
return format(date, timezone, TimeFormatText.BIBLIOGRAPHY_EPOCH_PATTERN);
}

/**
* Converts a date-time string from a specific time zone to UTC and formats it.
* This method tries multiple input formats to parse the date-time string.
*
* @param dateTimeStr The date-time string to be converted.
* @param sourceZoneId The time zone of the input date-time string (e.g., "Asia/Ho_Chi_Minh").
* @param formats List of possible date-time formats for parsing the input string.
* @return The formatted date-time string in UTC with the format "yyyy-MM-dd'T'HH:mm:ss.SSSZ".
*/
public static String formatToUTC(String dateTimeStr, String sourceZoneId, List<String> formats) {
if (String4j.isEmpty(dateTimeStr) || Collection4j.isEmpty(formats)) {
return "";
}
if (String4j.isEmpty(sourceZoneId)) {
sourceZoneId = TimezoneType.DefaultTimezoneVietnam.getTimeZoneId();
}
ZoneId zoneId = ZoneId.of(sourceZoneId);
LocalDateTime locality;

// Try each format to parse the input date-time string
for (String format : formats) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
try {
locality = LocalDateTime.parse(dateTimeStr, formatter);
// Convert LocalDateTime to ZonedDateTime in the source time zone
ZonedDateTime sourceZone = locality.atZone(zoneId);
// Convert to UTC
ZonedDateTime utc = sourceZone.withZoneSameInstant(ZoneId.of("UTC"));
return utc.format(DateTimeFormatter.ofPattern(TimeFormatText.ISO_DATE_TIME_WITH_TIMEZONE_OFFSET));
} catch (DateTimeParseException e) {
// Continue to the next format
logger.error("Formatting date-time to UTC got en exception: {} by date-time: {}, source timezone: {} and format(s): {}",
e.getMessage(), dateTimeStr, sourceZoneId, formats, e);
}
}
// If no format succeeded, throw an exception
throw new IllegalArgumentException("Invalid date-time format or time zone");
}

/**
* Converts a date-time string from a specific time zone to UTC and formats it.
* This method tries multiple input formats to parse the date-time string.
*
* @param dateTimeStr The date-time string to be converted.
* @param sourceZoneId The time zone of the input date-time string (e.g., "Asia/Ho_Chi_Minh").
* @param formats List of possible date-time formats for parsing the input string.
* @return The formatted date-time string in UTC with the format "yyyy-MM-dd'T'HH:mm:ss.SSSZ".
*/
public static String formatToUTC(String dateTimeStr, String sourceZoneId, String... formats) {
return formatToUTC(dateTimeStr, sourceZoneId, Arrays.asList(formats));
}

/**
* Parses a date with the specified year, month, and day using the system default time zone.
*
5 changes: 5 additions & 0 deletions plugin/src/main/groovy/org/unify4j/text/TimeFormatText.java
Original file line number Diff line number Diff line change
@@ -177,5 +177,10 @@ public class TimeFormatText {
*/
public static final String SHORT_TIME_BIBLIOGRAPHY_EPOCH_PATTERN = "HH:mm:ss";

/**
* Format: yyyy-MM-dd'T'HH:mm:ss.SSSZ
* Description: Pattern for date and time including year, month, day, hour, minutes, seconds, milliseconds, and timezone offset.
*/
public static final String ISO_DATE_TIME_WITH_TIMEZONE_OFFSET = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
}

13 changes: 13 additions & 0 deletions plugin/src/test/groovy/org/unify4j/Date4jTest.java
Original file line number Diff line number Diff line change
@@ -14,4 +14,17 @@ public void testParseDate() {
Date date = Date4j.parse("2024/05/25 19:54:23");
System.out.println(Time4j.since(date) + " * " + Time4j.format(date, TimeFormatText.SPREADSHEET_BIBLIOGRAPHY_EPOCH_PATTERN));
}

@Test
public void testFormatToUTC() {
String dateTimeString = "2024-08-08 09:57:43.912608";
String sourceZoneId = "Asia/Ho_Chi_Minh";
String utcFormattedDate = Time4j.formatToUTC(dateTimeString, sourceZoneId,
"yyyy-MM-dd HH:mm:ss.SSSSSS",
"yyyy-MM-dd HH:mm:ss.SSS",
"yyyy-MM-dd HH:mm:ss.SS",
"yyyy-MM-dd HH:mm:ss.S",
"yyyy-MM-dd HH:mm:ss");
System.out.println("UTC Formatted Date: " + utcFormattedDate);
}
}

0 comments on commit 8da6016

Please sign in to comment.