Example usage for java.text SimpleDateFormat toPattern

List of usage examples for java.text SimpleDateFormat toPattern

Introduction

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

Prototype

public String toPattern() 

Source Link

Document

Returns a pattern string describing this date format.

Usage

From source file:Main.java

public static Date parse(String dateFormatted, SimpleDateFormat dateFormat, boolean useUtc) {
    Date date = null;/*  www  .  j ava2s . c o  m*/
    if (!dateFormatted.isEmpty()) {
        try {
            if (useUtc) {
                dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            }
            date = dateFormat.parse(dateFormatted);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Error parsing the dateFormatted: " + dateFormatted + " pattern: " + dateFormat.toPattern(),
                    e);
        }
    }
    return date;
}

From source file:org.alfresco.util.CachingDateFormat.java

/**
 * @param length/*from  ww  w .j av  a  2  s. c o  m*/
 *            the type of date format, e.g. {@link CachingDateFormat#LONG }
 * @param locale
 *            the <code>Locale</code> that will be used to determine the
 *            date pattern
 * 
 * @see #getDateFormat(String, boolean)
 * @see CachingDateFormat#SHORT
 * @see CachingDateFormat#MEDIUM
 * @see CachingDateFormat#LONG
 * @see CachingDateFormat#FULL
 */
public static SimpleDateFormat getDateFormat(int length, Locale locale, boolean lenient) {
    SimpleDateFormat dateFormat = (SimpleDateFormat) CachingDateFormat.getDateInstance(length, locale);
    // extract the format string
    String pattern = dateFormat.toPattern();
    // we have a pattern to use
    return getDateFormat(pattern, lenient);
}

From source file:edu.ku.brc.af.prefs.AppPrefsCache.java

/**
 * Returns a SimpleDateFormat for date with two month and day digits and 4 year digits.
 * @return a SimpleDateFormat for date with two month and day digits and 4 year digits.
 *///from w  w w  .  j a  v a 2s  .  c  om
public static SimpleDateFormat getDefaultDatePattern() {
    boolean debug = AppPreferences.getLocalPrefs().getBoolean("DEBUG.DATES", false);

    SimpleDateFormat sdf = new SimpleDateFormat();

    String[] pieces = sdf.toPattern().split(" "); //$NON-NLS-1$
    if (pieces != null) {
        String pattern = pieces[0];

        if (debug) {
            System.out.println("[" + pattern + "][" + sdf.toPattern() + "]");

            System.out.println("Months: " + StringUtils.countMatches(pattern, "M"));
            System.out.println("Days:   " + StringUtils.countMatches(pattern, "d"));
            System.out.println("Years:  " + StringUtils.countMatches(pattern, "y"));
        }

        int months = StringUtils.countMatches(pattern, "M"); //$NON-NLS-1$
        int days = StringUtils.countMatches(pattern, "d"); //$NON-NLS-1$
        int years = StringUtils.countMatches(pattern, "y"); //$NON-NLS-1$

        if (months == 1) {
            pattern = pattern.replace("M", "MM"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        if (days == 1) {
            pattern = pattern.replace("d", "dd"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        if (years == 2) {
            pattern = pattern.replace("yy", "yyyy"); //$NON-NLS-1$ //$NON-NLS-2$
        }

        if (debug) {
            System.out.println(pattern);
            System.out.println("Months: " + StringUtils.countMatches(pattern, "M"));
            System.out.println("Days:   " + StringUtils.countMatches(pattern, "d"));
            System.out.println("Years:  " + StringUtils.countMatches(pattern, "y"));
        }
        return new SimpleDateFormat(pattern);

    }

    return new SimpleDateFormat("MM/dd/yyyy"); //$NON-NLS-1$
}

From source file:org.alfresco.util.CachingDateFormat.java

/**
 * @param dateLength//  ww w . java 2s .c om
 *            the type of date format, e.g. {@link CachingDateFormat#LONG }
 * @param timeLength
 *            the type of time format, e.g. {@link CachingDateFormat#LONG }
 * @param locale
 *            the <code>Locale</code> that will be used to determine the
 *            date pattern
 * 
 * @see #getDateFormat(String, boolean)
 * @see CachingDateFormat#SHORT
 * @see CachingDateFormat#MEDIUM
 * @see CachingDateFormat#LONG
 * @see CachingDateFormat#FULL
 */
public static SimpleDateFormat getDateTimeFormat(int dateLength, int timeLength, Locale locale,
        boolean lenient) {
    SimpleDateFormat dateFormat = (SimpleDateFormat) CachingDateFormat.getDateTimeInstance(dateLength,
            timeLength, locale);
    // extract the format string
    String pattern = dateFormat.toPattern();
    // we have a pattern to use
    return getDateFormat(pattern, lenient);
}

From source file:org.kalypso.dwd.servlet.dwdfilecopy.DWDCopyTask.java

/**
 * Return the date of the dwd forecast file. The date is coded in the file name. Example filename for dwd raster
 * format: "lm_2004_11_10_00" and its format would be 'lm_'yyyy'_'MM'_'dd'_'hh
 *///from www  .jav a  2  s. c o  m
public static Date getDateFromRaster(final FileObject file, final SimpleDateFormat df) {
    try {
        return df.parse(file.getName().getBaseName());
    } catch (final ParseException e) {
        DWDFileCopyServlet.LOG.fine("DWD-Forecast filename \"" + file.getName().getBaseName().toString()
                + "\" has not a valid format, should be:" + df.toPattern());
        return null;
    }
}

From source file:org.kalypso.dwd.DWDRasterHelper.java

/**
 * Return the date of the dwd forecast file. The date is coded in the file name.
 * <p>//from  ww w .j  a va2s  .c  o m
 * Example filename for dwd raster format: "lm_2004_11_10_00" and its format would be 'lm_'yyyy'_'MM'_'dd'_'hh
 * <p>
 */
public static Date getDateFromRaster(final File file, final SimpleDateFormat df) {
    try {
        return df.parse(file.getName());
    } catch (final ParseException e) {
        LOG.warning("DWD-Forecast filename \"" + file.getName() + "\" has not a valid format, should be:"
                + df.toPattern());
        return null;
    }
}

From source file:org.mifos.framework.util.helpers.DateUtils.java

public static String convertToDbFormat(Locale locale, String givenDate) throws InvalidDateException {
    SimpleDateFormat format = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale);
    String userfmt = convertToCurrentDateFormat(format.toPattern());
    return convertUserToDbFmt(givenDate, userfmt);
}

From source file:org.mifos.framework.util.helpers.DateUtils.java

public static String getCurrentDate(Locale locale) throws InvalidDateException {
    // the following line is for 1.1 release and will be removed when date
    // is localized
    locale = internalLocale;// www  .j  av a 2 s.c om
    Calendar currentCalendar = getCurrentDateCalendar();
    java.sql.Date currentDate = new java.sql.Date(currentCalendar.getTimeInMillis());
    SimpleDateFormat format = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale);
    String userfmt = convertToCurrentDateFormat(format.toPattern());
    return convertDbToUserFmt(currentDate.toString(), userfmt);
}

From source file:org.mifos.framework.util.helpers.DateUtils.java

public static String getLocalDateString(DateTime date, Locale locale) throws InvalidDateException {
    // the following line is for 1.1 release and will be removed when date
    // is localized
    locale = internalLocale;/*from w w  w  .ja  v  a 2 s  .  c  o m*/
    Calendar calendar = date.toCalendar(locale);
    java.sql.Date currentDate = new java.sql.Date(calendar.getTimeInMillis());
    SimpleDateFormat format = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale);
    String userfmt = convertToCurrentDateFormat(format.toPattern());
    return convertDbToUserFmt(currentDate.toString(), userfmt);
}

From source file:org.mifos.framework.util.helpers.DateUtils.java

public static String getCurrentDate() throws InvalidDateException {
    Calendar currentCalendar = getCurrentDateCalendar();
    java.sql.Date currentDate = new java.sql.Date(currentCalendar.getTimeInMillis());
    SimpleDateFormat format = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, internalLocale);
    String userfmt = convertToCurrentDateFormat(format.toPattern());
    return convertDbToUserFmt(currentDate.toString(), userfmt);
}