Java Code Examples for org.joda.time.format.DateTimeFormatter#withChronology()

The following examples show how to use org.joda.time.format.DateTimeFormatter#withChronology() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LimitChronology.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public String getMessage() {
    StringBuffer buf = new StringBuffer(85);
    buf.append("The");
    String desc = super.getMessage();
    if (desc != null) {
        buf.append(' ');
        buf.append(desc);
    }
    buf.append(" instant is ");

    DateTimeFormatter p = ISODateTimeFormat.dateTime();
    p = p.withChronology(getBase());
    if (iIsLow) {
        buf.append("below the supported minimum of ");
        p.printTo(buf, getLowerLimit().getMillis());
    } else {
        buf.append("above the supported maximum of ");
        p.printTo(buf, getUpperLimit().getMillis());
    }
    
    buf.append(" (");
    buf.append(getBase());
    buf.append(')');

    return buf.toString();
}
 
Example 2
Source File: LimitChronology.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public String getMessage() {
    StringBuffer buf = new StringBuffer(85);
    buf.append("The");
    String desc = super.getMessage();
    if (desc != null) {
        buf.append(' ');
        buf.append(desc);
    }
    buf.append(" instant is ");

    DateTimeFormatter p = ISODateTimeFormat.dateTime();
    p = p.withChronology(getBase());
    if (iIsLow) {
        buf.append("below the supported minimum of ");
        p.printTo(buf, getLowerLimit().getMillis());
    } else {
        buf.append("above the supported maximum of ");
        p.printTo(buf, getUpperLimit().getMillis());
    }
    
    buf.append(" (");
    buf.append(getBase());
    buf.append(')');

    return buf.toString();
}
 
Example 3
Source File: Configuration.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the date format used to string'ify SAML's {@link org.joda.time.DateTime} objects.
 * 
 * @return date format used to string'ify date objects
 */
public static DateTimeFormatter getSAMLDateFormatter() {
    if (dateFormatter == null) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern(defaultDateFormat);
        dateFormatter = formatter.withChronology(ISOChronology.getInstanceUTC());
    }

    return dateFormatter;
}
 
Example 4
Source File: TimeInterval.java    From FROST-Server with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public String asISO8601() {
    DateTimeFormatter printer = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
    printer = printer.withChronology(interval.getChronology());
    StringBuilder timeString = new StringBuilder(48);
    printer.printTo(timeString, interval.getStartMillis());
    timeString.append('/');
    printer.printTo(timeString, interval.getEndMillis());
    return timeString.toString();
}
 
Example 5
Source File: Helper.java    From ETSMobile-Android2 with Apache License 2.0 5 votes vote down vote up
public static DateTime ConvertFromWebService(String strDate) {
	DateTimeFormatter parser1 = new DateTimeFormatterBuilder().append(ISODateTimeFormat.date()).appendLiteral('T')
			.append(ISODateTimeFormat.hourMinuteSecond()).appendOptional(fractionElement())
			.appendOptional(offsetElement()).toFormatter().withZone(DateTimeZone.UTC);
	parser1.withChronology(ISOChronology.getInstanceUTC());
	return parser1.parseDateTime(strDate);
}
 
Example 6
Source File: AbstractInterval.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Output a string in ISO8601 interval format.
 * <p>
 * From version 2.1, the string includes the time zone offset.
 *
 * @return re-parsable string (in the default zone)
 */
public String toString() {
    DateTimeFormatter printer = ISODateTimeFormat.dateTime();
    printer = printer.withChronology(getChronology());
    StringBuffer buf = new StringBuffer(48);
    printer.printTo(buf, getStartMillis());
    buf.append('/');
    printer.printTo(buf, getEndMillis());
    return buf.toString();
}
 
Example 7
Source File: AbstractInterval.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Output a string in ISO8601 interval format.
 * <p>
 * From version 2.1, the string includes the time zone offset.
 *
 * @return re-parsable string (in the default zone)
 */
public String toString() {
    DateTimeFormatter printer = ISODateTimeFormat.dateTime();
    printer = printer.withChronology(getChronology());
    StringBuffer buf = new StringBuffer(48);
    printer.printTo(buf, getStartMillis());
    buf.append('/');
    printer.printTo(buf, getEndMillis());
    return buf.toString();
}
 
Example 8
Source File: AbstractJodaProcessorBuilder.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * 変換規則から、{@link DateTimeFormatter}のインスタンスを作成する。
 * <p>アノテーション{@link CsvDateTimeFormat}が付与されていない場合は、各種タイプごとの標準の書式で作成する。</p>
 * @param field フィールド情報
 * @param config システム設定
 * @return {@link DateTimeFormatter}のインスタンス。
 */
