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.
// ***************************************************************************************************************************
// * 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 Date Dates} 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 DateSwap extends StringSwap {
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return convert(CalendarUtils.parseDate(o, TO_STRING, session.getLocale(), session.getTimeZone()), hint);
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, TO_STRING, session.getLocale(), session.getTimeZone());
}
}
/**
* 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return convert(CalendarUtils.parseDate(o, ISO8601_DT, session.getLocale(), session.getTimeZone()), hint);
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, ISO8601_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return convert(CalendarUtils.parseDate(o, ISO8601_DTL, session.getLocale(), session.getTimeZone()), hint);
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, ISO8601_DTZ, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, ISO8601_DTZ, session.getLocale(), session.getTimeZone());
}
}
/**
* Same as {@link CalendarSwap.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, Date o) throws Exception {
return CalendarUtils.serialize(o, ISO8601_DTP, session.getLocale(), session.getTimeZone());
}
}
/**
* Same as {@link CalendarSwap.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, Date 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, ISO8601_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, ISO8601_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, RFC2822_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, RFC2822_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Same as {@link DateSwap.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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, RFC2822_DTZ, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, RFC2822_DTZ, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, RFC2822_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, RFC2822_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} to simple "yyyy/MM/dd HH:mm:ss" date-time strings.
*
*
Example output:
*
*
"2001/03/03 10:11:12"
*
*/
public static class DateTimeSimple extends DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, SIMPLE_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, SIMPLE_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} to simple "yyyy/MM/dd" date strings.
*
*
Example output:
*
*
"2001/03/03"
*
*/
public static class DateSimple extends DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, SIMPLE_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, SIMPLE_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} to simple "HH:mm:ss" time strings.
*
*
Example output:
*
*
"10:11:12"
*
*/
public static class TimeSimple extends DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, SIMPLE_T, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, SIMPLE_T, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, FULL_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, FULL_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, LONG_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, LONG_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, MEDIUM_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, MEDIUM_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, SHORT_D, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, SHORT_D, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, FULL_T, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, FULL_T, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, LONG_T, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, LONG_T, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, MEDIUM_T, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, MEDIUM_T, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, SHORT_T, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, SHORT_T, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, FULL_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, FULL_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, LONG_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, LONG_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, MEDIUM_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, MEDIUM_DT, session.getLocale(), session.getTimeZone());
}
}
/**
* Transforms {@link Date Dates} 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 DateSwap {
@Override /* PojoSwap */
public Date unswap(BeanSession session, String o, ClassMeta> hint) throws Exception {
return CalendarUtils.parseDate(o, SHORT_DT, session.getLocale(), session.getTimeZone());
}
@Override /* PojoSwap */
public String swap(BeanSession session, Date o) throws Exception {
return CalendarUtils.serialize(o, SHORT_DT, session.getLocale(), session.getTimeZone());
}
}
private static Date convert(Date in, ClassMeta> hint) throws Exception {
if (in == null)
return null;
if (hint == null || hint.isInstance(in))
return in;
Class> c = hint.getInnerClass();
if (c == java.util.Date.class)
return in;
if (c == java.sql.Date.class)
return new java.sql.Date(in.getTime());
if (c == java.sql.Time.class)
return new java.sql.Time(in.getTime());
if (c == java.sql.Timestamp.class)
return new java.sql.Timestamp(in.getTime());
throw new ParseException("DateSwap is unable to narrow object of type ''{0}''", c);
}
}