Example usage for java.text SimpleDateFormat set2DigitYearStart

List of usage examples for java.text SimpleDateFormat set2DigitYearStart

Introduction

In this page you can find the example usage for java.text SimpleDateFormat set2DigitYearStart.

Prototype

public void set2DigitYearStart(Date startDate) 

Source Link

Document

Sets the 100-year period 2-digit years will be interpreted as being in to begin on the date the user specifies.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    SimpleDateFormat formatter = new SimpleDateFormat("yy");
    formatter.set2DigitYearStart(new Date());

    System.out.println(formatter.format(new Date()));
}

From source file:com.ccc.mail.mailinglist.utils.DateUtils.java

/**
 * Parses the date value using the given date formats.
 *
 * @param dateValue the date value to parse
 * @param dateFormats the date formats to use
 * @param startDate During parsing, two digit years will be placed in the range
 * <code>startDate</code> to <code>startDate + 100 years</code>. This value may
 * be <code>null</code>. When <code>null</code> is given as a parameter, year
 * <code>2000</code> will be used.
 *
 * @return the parsed date// w  ww  .  java2  s  .  c o  m
 *
 * @throws DateParseException if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(String dateValue, String[] dateFormats, Date startDate) throws DateParseException {

    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_PATTERNS;
    }
    if (startDate == null) {
        startDate = DEFAULT_TWO_DIGIT_YEAR_START;
    }
    // trim single quotes around date if present
    // see issue #5279
    if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) {
        dateValue = dateValue.substring(1, dateValue.length() - 1);
    }

    for (String dateFormat : dateFormats) {
        SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormat);
        dateParser.set2DigitYearStart(startDate);

        try {
            return dateParser.parse(dateValue);
        } catch (ParseException pe) {
            // ignore this exception, we will try the next format
        }
    }

    // we were unable to parse the date
    throw new DateParseException("Unable to parse the date " + dateValue);
}

From source file:DateUtil.java

/**
 * Parses the date value using the given date formats.
 *
 * @param dateValue   the date value to parse
 * @param dateFormats the date formats to use
 * @param startDate   During parsing, two digit years will be placed in the range
 *                    <code>startDate</code> to <code>startDate + 100 years</code>. This value may
 *                    be <code>null</code>. When <code>null</code> is given as a parameter, year
 *                    <code>2000</code> will be used.
 * @return the parsed date/*  w  ww. j  a  v  a 2s  .co  m*/
 * @throws DateParseException if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(String dateValue, Collection<String> dateFormats, Date startDate) {

    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_PATTERNS;
    }
    if (startDate == null) {
        startDate = DEFAULT_TWO_DIGIT_YEAR_START;
    }
    // trim single quotes around date if present
    // see issue #5279
    if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) {
        dateValue = dateValue.substring(1, dateValue.length() - 1);
    }

    SimpleDateFormat dateParser = null;
    for (String format : dateFormats) {
        if (dateParser == null) {
            dateParser = new SimpleDateFormat(format, Locale.US);
            dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
            dateParser.set2DigitYearStart(startDate);
        } else {
            dateParser.applyPattern(format);
        }
        try {
            return dateParser.parse(dateValue);
        } catch (ParseException pe) {
            // ignore this exception, we will try the next format
        }
    }

    // we were unable to parse the date
    throw new RuntimeException("Unable to parse the date " + dateValue);
}

From source file:net.hasor.search.utils.DateUtil.java

