001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.transforms; 014 015import java.time.*; 016import java.time.format.*; 017import java.time.temporal.*; 018import java.util.*; 019 020import org.apache.juneau.*; 021import org.apache.juneau.internal.*; 022import org.apache.juneau.transform.*; 023 024/** 025 * Swap that converts {@link Date} objects to and from strings. 026 * 027 * <p> 028 * Uses the {@link DateTimeFormatter} class for converting {@link Date} objects. 029 */ 030public class TemporalDateSwap extends StringSwap<Date> { 031 032 /** 033 * Default swap to {@link DateTimeFormatter#BASIC_ISO_DATE}. 034 * <p> 035 * Example: <js>"20111203"</js> 036 */ 037 public static class BasicIsoDate extends TemporalDateSwap { 038 /** Constructor.*/ 039 public BasicIsoDate() { 040 super("BASIC_ISO_DATE"); 041 } 042 }; 043 044 /** 045 * Default swap to {@link DateTimeFormatter#ISO_DATE}. 046 * <p> 047 * Example: <js>"2011-12-03+01:00"</js> or <js>"2011-12-03"</js> 048 */ 049 public static class IsoDate extends TemporalDateSwap { 050 /** Constructor.*/ 051 public IsoDate() { 052 super("ISO_DATE"); 053 } 054 }; 055 056 /** 057 * Default swap to {@link DateTimeFormatter#ISO_DATE_TIME}. 058 * <p> 059 * Example: <js>"2011-12-03T10:15:30+01:00[Europe/Paris]"</js> 060 */ 061 public static class IsoDateTime extends TemporalDateSwap { 062 /** Constructor.*/ 063 public IsoDateTime() { 064 super("ISO_DATE_TIME"); 065 } 066 }; 067 068 /** 069 * Default swap to {@link DateTimeFormatter#ISO_INSTANT}. 070 * <p> 071 * Example: <js>"2011-12-03T10:15:30Z"</js> 072 */ 073 public static class IsoInstant extends TemporalDateSwap { 074 /** Constructor.*/ 075 public IsoInstant() { 076 super("ISO_INSTANT"); 077 } 078 }; 079 080 /** 081 * Default swap to {@link DateTimeFormatter#ISO_LOCAL_DATE}. 082 * <p> 083 * Example: <js>"2011-12-03"</js> 084 */ 085 public static class IsoLocalDate extends TemporalDateSwap { 086 /** Constructor.*/ 087 public IsoLocalDate() { 088 super("ISO_LOCAL_DATE"); 089 } 090 }; 091 092 /** 093 * Default swap to {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME}. 094 * <p> 095 * Example: <js>"2011-12-03T10:15:30"</js> 096 */ 097 public static class IsoLocalDateTime extends TemporalDateSwap { 098 /** Constructor.*/ 099 public IsoLocalDateTime() { 100 super("ISO_LOCAL_DATE_TIME"); 101 } 102 }; 103 104 /** 105 * Default swap to {@link DateTimeFormatter#ISO_LOCAL_TIME}. 106 * <p> 107 * Example: <js>"10:15:30"</js> 108 */ 109 public static class IsoLocalTime extends TemporalDateSwap { 110 /** Constructor.*/ 111 public IsoLocalTime() { 112 super("ISO_LOCAL_TIME"); 113 } 114 }; 115 116 /** 117 * Default swap to {@link DateTimeFormatter#ISO_OFFSET_DATE}. 118 * <p> 119 * Example: <js>"2011-12-03"</js> 120 */ 121 public static class IsoOffsetDate extends TemporalDateSwap { 122 /** Constructor.*/ 123 public IsoOffsetDate() { 124 super("ISO_OFFSET_DATE"); 125 } 126 }; 127 128 /** 129 * Default swap to {@link DateTimeFormatter#ISO_OFFSET_DATE_TIME}. 130 * <p> 131 * Example: <js>"2011-12-03T10:15:30+01:00"</js> 132 */ 133 public static class IsoOffsetDateTime extends TemporalDateSwap { 134 /** Constructor.*/ 135 public IsoOffsetDateTime() { 136 super("ISO_OFFSET_DATE_TIME"); 137 } 138 }; 139 140 /** 141 * Default swap to {@link DateTimeFormatter#ISO_OFFSET_TIME}. 142 * <p> 143 * Example: <js>"10:15:30+01:00"</js> 144 */ 145 public static class IsoOffsetTime extends TemporalDateSwap { 146 /** Constructor.*/ 147 public IsoOffsetTime() { 148 super("ISO_OFFSET_TIME"); 149 } 150 }; 151 152 /** 153 * Default swap to {@link DateTimeFormatter#ISO_ORDINAL_DATE}. 154 * <p> 155 * Example: <js>"2012-337"</js> 156 */ 157 public static class IsoOrdinalDate extends TemporalDateSwap { 158 /** Constructor.*/ 159 public IsoOrdinalDate() { 160 super("ISO_ORDINAL_DATE"); 161 } 162 }; 163 164 /** 165 * Default swap to {@link DateTimeFormatter#ISO_TIME}. 166 * <p> 167 * Example: <js>"10:15:30+01:00"</js> or <js>"10:15:30"</js> 168 */ 169 public static class IsoTime extends TemporalDateSwap { 170 /** Constructor.*/ 171 public IsoTime() { 172 super("ISO_TIME"); 173 } 174 }; 175 176 /** 177 * Default swap to {@link DateTimeFormatter#ISO_WEEK_DATE}. 178 * <p> 179 * Example: <js>"2012-W48-6"</js> 180 */ 181 public static class IsoWeekDate extends TemporalDateSwap { 182 /** Constructor.*/ 183 public IsoWeekDate() { 184 super("ISO_WEEK_DATE"); 185 } 186 }; 187 188 /** 189 * Default swap to {@link DateTimeFormatter#ISO_ZONED_DATE_TIME}. 190 * <p> 191 * Example: <js>"2011-12-03T10:15:30+01:00[Europe/Paris]"</js> 192 */ 193 public static class IsoZonedDateTime extends TemporalDateSwap { 194 /** Constructor.*/ 195 public IsoZonedDateTime() { 196 super("ISO_ZONED_DATE_TIME"); 197 } 198 }; 199 200 /** 201 * Default swap to {@link DateTimeFormatter#RFC_1123_DATE_TIME}. 202 * <p> 203 * Example: <js>"Tue, 3 Jun 2008 11:05:30 GMT"</js> 204 */ 205 public static class Rfc1123DateTime extends TemporalDateSwap { 206 /** Constructor.*/ 207 public Rfc1123DateTime() { 208 super("RFC_1123_DATE_TIME"); 209 } 210 }; 211 212 213 private final DateTimeFormatter formatter; 214 215 /** 216 * Constructor. 217 * 218 * @param pattern The timestamp format or name of predefined {@link DateTimeFormatter}. 219 */ 220 public TemporalDateSwap(String pattern) { 221 super(Date.class); 222 this.formatter = DateUtils.getFormatter(pattern); 223 } 224 225 @Override /* PojoSwap */ 226 public String swap(BeanSession session, Date o) throws Exception { 227 if (o == null) 228 return null; 229 return formatter.format(o.toInstant().atZone(session.getTimeZoneId())); 230 } 231 232 @Override /* PojoSwap */ 233 public Date unswap(BeanSession session, String f, ClassMeta<?> hint) throws Exception { 234 if (f == null) 235 return null; 236 ZoneId offset = session.getTimeZoneId(); 237 TemporalAccessor ta = new DefaultingTemporalAccessor(formatter.parse(f), offset); 238 return Date.from(ZonedDateTime.from(ta).toInstant()); 239 } 240}