Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.apache.juneau.transforms.CalendarSwap Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.transforms;
import static org.apache.juneau.utils.CalendarUtils.Format.*;
import java.text.*;
import java.util.*;
import org.apache.juneau.*;
import org.apache.juneau.parser.ParseException;
import org.apache.juneau.transform.*;
import org.apache.juneau.utils.*;
/**
* Transforms {@link Calendar Calendars} to {@link String Strings}.
*
* Behavior-specific subclasses
*
* The following direct subclasses are provided for convenience to the following formats:
*
* {@link ToString} - To {@link String Strings} using the {@code Date.toString()} method.
* {@link ISO8601DT} - To ISO8601 date-time strings.
* {@link ISO8601DTZ} - Same as {@link ISO8601DT}, except always serializes in GMT.
* {@link ISO8601DTP} - Same as {@link ISO8601DT} except with millisecond precision.
* {@link ISO8601DTPZ} - Same as {@link ISO8601DTZ} except with millisecond precision.
* {@link RFC2822DT} - To RFC2822 date-time strings.
* {@link RFC2822DTZ} - Same as {@link RFC2822DT}, except always serializes in GMT.
* {@link RFC2822D} - To RFC2822 date strings.
* {@link DateTimeSimple} - To simple "yyyy/MM/dd HH:mm:ss" date-time strings.
* {@link DateSimple} - To simple "yyyy/MM/dd" date strings.
* {@link TimeSimple} - To simple "HH:mm:ss" time strings.
* {@link DateFull} - To {@link DateFormat#FULL} date strings.
* {@link DateLong} - To {@link DateFormat#LONG} date strings.
* {@link DateMedium} - To {@link DateFormat#MEDIUM} date strings.
* {@link DateShort} - To {@link DateFormat#SHORT} date strings.
* {@link TimeFull} - To {@link DateFormat#FULL} time strings.
* {@link TimeLong} - To {@link DateFormat#LONG} time strings.
* {@link TimeMedium} - To {@link DateFormat#MEDIUM} time strings.
* {@link TimeShort} - To {@link DateFormat#SHORT} time strings.
* {@link DateTimeFull} - To {@link DateFormat#FULL} date-time strings.
* {@link DateTimeLong} - To {@link DateFormat#LONG} date-time strings.
* {@link DateTimeMedium} - To {@link DateFormat#MEDIUM} date-time strings.
* {@link DateTimeShort} - To {@link DateFormat#SHORT} date-time strings.
*
*/
public class CalendarSwap extends StringSwap {
/**
* Transforms {@link Calendar Calendars} to {@link String Strings} using the {@code Date.toString()} method.
*
* Example Output:
*
* "Wed Jul 04 15:30:45 EST 2001"
*
*/
public static class ToString extends CalendarSwap {
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, TO_STRING, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return convert(CalendarUtils.parseDate(o, TO_STRING, session.getLocale(), session.getTimeZone()), hint, session);
}
}
/**
* Transforms {@link Calendar Calendars} to ISO8601 date-time strings.
*
* Example Output:
*
* "2001-07-04T15:30:45-05:00"
* "2001-07-04T15:30:45Z"
*
*
* Example input:
*
* "2001-07-04T15:30:45-05:00"
* "2001-07-04T15:30:45Z"
* "2001-07-04T15:30:45.1Z"
* "2001-07-04T15:30Z"
* "2001-07-04"
* "2001-07"
* "2001"
*
*/
public static class ISO8601DT extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, ISO8601_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, ISO8601_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to ISO8601 date-time-local strings.
*
* Example Output:
*
* "2001-07-04T15:30:45"
*
*
* Example input:
*
* "2001-07-04T15:30:45"
* "2001-07-04T15:30:45.1"
* "2001-07-04T15:30"
* "2001-07-04"
* "2001-07"
* "2001"
*
*/
public static class ISO8601DTL extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return convert(CalendarUtils.parseCalendar(o, ISO8601_DTL, session.getLocale(), session.getTimeZone()), hint);
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, ISO8601_DTL, session.getLocale(), session.getTimeZone());
}
}
/**
* Same as {@link ISO8601DT}, except always serializes in GMT.
*
* Example Output:
* "2001-07-04T15:30:45Z"
*/
public static class ISO8601DTZ extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, ISO8601_DTZ, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, ISO8601_DTZ, session.getLocale(), session.getTimeZone());
}
}
/**
* Same as {@link ISO8601DT} except serializes to millisecond precision.
*
* Example Output:
* "2001-07-04T15:30:45.123Z"
*/
public static class ISO8601DTP extends ISO8601DT {
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, ISO8601_DTP, session.getLocale(), session.getTimeZone());
}
}
/**
* Same as {@link ISO8601DTZ} except serializes to millisecond precision.
*
* Example Output:
* "2001-07-04T15:30:45.123"
*/
public static class ISO8601DTPZ extends ISO8601DTZ {
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, ISO8601_DTPZ, session.getLocale(), session.getTimeZone());
}
}
/**
* ISO8601 date only.
*
* Example Output:
* "2001-07-04"
*/
public static class ISO8601D extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, ISO8601_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, ISO8601_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to RFC2822 date-time strings.
*
* Example Output:
*
* "Sat, 03 Mar 2001 10:11:12 +0000" // en_US
* "土, 03 3 2001 10:11:12 +0000" // ja_JP
* "토, 03 3월 2001 10:11:12 +0000" // ko_KR
*
*/
public static class RFC2822DT extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, RFC2822_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, RFC2822_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Same as {@link RFC2822DT}, except always serializes in GMT.
*
* Example Output:
*
* "Sat, 03 Mar 2001 10:11:12 GMT" // en_US
* "土, 03 3 2001 10:11:12 GMT" // ja_JP
* "토, 03 3월 2001 10:11:12 GMT" // ko_KR
*
*/
public static class RFC2822DTZ extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, RFC2822_DTZ, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, RFC2822_DTZ, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to RFC2822 date strings.
*
* Example Output:
*
* "03 Mar 2001" // en_US
* "03 3 2001" // ja_JP
* "03 3월 2001" // ko_KR
*
*/
public static class RFC2822D extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, RFC2822_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, RFC2822_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to simple "yyyy/MM/dd HH:mm:ss" date-time strings.
*
* Example Output:
*
* "2001/03/03 10:11:12"
*
*/
public static class DateTimeSimple extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, SIMPLE_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, SIMPLE_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to simple "yyyy/MM/dd" date strings.
*
* Example Output:
*
*/
public static class DateSimple extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, SIMPLE_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, SIMPLE_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to simple "HH:mm:ss" time strings.
*
* Example Output:
*
*/
public static class TimeSimple extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, SIMPLE_T, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, SIMPLE_T, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#FULL} date strings.
*
* Example Output:
*
* "Saturday, March 3, 2001" // en_US
* "2001年3月3日" // ja_JP
* "2001년 3월 3일 토요일" // ko_KR
*
*/
public static class DateFull extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, FULL_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, FULL_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#LONG} date strings.
*
* Example Output:
*
* "March 3, 2001" // en_US
* "2001/03/03" // ja_JP
* "2001년 3월 3일 (토)" // ko_KR
*
*/
public static class DateLong extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, LONG_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, LONG_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#MEDIUM} date strings.
*
* Example Output:
*
* "Mar 3, 2001" // en_US
* "2001/03/03" // ja_JP
* "2001. 3. 3" // ko_KR
*
*/
public static class DateMedium extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, MEDIUM_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, MEDIUM_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#SHORT} date strings.
*
* Example Output:
*
* "3/3/01" // en_US
* "01/03/03" // ja_JP
* "01. 3. 3" // ko_KR
*
*/
public static class DateShort extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, SHORT_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, SHORT_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#FULL} time strings.
*
* Example Output:
*
* "10:11:12 AM GMT" // en_US
* "10時11分12秒 GMT" // ja_JP
* "오전 10시 11분 12초 GMT" // ko_KR
*
*/
public static class TimeFull extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, FULL_T, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, FULL_T, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#LONG} time strings.
*
* Example Output:
*
* "10:11:12 AM GMT" // en_US
* "10:11:12 GMT" // ja_JP
* "오전 10시 11분 12초" // ko_KR
*
*/
public static class TimeLong extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, LONG_T, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, LONG_T, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#MEDIUM} time strings.
*
* Example Output:
*
* "10:11:12 AM" // en_US
* "10:11:12" // ja_JP
* "오전 10:11:12" // ko_KR
*
*/
public static class TimeMedium extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, MEDIUM_T, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, MEDIUM_T, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#SHORT} time strings.
*
* Example Output:
*
* "10:11 AM" // en_US
* "10:11 AM" // ja_JP
* "오전 10:11" // ko_KR
*
*/
public static class TimeShort extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, SHORT_T, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, SHORT_T, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#FULL} date-time strings.
*
* Example Output:
*
* "Saturday, March 3, 2001 10:11:12 AM GMT" // en_US
* "2001年3月3日 10時11分12秒 GMT" // ja_JP
* "2001년 3월 3일 토요일 오전 10시 11분 12초 GMT" // ko_KR
*
*/
public static class DateTimeFull extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, FULL_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, FULL_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#LONG} date-time strings.
*
* Example Output:
*
* "March 3, 2001 10:11:12 AM GMT" // en_US
* "2001/03/03 10:11:12 GMT" // ja_JP
* "2001년 3월 3일 (토) 오전 10시 11분 12초" // ko_KR
*
*/
public static class DateTimeLong extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, LONG_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, LONG_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#MEDIUM} date-time strings.
*
* Example Output:
*
* "Mar 3, 2001 10:11:12 AM" // en_US
* "2001/03/03 10:11:12" // ja_JP
* "2001. 3. 3 오전 10:11:12" // ko_KR
*
*/
public static class DateTimeMedium extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, MEDIUM_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, MEDIUM_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Calendar Calendars} to {@link DateFormat#SHORT} date-time strings.
*
* Example Output:
*
* "3/3/01 10:11 AM" // en_US
* "01/03/03 10:11" // ja_JP
* "01. 3. 3 오전 10:11" // ko_KR
*
*/
public static class DateTimeShort extends CalendarSwap {
@Override /* PojoSwap */
public Calendar unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseCalendar(o, SHORT_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Calendar o) throws Exception {
return CalendarUtils.serialize(o, SHORT_DT, session.getLocale(), session.getTimeZone());
}
}
static final Calendar convert(Calendar in, ClassMeta> hint) throws ParseException {
try {
if (hint.isInstance(in) || ! hint.canCreateNewInstance())
return in;
Calendar c = (Calendar)hint.newInstance();
c.setTime(in.getTime());
c.setTimeZone(in.getTimeZone());
return c;
} catch (Exception e) {
throw new ParseException(e);
}
}
static final Calendar convert(Date in, ClassMeta> hint, BeanSession session) throws ParseException {
try {
if (hint == null || ! hint.canCreateNewInstance())
hint = session.getClassMeta(GregorianCalendar.class);
Calendar c = (Calendar)hint.newInstance();
c.setTime(in);
return c;
} catch (Exception e) {
throw new ParseException(e);
}
}
}