001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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 String pattern; 083 084 switch(dtft) { 085 case SHORT_DATE -> pattern = dateTimeFormat.getJavaShortDateFormat(); 086 case ABBREV_DATE -> pattern = dateTimeFormat.getJavaAbbrevDateFormat(); 087 case ABBREV_DATE_WITH_WEEKDAY -> pattern = dateTimeFormat.getJavaAbbrevDateFormatWeekday(); 088 case LONG_DATE -> pattern = dateTimeFormat.getJavaLongDateFormat(); 089 case LONG_DATE_WITH_WEEKDAY -> pattern = dateTimeFormat.getJavaLongDateFormatWeekday(); 090 default -> throw new IllegalArgumentException(); 091 } 092 093 return new DateFormatter(pattern); 094 } 095 096 public DateTimeFormatter getDateTimeFormatter(final TimeZoneTransfer timeZone, final DateTimeFormatTransfer dateTimeFormat, final DateTimeFormatType dtft) { 097 String pattern; 098 099 switch(dtft) { 100 case SHORT_DATE -> pattern = dateTimeFormat.getJavaShortDateFormat(); 101 case ABBREV_DATE -> pattern = dateTimeFormat.getJavaAbbrevDateFormat(); 102 case ABBREV_DATE_WITH_WEEKDAY -> pattern = dateTimeFormat.getJavaAbbrevDateFormatWeekday(); 103 case LONG_DATE -> pattern = dateTimeFormat.getJavaLongDateFormat(); 104 case LONG_DATE_WITH_WEEKDAY -> pattern = dateTimeFormat.getJavaLongDateFormatWeekday(); 105 case TIME -> pattern = dateTimeFormat.getJavaTimeFormat(); 106 case TIME_WITH_SECONDS -> pattern = dateTimeFormat.getJavaTimeFormatSeconds(); 107 default -> throw new IllegalArgumentException(); 108 } 109 110 return new DateTimeFormatter(timeZone.getJavaTimeZoneName(), pattern); 111 } 112 113}