/**
 * Slightly modified from org.apache.commons.httpclient.util.DateUtil.parseDate
 * <p/>//from w w  w . j  a v a2s .c o  m
 * Parses the date value using the given date formats.
 *
 * @param dateValue   the date value to parse
 * @param dateFormats the date formats to use
 * @param startDate   During parsing, two digit years will be placed in the range
 *                    <code>startDate</code> to <code>startDate + 100 years</code>. This value may
 *                    be <code>null</code>. When <code>null</code> is given as a parameter, year
 *                    <code>2000</code> will be used.
 * @return the parsed date
 * @throws ParseException if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(String dateValue, Collection<String> dateFormats, Date startDate)
        throws ParseException {
    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_HTTP_CLIENT_PATTERNS;
    }
    if (startDate == null) {
        startDate = DEFAULT_TWO_DIGIT_YEAR_START;
    }
    // trim single quotes around date if present
    // see issue #5279
    if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) {
        dateValue = dateValue.substring(1, dateValue.length() - 1);
    }
    SimpleDateFormat dateParser = null;
    Iterator formatIter = dateFormats.iterator();
    while (formatIter.hasNext()) {
        String format = (String) formatIter.next();
        if (dateParser == null) {
            dateParser = new SimpleDateFormat(format, Locale.ROOT);
            dateParser.setTimeZone(GMT);
            dateParser.set2DigitYearStart(startDate);
        } else {
            dateParser.applyPattern(format);
        }
        try {
            return dateParser.parse(dateValue);
        } catch (ParseException pe) {
            // ignore this exception, we will try the next format
        }
    }
    // we were unable to parse the date
    throw new ParseException("Unable to parse the date " + dateValue, 0);
}

From source file:com.cloudlbs.core.utils.DateUtil.java

/**
 * Slightly modified from// w ww . j  a  v a  2 s  .  c o m
 * org.apache.commons.httpclient.util.DateUtil.parseDate
 * <p/>
 * Parses the date value using the given date formats.
 * 
 * @param dateValue
 *            the date value to parse
 * @param dateFormats
 *            the date formats to use
 * @param startDate
 *            During parsing, two digit years will be placed in the range
 *            <code>startDate</code> to <code>startDate + 100 years</code>.
 *            This value may be <code>null</code>. When <code>null</code> is
 *            given as a parameter, year <code>2000</code> will be used.
 * @return the parsed date
 * @throws ParseException
 *             if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(String dateValue, Collection<String> dateFormats, Date startDate)
        throws ParseException {

    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_HTTP_CLIENT_PATTERNS;
    }
    if (startDate == null) {
        startDate = DEFAULT_TWO_DIGIT_YEAR_START;
    }
    // trim single quotes around date if present
    // see issue #5279
    if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) {
        dateValue = dateValue.substring(1, dateValue.length() - 1);
    }

    SimpleDateFormat dateParser = null;
    Iterator<String> formatIter = dateFormats.iterator();

    while (formatIter.hasNext()) {
        String format = (String) formatIter.next();
        if (dateParser == null) {
            dateParser = new SimpleDateFormat(format, Locale.US);
            dateParser.setTimeZone(GMT);
            dateParser.set2DigitYearStart(startDate);
        } else {
            dateParser.applyPattern(format);
        }
        try {
            return dateParser.parse(dateValue);
        } catch (ParseException pe) {
            // ignore this exception, we will try the next format
        }
    }

    // we were unable to parse the date
    throw new ParseException("Unable to parse the date " + dateValue, 0);
}

From source file:org.apache.logging.log4j.core.util.datetime.FastDateParserTest.java

private void validateSdfFormatFdpParseEquality(final String format, final Locale locale, final TimeZone tz,
        final DateParser fdp, final Date in, final int year, final Date cs) throws ParseException {
    final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
    sdf.setTimeZone(tz);//from   www.  j  a v  a2s  .c  om
    if (format.equals(SHORT_FORMAT)) {
        sdf.set2DigitYearStart(cs);
    }
    final String fmt = sdf.format(in);
    try {
        final Date out = fdp.parse(fmt);
        assertEquals(locale.toString() + " " + in + " " + format + " " + tz.getID(), in, out);
    } catch (final ParseException pe) {
        if (year >= 1868 || !locale.getCountry().equals("JP")) {// LANG-978
            throw pe;
        }
    }
}

From source file:org.apache.solr.common.util.DateUtil.java

/**
 * Slightly modified from org.apache.commons.httpclient.util.DateUtil.parseDate
 * <p/>//from  w ww .  j  av  a2s.co  m
 * Parses the date value using the given date formats.
 *
 * @param dateValue   the date value to parse
 * @param dateFormats the date formats to use
 * @param startDate   During parsing, two digit years will be placed in the range
 *                    <code>startDate</code> to <code>startDate + 100 years</code>. This value may
 *                    be <code>null</code>. When <code>null</code> is given as a parameter, year
 *                    <code>2000</code> will be used.
 * @return the parsed date
 * @throws ParseException if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(String dateValue, Collection<String> dateFormats, Date startDate)
        throws ParseException {

    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_HTTP_CLIENT_PATTERNS;
    }
    if (startDate == null) {
        startDate = DEFAULT_TWO_DIGIT_YEAR_START;
    }
    // trim single quotes around date if present
    // see issue #5279
    if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) {
        dateValue = dateValue.substring(1, dateValue.length() - 1);
    }

    SimpleDateFormat dateParser = null;
    Iterator formatIter = dateFormats.iterator();

    while (formatIter.hasNext()) {
        String format = (String) formatIter.next();
        if (dateParser == null) {
            dateParser = new SimpleDateFormat(format, Locale.US);
            dateParser.setTimeZone(GMT);
            dateParser.set2DigitYearStart(startDate);
        } else {
            dateParser.applyPattern(format);
        }
        try {
            return dateParser.parse(dateValue);
        } catch (ParseException pe) {
            // ignore this exception, we will try the next format
        }
    }

    // we were unable to parse the date
    throw new ParseException("Unable to parse the date " + dateValue, 0);
}

From source file:org.apache.solr.handler.extraction.ExtractionDateUtil.java

/**
 * Slightly modified from org.apache.commons.httpclient.util.DateUtil.parseDate
 * <p>/*from   w ww. j a  v  a 2  s. c om*/
 * Parses the date value using the given date formats.
 *
 * @param dateValue   the date value to parse
 * @param dateFormats the date formats to use
 * @param startDate   During parsing, two digit years will be placed in the range
 *                    <code>startDate</code> to <code>startDate + 100 years</code>. This value may
 *                    be <code>null</code>. When <code>null</code> is given as a parameter, year
 *                    <code>2000</code> will be used.
 * @return the parsed date
 * @throws ParseException if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(String dateValue, Collection<String> dateFormats, Date startDate)
        throws ParseException {

    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_HTTP_CLIENT_PATTERNS;
    }
    if (startDate == null) {
        startDate = DEFAULT_TWO_DIGIT_YEAR_START;
    }
    // trim single quotes around date if present
    // see issue #5279
    if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) {
        dateValue = dateValue.substring(1, dateValue.length() - 1);
    }

    //TODO upgrade to Java 8 DateTimeFormatter. But how to deal with the GMT as a default?
    SimpleDateFormat dateParser = null;
    Iterator formatIter = dateFormats.iterator();

    while (formatIter.hasNext()) {
        String format = (String) formatIter.next();
        if (dateParser == null) {
            dateParser = new SimpleDateFormat(format, Locale.ENGLISH);
            dateParser.setTimeZone(GMT);
            dateParser.set2DigitYearStart(startDate);
        } else {
            dateParser.applyPattern(format);
        }
        try {
            return dateParser.parse(dateValue);
        } catch (ParseException pe) {
            // ignore this exception, we will try the next format
        }
    }

    // we were unable to parse the date
    throw new ParseException("Unable to parse the date " + dateValue, 0);
}

From source file:org.eclipse.dirigible.runtime.registry.RegistryServlet.java

private Date parseDate(String modifiedSinceHeader) {
    final Calendar calendar = Calendar.getInstance();
    if ((modifiedSinceHeader.length() > 1) && modifiedSinceHeader.startsWith(SINGLE_QUOTE)
            && modifiedSinceHeader.endsWith(SINGLE_QUOTE)) {
        modifiedSinceHeader = modifiedSinceHeader.substring(1, modifiedSinceHeader.length() - 1);
    }//from w w w  . j a va  2 s.c  o  m
    for (String format : DATE_FORMATS) {
        SimpleDateFormat dateParser = new SimpleDateFormat(format);
        dateParser.set2DigitYearStart(calendar.getTime());
        final ParsePosition pos = new ParsePosition(0);
        final Date result = dateParser.parse(modifiedSinceHeader, pos);
        if (pos.getIndex() != 0) {
            return result;
        }
    }
    return null;
}