Skip to content

Commit dbc4fc9

Browse files
Add methods to format ZonedDateTime, OffsetDateTime, and Instant as ISO date-time strings
1 parent fd7f1d5 commit dbc4fc9

1 file changed

Lines changed: 40 additions & 3 deletions

File tree

platform/core/commons/src/main/java/tools/dynamia/commons/Formatters.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
import java.text.DateFormat;
2020
import java.text.DecimalFormat;
2121
import java.text.NumberFormat;
22-
import java.time.LocalDate;
23-
import java.time.LocalDateTime;
24-
import java.time.LocalTime;
22+
import java.time.*;
2523
import java.time.format.DateTimeFormatter;
2624
import java.time.temporal.TemporalAccessor;
2725
import java.util.Date;
@@ -312,4 +310,43 @@ public static String formatDate(Date date, String pattern, java.util.Locale loca
312310
*/
313311
private Formatters() {
314312
}
313+
314+
/**
315+
* Formats a {@link ZonedDateTime} object as an ISO zoned date-time string.
316+
*
317+
* @param zonedDateTime the zoned date-time to format
318+
* @return the formatted ISO zoned date-time string
319+
*/
320+
public static String formatZonedDateTime(ZonedDateTime zonedDateTime) {
321+
if (zonedDateTime == null) {
322+
return "";
323+
}
324+
return zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
325+
}
326+
327+
/**
328+
* Formats an {@link OffsetDateTime} object as an ISO offset date-time string.
329+
*
330+
* @param offsetDateTime the offset date-time to format
331+
* @return the formatted ISO offset date-time string
332+
*/
333+
public static String formatOffsetDateTime(OffsetDateTime offsetDateTime) {
334+
if (offsetDateTime == null) {
335+
return "";
336+
}
337+
return offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
338+
}
339+
340+
/**
341+
* Formats an {@link Instant} object as an ISO offset date-time string in the default time zone.
342+
*
343+
* @param instant the instant to format
344+
* @return the formatted ISO offset date-time string
345+
*/
346+
public static String formatInstant(Instant instant) {
347+
if (instant == null) {
348+
return "";
349+
}
350+
return instant.atZone(Messages.getDefaultTimeZone()).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
351+
}
315352
}

0 commit comments

Comments
 (0)