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 static org.apache.juneau.utils.CalendarUtils.Format.*;
016
017import java.text.*;
018import java.util.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.parser.ParseException;
022import org.apache.juneau.transform.*;
023import org.apache.juneau.utils.*;
024
025/**
026 * Transforms {@link Calendar Calendars} to {@link String Strings}.
027 *
028 * <h5 class='topic'>Behavior-specific subclasses</h5>
029 *
030 * The following direct subclasses are provided for convenience to the following formats:
031 * <ul>
032 *    <li>{@link ToString} - To {@link String Strings} using the {@code Date.toString()} method.
033 *    <li>{@link ISO8601DT} - To ISO8601 date-time strings.
034 *    <li>{@link ISO8601DTZ} - Same as {@link ISO8601DT}, except always serializes in GMT.
035 *    <li>{@link ISO8601DTP} - Same as {@link ISO8601DT} except with millisecond precision.
036 *    <li>{@link ISO8601DTPZ} - Same as {@link ISO8601DTZ} except with millisecond precision.
037 *    <li>{@link RFC2822DT} - To RFC2822 date-time strings.
038 *    <li>{@link RFC2822DTZ} - Same as {@link RFC2822DT}, except always serializes in GMT.
039 *    <li>{@link RFC2822D} - To RFC2822 date strings.
040 *    <li>{@link DateTimeSimple} - To simple <js>"yyyy/MM/dd HH:mm:ss"</js> date-time strings.
041 *    <li>{@link DateSimple} - To simple <js>"yyyy/MM/dd"</js> date strings.
042 *    <li>{@link TimeSimple} - To simple <js>"HH:mm:ss"</js> time strings.
043 *    <li>{@link DateFull} - To {@link DateFormat#FULL} date strings.
044 *    <li>{@link DateLong} - To {@link DateFormat#LONG} date strings.
045 *    <li>{@link DateMedium} - To {@link DateFormat#MEDIUM} date strings.
046 *    <li>{@link DateShort} - To {@link DateFormat#SHORT} date strings.
047 *    <li>{@link TimeFull} - To {@link DateFormat#FULL} time strings.
048 *    <li>{@link TimeLong} - To {@link DateFormat#LONG} time strings.
049 *    <li>{@link TimeMedium} - To {@link DateFormat#MEDIUM} time strings.
050 *    <li>{@link TimeShort} - To {@link DateFormat#SHORT} time strings.
051 *    <li>{@link DateTimeFull} - To {@link DateFormat#FULL} date-time strings.
052 *    <li>{@link DateTimeLong} - To {@link DateFormat#LONG} date-time strings.
053 *    <li>{@link DateTimeMedium} - To {@link DateFormat#MEDIUM} date-time strings.
054 *    <li>{@link DateTimeShort} - To {@link DateFormat#SHORT} date-time strings.
055 * </ul>
056 *
057 * @deprecated Use {@link TemporalCalendarSwap}
058 */
059@Deprecated
060public class CalendarSwap extends StringSwap<Calendar> {
061
062   /**
063    * Transforms {@link Calendar Calendars} to {@link String Strings} using the {@code Date.toString()} method.
064    *
065    * <h5 class='section'>Example Output:</h5>
066    * <ul>
067    *    <li><js>"Wed Jul 04 15:30:45 EST 2001"</js>
068    * </ul>
069    */
070   public static class ToString extends CalendarSwap {
071
072      @Override /* PojoSwap */
073      public String swap(BeanSession session, Calendar o) throws Exception {
074         return CalendarUtils.serialize(o, TO_STRING, session.getLocale(), session.getTimeZone());
075      }
076
077      @Override /* PojoSwap */
078      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
079         return convert(CalendarUtils.parseDate(o, TO_STRING, session.getLocale(), session.getTimeZone()), hint, session);
080      }
081   }
082
083   /**
084    * Transforms {@link Calendar Calendars} to ISO8601 date-time strings.
085    *
086    * <h5 class='section'>Example Output:</h5>
087    * <ul>
088    *    <li><js>"2001-07-04T15:30:45-05:00"</js>
089    *    <li><js>"2001-07-04T15:30:45Z"</js>
090    * </ul>
091    *
092    * <h5 class='topic'>Example input:</h5>
093    * <ul>
094    *    <li><js>"2001-07-04T15:30:45-05:00"</js>
095    *    <li><js>"2001-07-04T15:30:45Z"</js>
096    *    <li><js>"2001-07-04T15:30:45.1Z"</js>
097    *    <li><js>"2001-07-04T15:30Z"</js>
098    *    <li><js>"2001-07-04"</js>
099    *    <li><js>"2001-07"</js>
100    *    <li><js>"2001"</js>
101    * </ul>
102    */
103   public static class ISO8601DT extends CalendarSwap {
104
105      @Override /* PojoSwap */
106      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
107         return CalendarUtils.parseCalendar(o, ISO8601_DT, session.getLocale(), session.getTimeZone());
108      }
109
110      @Override /* PojoSwap */
111      public String swap(BeanSession session, Calendar o) throws Exception {
112         return CalendarUtils.serialize(o, ISO8601_DT, session.getLocale(), session.getTimeZone());
113      }
114   }
115
116   /**
117    * Transforms {@link Calendar Calendars} to ISO8601 date-time-local strings.
118    *
119    * <h5 class='section'>Example Output:</h5>
120    * <ul>
121    *    <li><js>"2001-07-04T15:30:45"</js>
122    * </ul>
123    *
124    * <h5 class='topic'>Example input:</h5>
125    * <ul>
126    *    <li><js>"2001-07-04T15:30:45"</js>
127    *    <li><js>"2001-07-04T15:30:45.1"</js>
128    *    <li><js>"2001-07-04T15:30"</js>
129    *    <li><js>"2001-07-04"</js>
130    *    <li><js>"2001-07"</js>
131    *    <li><js>"2001"</js>
132    * </ul>
133    */
134   public static class ISO8601DTL extends CalendarSwap {
135
136      @Override /* PojoSwap */
137      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
138         return convert(CalendarUtils.parseCalendar(o, ISO8601_DTL, session.getLocale(), session.getTimeZone()), hint);
139      }
140
141      @Override /* PojoSwap */
142      public String swap(BeanSession session, Calendar o) throws Exception {
143         return CalendarUtils.serialize(o, ISO8601_DTL, session.getLocale(), session.getTimeZone());
144      }
145   }
146
147   /**
148    * Same as {@link ISO8601DT}, except always serializes in GMT.
149    *
150    * <h5 class='section'>Example Output:</h5>
151    * <js>"2001-07-04T15:30:45Z"</js>
152    */
153   public static class ISO8601DTZ extends CalendarSwap {
154
155      @Override /* PojoSwap */
156      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
157         return CalendarUtils.parseCalendar(o, ISO8601_DTZ, session.getLocale(), session.getTimeZone());
158      }
159
160      @Override /* PojoSwap */
161      public String swap(BeanSession session, Calendar o) throws Exception {
162         return CalendarUtils.serialize(o, ISO8601_DTZ, session.getLocale(), session.getTimeZone());
163      }
164   }
165
166   /**
167    * Same as {@link ISO8601DT} except serializes to millisecond precision.
168    *
169    * <h5 class='section'>Example Output:</h5>
170    * <js>"2001-07-04T15:30:45.123Z"</js>
171    */
172   public static class ISO8601DTP extends ISO8601DT {
173
174      @Override /* PojoSwap */
175      public String swap(BeanSession session, Calendar o) throws Exception {
176         return CalendarUtils.serialize(o, ISO8601_DTP, session.getLocale(), session.getTimeZone());
177      }
178   }
179
180   /**
181    * Same as {@link ISO8601DTZ} except serializes to millisecond precision.
182    *
183    * <h5 class='section'>Example Output:</h5>
184    * <js>"2001-07-04T15:30:45.123"</js>
185    */
186   public static class ISO8601DTPZ extends ISO8601DTZ {
187
188      @Override /* PojoSwap */
189      public String swap(BeanSession session, Calendar o) throws Exception {
190         return CalendarUtils.serialize(o, ISO8601_DTPZ, session.getLocale(), session.getTimeZone());
191      }
192   }
193
194   /**
195    * ISO8601 date only.
196    *
197    * <h5 class='section'>Example Output:</h5>
198    * <js>"2001-07-04"</js>
199    */
200   public static class ISO8601D extends CalendarSwap {
201
202      @Override /* PojoSwap */
203      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
204         return CalendarUtils.parseCalendar(o, ISO8601_D, session.getLocale(), session.getTimeZone());
205      }
206
207      @Override /* PojoSwap */
208      public String swap(BeanSession session, Calendar o) throws Exception {
209         return CalendarUtils.serialize(o, ISO8601_D, session.getLocale(), session.getTimeZone());
210      }
211   }
212
213   /**
214    * Transforms {@link Calendar Calendars} to RFC2822 date-time strings.
215    *
216    * <h5 class='section'>Example Output:</h5>
217    * <ul>
218    *    <li><js>"Sat, 03 Mar 2001 10:11:12 +0000"</js> <jc>// en_US</jc>
219    *    <li><js>"土, 03 3 2001 10:11:12 +0000"</js> <jc>// ja_JP</jc>
220    *    <li><js>"토, 03 3월 2001 10:11:12 +0000"</js> <jc>// ko_KR</jc>
221    * </ul>
222    */
223   public static class RFC2822DT extends CalendarSwap {
224
225      @Override /* PojoSwap */
226      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
227         return CalendarUtils.parseCalendar(o, RFC2822_DT, session.getLocale(), session.getTimeZone());
228      }
229
230      @Override /* PojoSwap */
231      public String swap(BeanSession session, Calendar o) throws Exception {
232         return CalendarUtils.serialize(o, RFC2822_DT, session.getLocale(), session.getTimeZone());
233      }
234   }
235
236   /**
237    * Same as {@link RFC2822DT}, except always serializes in GMT.
238    *
239    * <h5 class='section'>Example Output:</h5>
240    * <ul>
241    *    <li><js>"Sat, 03 Mar 2001 10:11:12 GMT"</js> <jc>// en_US</jc>
242    *    <li><js>"土, 03 3 2001 10:11:12 GMT"</js> <jc>// ja_JP</jc>
243    *    <li><js>"토, 03 3월 2001 10:11:12 GMT"</js> <jc>// ko_KR</jc>
244    * </ul>
245    */
246   public static class RFC2822DTZ extends CalendarSwap {
247
248      @Override /* PojoSwap */
249      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
250         return CalendarUtils.parseCalendar(o, RFC2822_DTZ, session.getLocale(), session.getTimeZone());
251      }
252
253      @Override /* PojoSwap */
254      public String swap(BeanSession session, Calendar o) throws Exception {
255         return CalendarUtils.serialize(o, RFC2822_DTZ, session.getLocale(), session.getTimeZone());
256      }
257   }
258
259   /**
260    * Transforms {@link Calendar Calendars} to RFC2822 date strings.
261    *
262    * <h5 class='section'>Example Output:</h5>
263    * <ul>
264    *    <li><js>"03 Mar 2001"</js> <jc>// en_US</jc>
265    *    <li><js>"03 3 2001"</js> <jc>// ja_JP</jc>
266    *    <li><js>"03 3월 2001"</js> <jc>// ko_KR</jc>
267    * </ul>
268    */
269   public static class RFC2822D extends CalendarSwap {
270
271      @Override /* PojoSwap */
272      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
273         return CalendarUtils.parseCalendar(o, RFC2822_D, session.getLocale(), session.getTimeZone());
274      }
275
276      @Override /* PojoSwap */
277      public String swap(BeanSession session, Calendar o) throws Exception {
278         return CalendarUtils.serialize(o, RFC2822_D, session.getLocale(), session.getTimeZone());
279      }
280   }
281
282   /**
283    * Transforms {@link Calendar Calendars} to simple <js>"yyyy/MM/dd HH:mm:ss"</js> date-time strings.
284    *
285    * <h5 class='section'>Example Output:</h5>
286    * <ul>
287    *    <li><js>"2001/03/03 10:11:12"</js>
288    * </ul>
289    */
290   public static class DateTimeSimple extends CalendarSwap {
291
292      @Override /* PojoSwap */
293      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
294         return CalendarUtils.parseCalendar(o, SIMPLE_DT, session.getLocale(), session.getTimeZone());
295      }
296
297      @Override /* PojoSwap */
298      public String swap(BeanSession session, Calendar o) throws Exception {
299         return CalendarUtils.serialize(o, SIMPLE_DT, session.getLocale(), session.getTimeZone());
300      }
301   }
302
303   /**
304    * Transforms {@link Calendar Calendars} to simple <js>"yyyy/MM/dd"</js> date strings.
305    *
306    * <h5 class='section'>Example Output:</h5>
307    * <ul>
308    *    <li><js>"2001/03/03"</js>
309    * </ul>
310    */
311   public static class DateSimple extends CalendarSwap {
312
313      @Override /* PojoSwap */
314      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
315         return CalendarUtils.parseCalendar(o, SIMPLE_D, session.getLocale(), session.getTimeZone());
316      }
317
318      @Override /* PojoSwap */
319      public String swap(BeanSession session, Calendar o) throws Exception {
320         return CalendarUtils.serialize(o, SIMPLE_D, session.getLocale(), session.getTimeZone());
321      }
322   }
323
324   /**
325    * Transforms {@link Calendar Calendars} to simple <js>"HH:mm:ss"</js> time strings.
326    *
327    * <h5 class='section'>Example Output:</h5>
328    * <ul>
329    *    <li><js>"10:11:12"</js>
330    * </ul>
331    */
332   public static class TimeSimple extends CalendarSwap {
333
334      @Override /* PojoSwap */
335      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
336         return CalendarUtils.parseCalendar(o, SIMPLE_T, session.getLocale(), session.getTimeZone());
337      }
338
339      @Override /* PojoSwap */
340      public String swap(BeanSession session, Calendar o) throws Exception {
341         return CalendarUtils.serialize(o, SIMPLE_T, session.getLocale(), session.getTimeZone());
342      }
343   }
344
345   /**
346    * Transforms {@link Calendar Calendars} to {@link DateFormat#FULL} date strings.
347    *
348    * <h5 class='section'>Example Output:</h5>
349    * <ul>
350    *    <li><js>"Saturday, March 3, 2001"</js> <jc>// en_US</jc>
351    *    <li><js>"2001年3月3日"</js> <jc>// ja_JP</jc>
352    *    <li><js>"2001년 3월 3일 토요일"</js> <jc>// ko_KR</jc>
353    * </ul>
354    */
355   public static class DateFull extends CalendarSwap {
356
357      @Override /* PojoSwap */
358      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
359         return CalendarUtils.parseCalendar(o, FULL_D, session.getLocale(), session.getTimeZone());
360      }
361
362      @Override /* PojoSwap */
363      public String swap(BeanSession session, Calendar o) throws Exception {
364         return CalendarUtils.serialize(o, FULL_D, session.getLocale(), session.getTimeZone());
365      }
366   }
367
368   /**
369    * Transforms {@link Calendar Calendars} to {@link DateFormat#LONG} date strings.
370    *
371    * <h5 class='section'>Example Output:</h5>
372    * <ul>
373    *    <li><js>"March 3, 2001"</js> <jc>// en_US</jc>
374    *    <li><js>"2001/03/03"</js> <jc>// ja_JP</jc>
375    *    <li><js>"2001년 3월 3일 (토)"</js> <jc>// ko_KR</jc>
376    * </ul>
377    */
378   public static class DateLong extends CalendarSwap {
379
380      @Override /* PojoSwap */
381      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
382         return CalendarUtils.parseCalendar(o, LONG_D, session.getLocale(), session.getTimeZone());
383      }
384
385      @Override /* PojoSwap */
386      public String swap(BeanSession session, Calendar o) throws Exception {
387         return CalendarUtils.serialize(o, LONG_D, session.getLocale(), session.getTimeZone());
388      }
389   }
390
391   /**
392    * Transforms {@link Calendar Calendars} to {@link DateFormat#MEDIUM} date strings.
393    *
394    * <h5 class='section'>Example Output:</h5>
395    * <ul>
396    *    <li><js>"Mar 3, 2001"</js> <jc>// en_US</jc>
397    *    <li><js>"2001/03/03"</js> <jc>// ja_JP</jc>
398    *    <li><js>"2001. 3. 3"</js> <jc>// ko_KR</jc>
399    * </ul>
400    */
401   public static class DateMedium extends CalendarSwap {
402
403      @Override /* PojoSwap */
404      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
405         return CalendarUtils.parseCalendar(o, MEDIUM_D, session.getLocale(), session.getTimeZone());
406      }
407
408      @Override /* PojoSwap */
409      public String swap(BeanSession session, Calendar o) throws Exception {
410         return CalendarUtils.serialize(o, MEDIUM_D, session.getLocale(), session.getTimeZone());
411      }
412   }
413
414   /**
415    * Transforms {@link Calendar Calendars} to {@link DateFormat#SHORT} date strings.
416    *
417    * <h5 class='section'>Example Output:</h5>
418    * <ul>
419    *    <li><js>"3/3/01"</js> <jc>// en_US</jc>
420    *    <li><js>"01/03/03"</js> <jc>// ja_JP</jc>
421    *    <li><js>"01. 3. 3"</js> <jc>// ko_KR</jc>
422    * </ul>
423    */
424   public static class DateShort extends CalendarSwap {
425
426      @Override /* PojoSwap */
427      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
428         return CalendarUtils.parseCalendar(o, SHORT_D, session.getLocale(), session.getTimeZone());
429      }
430
431      @Override /* PojoSwap */
432      public String swap(BeanSession session, Calendar o) throws Exception {
433         return CalendarUtils.serialize(o, SHORT_D, session.getLocale(), session.getTimeZone());
434      }
435   }
436
437   /**
438    * Transforms {@link Calendar Calendars} to {@link DateFormat#FULL} time strings.
439    *
440    * <h5 class='section'>Example Output:</h5>
441    * <ul>
442    *    <li><js>"10:11:12 AM GMT"</js> <jc>// en_US</jc>
443    *    <li><js>"10時11分12秒 GMT"</js> <jc>// ja_JP</jc>
444    *    <li><js>"오전 10시 11분 12초 GMT"</js> <jc>// ko_KR</jc>
445    * </ul>
446    */
447   public static class TimeFull extends CalendarSwap {
448
449      @Override /* PojoSwap */
450      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
451         return CalendarUtils.parseCalendar(o, FULL_T, session.getLocale(), session.getTimeZone());
452      }
453
454      @Override /* PojoSwap */
455      public String swap(BeanSession session, Calendar o) throws Exception {
456         return CalendarUtils.serialize(o, FULL_T, session.getLocale(), session.getTimeZone());
457      }
458   }
459
460   /**
461    * Transforms {@link Calendar Calendars} to {@link DateFormat#LONG} time strings.
462    *
463    * <h5 class='section'>Example Output:</h5>
464    * <ul>
465    *    <li><js>"10:11:12 AM GMT"</js> <jc>// en_US</jc>
466    *    <li><js>"10:11:12 GMT"</js> <jc>// ja_JP</jc>
467    *    <li><js>"오전 10시 11분 12초"</js> <jc>// ko_KR</jc>
468    * </ul>
469    */
470   public static class TimeLong extends CalendarSwap {
471
472      @Override /* PojoSwap */
473      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
474         return CalendarUtils.parseCalendar(o, LONG_T, session.getLocale(), session.getTimeZone());
475      }
476
477      @Override /* PojoSwap */
478      public String swap(BeanSession session, Calendar o) throws Exception {
479         return CalendarUtils.serialize(o, LONG_T, session.getLocale(), session.getTimeZone());
480      }
481   }
482
483   /**
484    * Transforms {@link Calendar Calendars} to {@link DateFormat#MEDIUM} time strings.
485    *
486    * <h5 class='section'>Example Output:</h5>
487    * <ul>
488    *    <li><js>"10:11:12 AM"</js> <jc>// en_US</jc>
489    *    <li><js>"10:11:12"</js> <jc>// ja_JP</jc>
490    *    <li><js>"오전 10:11:12"</js> <jc>// ko_KR</jc>
491    * </ul>
492    */
493   public static class TimeMedium extends CalendarSwap {
494
495      @Override /* PojoSwap */
496      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
497         return CalendarUtils.parseCalendar(o, MEDIUM_T, session.getLocale(), session.getTimeZone());
498      }
499
500      @Override /* PojoSwap */
501      public String swap(BeanSession session, Calendar o) throws Exception {
502         return CalendarUtils.serialize(o, MEDIUM_T, session.getLocale(), session.getTimeZone());
503      }
504   }
505
506   /**
507    * Transforms {@link Calendar Calendars} to {@link DateFormat#SHORT} time strings.
508    *
509    * <h5 class='section'>Example Output:</h5>
510    * <ul>
511    *    <li><js>"10:11 AM"</js> <jc>// en_US</jc>
512    *    <li><js>"10:11 AM"</js> <jc>// ja_JP</jc>
513    *    <li><js>"오전 10:11"</js> <jc>// ko_KR</jc>
514    * </ul>
515    */
516   public static class TimeShort extends CalendarSwap {
517
518      @Override /* PojoSwap */
519      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
520         return CalendarUtils.parseCalendar(o, SHORT_T, session.getLocale(), session.getTimeZone());
521      }
522
523      @Override /* PojoSwap */
524      public String swap(BeanSession session, Calendar o) throws Exception {
525         return CalendarUtils.serialize(o, SHORT_T, session.getLocale(), session.getTimeZone());
526      }
527   }
528
529   /**
530    * Transforms {@link Calendar Calendars} to {@link DateFormat#FULL} date-time strings.
531    *
532    * <h5 class='section'>Example Output:</h5>
533    * <ul>
534    *    <li><js>"Saturday, March 3, 2001 10:11:12 AM GMT"</js> <jc>// en_US</jc>
535    *    <li><js>"2001年3月3日 10時11分12秒 GMT"</js> <jc>// ja_JP</jc>
536    *    <li><js>"2001년 3월 3일 토요일 오전 10시 11분 12초 GMT"</js> <jc>// ko_KR</jc>
537    * </ul>
538    */
539   public static class DateTimeFull extends CalendarSwap {
540
541      @Override /* PojoSwap */
542      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
543         return CalendarUtils.parseCalendar(o, FULL_DT, session.getLocale(), session.getTimeZone());
544      }
545
546      @Override /* PojoSwap */
547      public String swap(BeanSession session, Calendar o) throws Exception {
548         return CalendarUtils.serialize(o, FULL_DT, session.getLocale(), session.getTimeZone());
549      }
550   }
551
552   /**
553    * Transforms {@link Calendar Calendars} to {@link DateFormat#LONG} date-time strings.
554    *
555    * <h5 class='section'>Example Output:</h5>
556    * <ul>
557    *    <li><js>"March 3, 2001 10:11:12 AM GMT"</js> <jc>// en_US</jc>
558    *    <li><js>"2001/03/03 10:11:12 GMT"</js> <jc>// ja_JP</jc>
559    *    <li><js>"2001년 3월 3일 (토) 오전 10시 11분 12초"</js> <jc>// ko_KR</jc>
560    * </ul>
561    */
562   public static class DateTimeLong extends CalendarSwap {
563
564      @Override /* PojoSwap */
565      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
566         return CalendarUtils.parseCalendar(o, LONG_DT, session.getLocale(), session.getTimeZone());
567      }
568
569      @Override /* PojoSwap */
570      public String swap(BeanSession session, Calendar o) throws Exception {
571         return CalendarUtils.serialize(o, LONG_DT, session.getLocale(), session.getTimeZone());
572      }
573   }
574
575   /**
576    * Transforms {@link Calendar Calendars} to {@link DateFormat#MEDIUM} date-time strings.
577    *
578    * <h5 class='section'>Example Output:</h5>
579    * <ul>
580    *    <li><js>"Mar 3, 2001 10:11:12 AM"</js> <jc>// en_US</jc>
581    *    <li><js>"2001/03/03 10:11:12"</js> <jc>// ja_JP</jc>
582    *    <li><js>"2001. 3. 3 오전 10:11:12"</js> <jc>// ko_KR</jc>
583    * </ul>
584    */
585   public static class DateTimeMedium extends CalendarSwap {
586
587      @Override /* PojoSwap */
588      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
589         return CalendarUtils.parseCalendar(o, MEDIUM_DT, session.getLocale(), session.getTimeZone());
590      }
591
592      @Override /* PojoSwap */
593      public String swap(BeanSession session, Calendar o) throws Exception {
594         return CalendarUtils.serialize(o, MEDIUM_DT, session.getLocale(), session.getTimeZone());
595      }
596   }
597
598   /**
599    * Transforms {@link Calendar Calendars} to {@link DateFormat#SHORT} date-time strings.
600    *
601    * <h5 class='section'>Example Output:</h5>
602    * <ul>
603    *    <li><js>"3/3/01 10:11 AM"</js> <jc>// en_US</jc>
604    *    <li><js>"01/03/03 10:11"</js> <jc>// ja_JP</jc>
605    *    <li><js>"01. 3. 3 오전 10:11"</js> <jc>// ko_KR</jc>
606    * </ul>
607    */
608   public static class DateTimeShort extends CalendarSwap {
609
610      @Override /* PojoSwap */
611      public Calendar unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
612         return CalendarUtils.parseCalendar(o, SHORT_DT, session.getLocale(), session.getTimeZone());
613      }
614
615      @Override /* PojoSwap */
616      public String swap(BeanSession session, Calendar o) throws Exception {
617         return CalendarUtils.serialize(o, SHORT_DT, session.getLocale(), session.getTimeZone());
618      }
619   }
620
621   static final Calendar convert(Calendar in, ClassMeta<?> hint) throws ParseException {
622      try {
623         if (hint.isInstance(in) || ! hint.canCreateNewInstance())
624            return in;
625         Calendar c = (Calendar)hint.newInstance();
626         c.setTime(in.getTime());
627         c.setTimeZone(in.getTimeZone());
628         return c;
629      } catch (Exception e) {
630         throw new ParseException(e);
631      }
632   }
633
634   static final Calendar convert(Date in, ClassMeta<?> hint, BeanSession session) throws ParseException {
635      try {
636         if (hint == null || ! hint.canCreateNewInstance())
637            hint = session.getClassMeta(GregorianCalendar.class);
638         Calendar c = (Calendar)hint.newInstance();
639         c.setTime(in);
640         return c;
641      } catch (Exception e) {
642         throw new ParseException(e);
643      }
644   }
645}