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:Main.java

public static String calculateByDate(String date, String format, int dayOffset) {

    SimpleDateFormat formater = new SimpleDateFormat();
    try {// w w  w. j  av  a  2s .  c o m
        formater.applyPattern(format);
        Date time = formater.parse(date);
        long ts = time.getTime() + dayOffset * 24 * 3600 * 1000L;
        Date newDate = new Date(ts);
        return date2String(format, newDate);
    } catch (ParseException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Parse the serialized string form into a java.util.Date
 *
 * @param date/*from  w ww .ja  v a 2 s  .co  m*/
 *            The serialized string form of the date
 * @return The created java.util.Date
 */
public static long parseAtomDate(String date) {
    SimpleDateFormat sdf = new SimpleDateFormat();
    for (String s : masksAtom) {
        try {
            sdf.applyPattern(s);
            return sdf.parse(date).getTime();
        } catch (Exception e) {
        }
    }
    return 0;
}

From source file:org.apache.syncope.core.misc.DataFormat.java

public static Date parseDate(final String source, final String conversionPattern) throws ParseException {
    SimpleDateFormat sdf = DATE_FORMAT.get();
    sdf.applyPattern(conversionPattern);
    sdf.setLenient(false);/*from  w ww .  jav a  2  s.c  o m*/
    return sdf.parse(source);
}

From source file:Main.java

private static String setDate(int day, Locale locale) {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, +day);
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    if (df instanceof SimpleDateFormat) {
        SimpleDateFormat sdf = (SimpleDateFormat) df;
        // To show Locale specific short date expression with full year
        String pattern = sdf.toPattern().replaceAll("y+", "yyyy");
        sdf.applyPattern(pattern);
        return sdf.format(cal.getTime());
    }/*from   ww  w. j  av  a2 s.c o  m*/
    String formattedDate = df.format(cal.getTime());
    return formattedDate;
}

From source file:fm.audiobox.core.utils.ModelUtil.java

/**
 * Given a string representing an UTC date provided by AudioBox server,
 * this method returns a unix timestamp (always in UTC).
 *
 * @param simpleDateFormat the simple date format
 * @param audioboxDate     the string date provided by audiobox server.
 *
 * @return unix timestamp/*from  ww  w.  j  av a  2  s. co m*/
 *
 * @throws ParseException the parse exception
 */
public static long toUnixTime(SimpleDateFormat simpleDateFormat, String audioboxDate) throws ParseException {

    simpleDateFormat.applyPattern(AUDIOBOX_DATE_FORMAT);
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = simpleDateFormat.parse(audioboxDate);
    return date.getTime();
}

From source file:Main.java

public static String getAbrv_h_m_MMM_d(long date) {
    String patternTime = "h:mm";
    String patternAMPM = "a, ";
    String patternDay = "MMM d (EEE)";

    String dateString = "";

    SimpleDateFormat sdf = new SimpleDateFormat(patternTime);
    mDate.setTime(date);//w  w w.  ja  v  a2 s.  co  m

    dateString += sdf.format(mDate).toString();
    sdf.applyPattern(patternAMPM);
    dateString += sdf.format(mDate).toLowerCase();
    sdf.applyPattern(patternDay);
    dateString += sdf.format(mDate).toString();

    return dateString;
}

From source file:Main.java

public static Date parseRFC822Date(String date) {
    Date result = null;/*www  .ja v  a2 s .  co  m*/
    if (date.contains("PDT")) {
        date = date.replace("PDT", "PST8PDT");
    }
    if (date.contains(",")) {
        // Remove day of the week
        date = date.substring(date.indexOf(",") + 1).trim();
    }
    SimpleDateFormat format = RFC822Formatter.get();
    for (int i = 0; i < RFC822DATES.length; i++) {
        try {
            format.applyPattern(RFC822DATES[i]);
            result = format.parse(date);
            break;
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    if (result == null) {
        Log.e(TAG, "Unable to parse feed date correctly");
    }

    return result;
}

From source file:org.apache.syncope.core.misc.DataFormat.java

public static String format(final Date date, final boolean lenient, final String conversionPattern) {
    SimpleDateFormat sdf = DATE_FORMAT.get();
    if (conversionPattern != null) {
        sdf.applyPattern(conversionPattern);
    }//w  ww  . j av  a 2 s  .c o  m
    sdf.setLenient(lenient);
    return sdf.format(date);
}

From source file:com.baidu.rigel.biplatform.ac.util.TimeRangeDetail.java

/**
 * /*from w w w. j  a v  a 2  s . co  m*/
 * @return
 * 
 */
private static SimpleDateFormat initDateFormat() {
    SimpleDateFormat format = new SimpleDateFormat();
    format.applyPattern(FORMAT_STRING);
    return format;
}

From source file:DateUtil.java

/**
 * Parse String to Date.//from  ww  w .j a va 2  s  .c o  m
 * 
 * @param dateString
 *            String of Date, the format is yyyy-MM-dd or yyyy/MM/dd or
 *            yyyy.MM.dd
 * @return Date
 * @throws ParseException
 */
public static Date stringToDate(String dateString) throws ParseException {
    String[] formatstring = getdateformat();
    int index = 0;
    Date parseDate = null;
    ParseException throwe = null;
    SimpleDateFormat sdf = new SimpleDateFormat();

    while (formatstring != null && index < formatstring.length) {
        try {
            sdf.applyPattern(formatstring[index]);
            index++;
            parseDate = sdf.parse(dateString);
            break;
        } catch (ParseException gete) {
            throwe = gete;
            continue;
        }
    }
    if (parseDate == null)
        throw throwe;
    return parseDate;
}