001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.util.common.string; 018 019import com.echothree.model.control.party.common.transfer.DateTimeFormatTransfer; 020import com.echothree.model.control.party.common.transfer.TimeZoneTransfer; 021import java.time.Instant; 022import java.time.LocalDate; 023import java.time.LocalTime; 024import java.time.ZoneId; 025 026public class DateUtils { 027 028 private DateUtils() { 029 super(); 030 } 031 032 private static class DateUtilsHolder { 033 static DateUtils instance = new DateUtils(); 034 } 035 036 public static DateUtils getInstance() { 037 return DateUtilsHolder.instance; 038 } 039 040 private final java.time.format.DateTimeFormatter dateTimeFormatter = java.time.format.DateTimeFormatter.ofPattern("yyyyMMdd"); 041 042 public String formatDate(final DateTimeFormatTransfer dateTimeFormat, final Integer date) { 043 String result = null; 044 045 if(date != null) { 046 var localDate = LocalDate.parse(date.toString(), dateTimeFormatter); 047 var resultDateTimeFormatter = java.time.format.DateTimeFormatter.ofPattern(dateTimeFormat.getJavaShortDateFormat()); 048 049 result = resultDateTimeFormatter.format(localDate); 050 } 051 052 return result; 053 } 054 055 protected ZoneId getJavaTimeZone(final TimeZoneTransfer timeZone) { 056 return ZoneId.of(timeZone.getJavaTimeZoneName()); 057 } 058 059 protected String formatDateUsingShortDateFormat(final TimeZoneTransfer timeZone, final DateTimeFormatTransfer dateTimeFormat, final Instant instant) { 060 var dateTimeFormatter = java.time.format.DateTimeFormatter.ofPattern(dateTimeFormat.getJavaShortDateFormat()); 061 var localDate = LocalDate.ofInstant(instant, getJavaTimeZone(timeZone)); 062 063 return localDate.format(dateTimeFormatter); 064 } 065 066 protected String formatTimeUsingTimeFormatSeconds(final TimeZoneTransfer timeZone, final DateTimeFormatTransfer dateTimeFormat, final Instant instant) { 067 var dateTimeFormatter = java.time.format.DateTimeFormatter.ofPattern(dateTimeFormat.getJavaTimeFormatSeconds()); 068 var localTime = LocalTime.ofInstant(instant, getJavaTimeZone(timeZone)); 069 070 return localTime.format(dateTimeFormatter); 071 } 072 073 public String formatTypicalDateTime(final TimeZoneTransfer timeZone, final DateTimeFormatTransfer dateTimeFormat, final Instant instant) { 074 return instant == null || dateTimeFormat == null ? null : formatDateUsingShortDateFormat(timeZone, dateTimeFormat, instant) + ' ' + formatTimeUsingTimeFormatSeconds(timeZone, dateTimeFormat, instant); 075 } 076 077 public String formatTypicalDateTime(final TimeZoneTransfer timeZone, final DateTimeFormatTransfer dateTimeFormat, final Long time) { 078 return time == null? null: formatTypicalDateTime(timeZone, dateTimeFormat, Instant.ofEpochMilli(time)); 079 } 080 081 public DateFormatter getDateFormatter(DateTimeFormatTransfer dateTimeFormat, DateTimeFormatType dtft) { 082 var pattern = switch(dtft) { 083 case SHORT_DATE -> dateTimeFormat.getJavaShortDateFormat(); 084 case ABBREV_DATE -> dateTimeFormat.getJavaAbbrevDateFormat(); 085 case ABBREV_DATE_WITH_WEEKDAY -> dateTimeFormat.getJavaAbbrevDateFormatWeekday(); 086 case LONG_DATE -> dateTimeFormat.getJavaLongDateFormat(); 087 case LONG_DATE_WITH_WEEKDAY -> dateTimeFormat.getJavaLongDateFormatWeekday(); 088 default -> throw new IllegalArgumentException(); 089 }; 090 091 return new DateFormatter(pattern); 092 } 093 094 public DateTimeFormatter getDateTimeFormatter(final TimeZoneTransfer timeZone, final DateTimeFormatTransfer dateTimeFormat, final DateTimeFormatType dtft) { 095 var pattern = switch(dtft) { 096 case SHORT_DATE -> dateTimeFormat.getJavaShortDateFormat(); 097 case ABBREV_DATE -> dateTimeFormat.getJavaAbbrevDateFormat(); 098 case ABBREV_DATE_WITH_WEEKDAY -> dateTimeFormat.getJavaAbbrevDateFormatWeekday(); 099 case LONG_DATE -> dateTimeFormat.getJavaLongDateFormat(); 100 case LONG_DATE_WITH_WEEKDAY -> dateTimeFormat.getJavaLongDateFormatWeekday(); 101 case TIME -> dateTimeFormat.getJavaTimeFormat(); 102 case TIME_WITH_SECONDS -> dateTimeFormat.getJavaTimeFormatSeconds(); 103 default -> throw new IllegalArgumentException(); 104 }; 105 106 return new DateTimeFormatter(timeZone.getJavaTimeZoneName(), pattern); 107 } 108 109}