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 void main(String[] args) {
    DateFormat df = DateFormat.getDateInstance();
    if (df instanceof SimpleDateFormat) {
        SimpleDateFormat sdf = (SimpleDateFormat) df;
        System.out.println(sdf.toPattern());
    } else {//from www . j a  va2s . c  om
        System.out.println("sorry");
    }
}

From source file:Main.java

public static void main(String args[]) {
    SimpleDateFormat df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT);
    System.out.println("The short date format is  " + df.toPattern());
    Locale loc = Locale.ITALY;
    df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, loc);
    System.out.println("The short date format is  " + df.toPattern());
}

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);//from   ww w. jav  a 2s . com
        return sdf.format(cal.getTime());
    }
    String formattedDate = df.format(cal.getTime());
    return formattedDate;
}

From source file:surrey.csv.profile.expression.DateExpressionEvaluator.java

public static Date getDate(String input) {
    SimpleDateFormat df = new SimpleDateFormat();
    String pattern = df.toPattern();
    String value = input;//from w w w  . j  a v  a 2s. c o  m
    if (input.indexOf(PATTERN_SEPARATOR) > -1) {
        pattern = input.substring(0, input.indexOf(PATTERN_SEPARATOR));
        value = input.substring(input.indexOf(PATTERN_SEPARATOR) + PATTERN_SEPARATOR.length());
    }
    df.applyPattern(pattern);
    try {
        Date date = df.parse(value);
        return date;
    } catch (ParseException e) {
        return null;
    }
}

From source file:vng.paygate.domain.common.DateUtils.java

public static boolean checkDateFormat(String inDate, String format) {
    if (inDate == null) {
        return false;
    }/*  w w w.  j  av a 2  s .  c  om*/
    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    if (inDate.trim().length() != dateFormat.toPattern().length()) {
        return false;
    }
    dateFormat.setLenient(false);
    try {
        dateFormat.parse(inDate.trim());
    } catch (Exception ex) {
        return false;
    }
    return true;
}

From source file:net.sf.housekeeper.swing.util.BoundComponentFactory.java

/**
 * Creates a JSpinner whose value is bound to the given ValueModel. This
 * means, that the value of the spinner and the value of the ValueModel are
 * synchronized bidirectionally. Additionally, the spinner uses
 * the current locale's short format for displaying a date.
 * /*from w w  w . j  a  v  a  2  s  .c om*/
 * @param valueModel the model that provides the value. Must not be null and
 *            must provide {@link Date}objects.
 * @return A spinner whose value is bound to <code>valueModel</code>.
 */
public static JSpinner createDateSpinner(final ValueModel valueModel) {
    assert valueModel != null : "Parameter valueModel must not be null";
    assert valueModel.getValue().getClass()
            .equals(Date.class) : "valueModel must provide Date objects as values";

    final SpinnerDateModel model = new SpinnerDateModelAdapter(valueModel);
    //Need to truncate the current date for correct spinner operation
    model.setStart(DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH));
    model.setCalendarField(Calendar.DAY_OF_MONTH);

    final JSpinner spinner = new JSpinner(model);

    //Set the spinner's editor to use the current locale's short date
    // format
    final SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance(DateFormat.SHORT);
    final String formatPattern = dateFormat.toPattern();
    spinner.setEditor(new JSpinner.DateEditor(spinner, formatPattern));

    return spinner;
}

From source file:DateUtil.java

/**
 * Gets date pattern without time symbols according to given locale and date style
 *
 * @param locale    Locale to searh pattern for
 * @param dateStyle Date style to search pattern by
 * @return Date pattern without time symbols
 *///from   ww  w  . j a v a 2s.co  m
public static String getDatePattern(Locale locale, int dateStyle) {
    String id = locale.toString() + "+" + dateStyle;
    String pattern = (String) patterns.get(id);
    if (pattern == null) {
        SimpleDateFormat format = (SimpleDateFormat) DateFormat.getDateInstance(dateStyle, locale);
        pattern = format.toPattern();
        patterns.put(id, pattern);
    }
    return pattern;
}

From source file:br.com.nordestefomento.jrimum.utilix.DateUtil.java

/**
 * <p>/*from ww  w .  ja  v a2  s .  c o m*/
 * Converte um objeto <code>String</code> em um objeto
 * <code>java.util.Date</code> atravs do objeto
 * <code>java.text.DateFormat</code> especificado.
 * </p>
 * 
 * @param dateAsString
 *            - um valor de data em forma de <code>String</code>.
 * @param dateFormat
 *            - formatador para objetos <code>java.util.Date</code>.
 * @return Objeto <code>java.util.Date</code> convertido a partir do objeto
 *         <code>String</code>
 * 
 * @throws IllegalArgumentException
 *             caso o objeto <code>String</code> no seja um valor vlido de
 *             data suportado pelo formatador.
 * @since 0.2
 */
public static Date parse(String dateAsString, DateFormat dateFormat) {

    Date date = null;

    if (dateAsString == null) {
        throw new NullPointerException("A String a ser convertida no pode ter valor [null].");
    }

    if (dateFormat == null) {
        throw new NullPointerException("O formatador no pode ter valor [null].");
    }

    try {

        date = dateFormat.parse(dateAsString);

    } catch (ParseException e) {

        String msg = "A String [" + dateAsString + "] deve ser uma data vlida no formato";
        if (dateFormat instanceof SimpleDateFormat) {
            SimpleDateFormat sdf = (SimpleDateFormat) dateFormat;
            msg += " [" + sdf.toPattern() + "].";

        } else {
            msg += " especificado.";
        }

        IllegalArgumentException iae = new IllegalArgumentException(msg);
        iae.initCause(e);
        throw iae;
    }

    return date;
}

From source file:com.espertech.esper.client.util.DateTime.java

private static Date parse(String str, SimpleDateFormat format) {
    Date d;//w ww . ja v  a2s. com
    try {
        d = format.parse(str);
    } catch (ParseException e) {
        log.warn("Error parsing date '" + str + "' according to format '" + format.toPattern() + "': "
                + e.getMessage(), e);
        return null;
    }
    return d;
}

From source file:org.jdal.vaadin.ui.FormUtils.java

/**
 * Create a new DateField with format for current locale and given style.
 * @param style DateFormat style// w w w  . j a  va2 s  .  c  o m
 * @return a new DateField
 */
public static DateField newDateField(int style) {
    DateField df = new DateField();
    Locale locale = LocaleContextHolder.getLocale();
    df.setLocale(locale);
    DateFormat dateFormat = DateFormat.getDateInstance(style);

    if (dateFormat instanceof SimpleDateFormat) {
        SimpleDateFormat sdf = (SimpleDateFormat) dateFormat;
        df.setDateFormat(sdf.toPattern());
    }

    return df;
}