Example usage for java.text SimpleDateFormat applyPattern

List of usage examples for java.text SimpleDateFormat applyPattern

Introduction

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

Prototype

public void applyPattern(String pattern) 

Source Link

Document

Applies the given pattern string to this date format.

Usage

From source file:util.android.util.DateUtils.java

public static String formatDateName(Date inDate) {
    SimpleDateFormat sdf = new SimpleDateFormat();
    sdf.applyPattern("EEEE");
    return sdf.format(inDate);
}

From source file:util.android.util.DateUtils.java

/**
 * Format a Date object as an Atom Date/Time String.
 *
 * @param inDate//from ww  w.  ja  v a 2  s  .com
 * @return String
 */
@SuppressLint("SimpleDateFormat")
public static String formatAtomDate(Date inDate) {
    SimpleDateFormat sdf = new SimpleDateFormat();
    sdf.applyPattern(atomMasks[0]);
    return sdf.format(inDate);
}

From source file:DateParser.java

/**
 * Parses the date value using the given date formats.
 * //from  w w w.j  a  v  a2s . co m
 * @param dateValue the date value to parse
 * @param dateFormats the date formats to use
 * 
 * @return the parsed date
 * 
 * @throws DateParseException if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(String dateValue, Collection dateFormats) {

    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_PATTERNS;
    }
    // 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(TimeZone.getTimeZone("GMT"));
        } 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:com.luke.lukef.lukeapp.tools.LukeUtils.java

/**
 * Parses the given String into a date object and outputs it in a different pattern.
 *
 * @param dateToParse Date as a String in the following pattern: <b>yyyy-MM-dd'T'HH:mm:ss.SSS'Z'</b>.
 * @return The given date, but in the following format: <b>hh:mm dd/MM/yyyy</b>.
 *//*from   w w w.ja  v  a  2s  .c  o  m*/
public static String parseDateFromString(String dateToParse) {
    try {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
        Date date;
        date = format.parse(dateToParse);
        format.applyPattern("hh:mm dd/MM/yyyy");
        return format.format(date);
    } catch (ParseException e) {
        Log.e(TAG, "parseDateFromString: ", e);
        return null;
    }
}

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

/**
 * Slightly modified from org.apache.commons.httpclient.util.DateUtil.parseDate
 * <p>/*w  w  w.  ja v  a  2s . 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:util.android.util.DateUtils.java

/**
 * Try to parse an ordinal date in the format:
 * <p>/*  w  w w . ja v a 2 s .c o m*/
 * Monday 1st July 2013
 * <p>
 * SimpleDateFormat doesn't like st, nd, th, rd in dates so we modify the input String before processing.
 *
 * @param dateString
 * @param timezone
 * @return Date
 */
@SuppressLint("SimpleDateFormat")
public static Date parseOrdinalDate(String dateString, TimeZone timezone) throws IllegalArgumentException {
    dateString = dateString.trim().replaceAll("([0-9]+)(?:st|nd|rd|th)?", "$1").replace("  ", " ");
    Date d = null;
    SimpleDateFormat sdf = new SimpleDateFormat();
    for (String ordinalMask : ordinalMasks) {
        try {
            sdf.applyPattern(ordinalMask);
            sdf.setTimeZone(timezone);
            sdf.setLenient(true);
            d = sdf.parse(dateString, new ParsePosition(0));
            if (d != null)
                break;
        } catch (Exception ignored) {
        }
    }
    if (d == null)
        throw new IllegalArgumentException();
    return d;
}

From source file:gov.nih.nci.cabig.caaers.utils.DateUtils.java

public static Date parseDate(String dateStr, String... parsePatterns) throws ParseException {

    if (dateStr == null || parsePatterns == null) {
        throw new IllegalArgumentException("Date and Patterns must not be null");
    }//from  w ww. j  a v a  2 s. c om

    String strDate = dateStr;
    //do year correction. (partial year >=50 will be 1999 and <50 will be 2000)
    String[] parts = StringUtils.split(dateStr, '/');
    int len = parts.length;

    if (len != 3 || parts[0].length() > 2 || parts[1].length() > 2)
        throw new ParseException("Unable to parse the date " + strDate, -1);

    String yStr = parts[2];

    if (!(yStr.length() == 4 || yStr.length() == 2 || yStr.length() == 10))
        throw new ParseException("Unable to parse the date " + strDate, -1);
    if (yStr.length() == 2 && StringUtils.isNumeric(yStr)) {

        if (Integer.parseInt(yStr) < 50)
            yStr = "20" + yStr;
        else
            yStr = "19" + yStr;

        parts[2] = yStr;
        strDate = StringUtils.join(parts, '/');
    }

    //BJ: date formats are not thread save, so we need to create one each time.
    SimpleDateFormat parser = null;
    ParsePosition pos = new ParsePosition(0);
    for (int i = 0; i < parsePatterns.length; i++) {
        if (i == 0) {
            parser = new SimpleDateFormat(parsePatterns[0]);
        } else {
            parser.applyPattern(parsePatterns[i]);
        }
        pos.setIndex(0);

        Date date = parser.parse(strDate, pos);
        if (date != null && pos.getIndex() == strDate.length()) {
            return date;
        }
    }
    throw new ParseException("Unable to parse the date: " + strDate, -1);
}

From source file:com.panet.imeta.core.util.StringUtil.java

public static Date str2dat(String arg0, String arg1, String val) throws KettleValueException {
    SimpleDateFormat df = new SimpleDateFormat();

    DateFormatSymbols dfs = new DateFormatSymbols();
    if (arg1 != null)
        dfs.setLocalPatternChars(arg1);/* ww  w  .jav a  2s.com*/
    if (arg0 != null)
        df.applyPattern(arg0);

    try {
        return df.parse(val);
    } catch (Exception e) {
        throw new KettleValueException("TO_DATE Couldn't convert String to Date " + e.toString());
    }
}

From source file:org.openossad.util.core.util.StringUtil.java

public static Date str2dat(String arg0, String arg1, String val) throws OpenDESIGNERValueException {
    SimpleDateFormat df = new SimpleDateFormat();

    DateFormatSymbols dfs = new DateFormatSymbols();
    if (arg1 != null)
        dfs.setLocalPatternChars(arg1);/*  ww w.j  a v a2  s  .  c o  m*/
    if (arg0 != null)
        df.applyPattern(arg0);

    try {
        return df.parse(val);
    } catch (Exception e) {
        throw new OpenDESIGNERValueException("TO_DATE Couldn't convert String to Date " + e.toString());
    }
}

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

/**
 * Slightly modified from org.apache.commons.httpclient.util.DateUtil.parseDate
 * <p/>// w  w w .ja v  a  2 s.  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);
}