protected DateTimeFormatter createFormatter(final FieldAccessor field, final Configuration config) {
    
    final Optional<CsvDateTimeFormat> formatAnno = field.getAnnotation(CsvDateTimeFormat.class);
    if(!formatAnno.isPresent()) {
        return DateTimeFormat.forPattern(getDefaultPattern());
    }
    
    String pattern = formatAnno.get().pattern();
    if(pattern.isEmpty()) {
        pattern = getDefaultPattern();
    }
    
    final Locale locale = Utils.getLocale(formatAnno.get().locale());
    final DateTimeZone zone = formatAnno.get().timezone().isEmpty() ? DateTimeZone.getDefault()
            : DateTimeZone.forTimeZone(TimeZone.getTimeZone(formatAnno.get().timezone()));
    
    final DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern)
            .withLocale(locale)
            .withZone(zone);
    
    final boolean lenient = formatAnno.get().lenient();
    if(lenient) {
        Chronology chronology = LenientChronology.getInstance(ISOChronology.getInstance());
        return formatter.withChronology(chronology);
        
    } else {
        return formatter;
    }
    
}
 
Example 9
Source File: StringConverter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the value of the mutable interval from the string.
 * 
 * @param writableInterval  the interval to set
 * @param object  the String to convert, must not be null
 * @param chrono  the chronology to use, may be null
 */
public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) {
    String str = (String) object;

    int separator = str.indexOf('/');
    if (separator < 0) {
        throw new IllegalArgumentException("Format requires a '/' separator: " + str);
    }

    String leftStr = str.substring(0, separator);
    if (leftStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    String rightStr = str.substring(separator + 1);
    if (rightStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }

    DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();
    dateTimeParser = dateTimeParser.withChronology(chrono);
    PeriodFormatter periodParser = ISOPeriodFormat.standard();
    long startInstant = 0, endInstant = 0;
    Period period = null;
    Chronology parsedChrono = null;
    
    // before slash
    char c = leftStr.charAt(0);
    if (c == 'P' || c == 'p') {
        period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr);
    } else {
        DateTime start = dateTimeParser.parseDateTime(leftStr);
        startInstant = start.getMillis();
        parsedChrono = start.getChronology();
    }
    
    // after slash
    c = rightStr.charAt(0);
    if (c == 'P' || c == 'p') {
        if (period != null) {
            throw new IllegalArgumentException("Interval composed of two durations: " + str);
        }
        period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr);
        chrono = (chrono != null ? chrono : parsedChrono);
        endInstant = chrono.add(period, startInstant, 1);
    } else {
        DateTime end = dateTimeParser.parseDateTime(rightStr);
        endInstant = end.getMillis();
        parsedChrono = (parsedChrono != null ? parsedChrono : end.getChronology());
        chrono = (chrono != null ? chrono : parsedChrono);
        if (period != null) {
            startInstant = chrono.add(period, endInstant, -1);
        }
    }
    
    writableInterval.setInterval(startInstant, endInstant);
    writableInterval.setChronology(chrono);
}
 
Example 10
Source File: StringConverter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the value of the mutable interval from the string.
 * 
 * @param writableInterval  the interval to set
 * @param object  the String to convert, must not be null
 * @param chrono  the chronology to use, may be null
 */
public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) {
    String str = (String) object;

    int separator = str.indexOf('/');
    if (separator < 0) {
        throw new IllegalArgumentException("Format requires a '/' separator: " + str);
    }

    String leftStr = str.substring(0, separator);
    if (leftStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    String rightStr = str.substring(separator + 1);
    if (rightStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }

    DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();
    dateTimeParser = dateTimeParser.withChronology(chrono);
    PeriodFormatter periodParser = ISOPeriodFormat.standard();
    long startInstant = 0, endInstant = 0;
    Period period = null;
    Chronology parsedChrono = null;
    
    // before slash
    char c = leftStr.charAt(0);
    if (c == 'P' || c == 'p') {
        period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr);
    } else {
        DateTime start = dateTimeParser.parseDateTime(leftStr);
        startInstant = start.getMillis();
        parsedChrono = start.getChronology();
    }
    
    // after slash
    c = rightStr.charAt(0);
    if (c == 'P' || c == 'p') {
        if (period != null) {
            throw new IllegalArgumentException("Interval composed of two durations: " + str);
        }
        period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr);
        chrono = (chrono != null ? chrono : parsedChrono);
        endInstant = chrono.add(period, startInstant, 1);
    } else {
        DateTime end = dateTimeParser.parseDateTime(rightStr);
        endInstant = end.getMillis();
        parsedChrono = (parsedChrono != null ? parsedChrono : end.getChronology());
        chrono = (chrono != null ? chrono : parsedChrono);
        if (period != null) {
            startInstant = chrono.add(period, endInstant, -1);
        }
    }
    
    writableInterval.setInterval(startInstant, endInstant);
    writableInterval.setChronology(chrono);
}
 
Example 11
Source File: Configuration.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Sets the date format used to string'ify SAML's date/time objects.
 * 
 * See the
 * {@link <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat</a>}
 * documentation for format syntax.
 * 
 * @param format date format used to string'ify date objects
 */
public static void setSAMLDateFormat(String format) {
    DateTimeFormatter formatter = DateTimeFormat.forPattern(format);
    dateFormatter = formatter.withChronology(ISOChronology.getInstanceUTC());
}