Example usage for java.text DateFormat getDateInstance

List of usage examples for java.text DateFormat getDateInstance

Introduction

In this page you can find the example usage for java.text DateFormat getDateInstance.

Prototype

public static final DateFormat getDateInstance(int style, Locale aLocale) 

Source Link

Document

Gets the date formatter with the given formatting style for the given locale.

Usage

From source file:DateFormatBest.java

public static void main(String[] args) {
    Date today = new Date();

    DateFormat df = DateFormat.getInstance();
    System.out.println(df.format(today));

    DateFormat df_fr = DateFormat.getDateInstance(DateFormat.FULL, Locale.FRENCH);
    System.out.println(df_fr.format(today));
}

From source file:Main.java

public static String calendarDateForLocale(Calendar calendar, Locale locale) {
    DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    return dateFormatter.format(calendar.getTime());
}

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   w w w  .  j  av a 2 s. co  m*/
        return sdf.format(cal.getTime());
    }
    String formattedDate = df.format(cal.getTime());
    return formattedDate;
}

From source file:Main.java

public static void printDate(Locale locale, Date date) {
    DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    String formattedDate = formatter.format(date);
    System.out.println("SHORT: " + formattedDate);

    formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
    formattedDate = formatter.format(date);
    System.out.println("MEDIUM: " + formattedDate + "\n");

}

From source file:com.nextgis.woody.util.UiUtil.java

public static String formatDate(long timestamp, int style) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp);
    Date calendarTime = calendar.getTime();
    DateFormat df = DateFormat.getDateInstance(style, Locale.getDefault());
    return df.format(calendarTime);
}

From source file:com.vityuk.ginger.provider.format.JdkDateUtils.java

private static DateFormat createJdkDateFormat(FormatType formatType, DateFormatStyle formatStyle,
        Locale locale) {//from w  w  w  .j av  a2s. c o  m
    int style = createJdkDateStyleCode(formatStyle);
    switch (formatType) {
    case TIME:
        return DateFormat.getTimeInstance(style, locale);
    case DATE:
        return DateFormat.getDateInstance(style, locale);
    case DATETIME:
        return DateFormat.getDateTimeInstance(style, style, locale);
    }
    throw new IllegalArgumentException();
}

From source file:DateFormatDemo.java

static public void displayDate(Locale currentLocale) {

    Date today;//from   w  w w.ja  v  a2  s.c  o  m
    String dateOut;
    DateFormat dateFormatter;

    dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
    today = new Date();
    dateOut = dateFormatter.format(today);

    System.out.println(dateOut + "   " + currentLocale.toString());
}

From source file:DateUtil.java

/**
 * Formats given date according to specified locale and date style
 *
 * @param date      Date to convert/*from  w  ww.  ja va 2  s.c o  m*/
 * @param locale    Locale to use for formatting date
 * @param dateStyle Date style
 * @return String representation of date according to given locale and date style
 * @see java.text.DateFormat
 */
public static String formatDate(Date date, Locale locale, int dateStyle) {
    DateFormat formatter = DateFormat.getDateInstance(dateStyle, locale);
    return formatter.format(date);
}

From source file:org.jamwiki.utils.DateUtil.java

/**
 * Format the given date using the given pattern to return the date
 * as a formatted string.  If the pattern is invalid this method will
 * return <code>null</code>.
 *//*from   w w  w . j ava 2 s .c om*/
public static String formatDate(Date date, String pattern, String localeString, String timeZoneString,
        DateFormatType dateFormatType) {
    Locale locale = DateUtil.stringToLocale(localeString);
    TimeZone tz = DateUtil.stringToTimeZone(timeZoneString);
    SimpleDateFormat sdf = null;
    int style = DateUtil.stringToDateFormatStyle(pattern);
    if (style != -1 && dateFormatType == DateFormatType.DATE_ONLY) {
        sdf = (SimpleDateFormat) DateFormat.getDateInstance(style, locale);
    } else if (style != -1 && dateFormatType == DateFormatType.TIME_ONLY) {
        sdf = (SimpleDateFormat) DateFormat.getTimeInstance(style, locale);
    } else if (style != -1 && dateFormatType == DateFormatType.DATE_AND_TIME) {
        sdf = (SimpleDateFormat) DateFormat.getDateTimeInstance(style, style, locale);
    } else {
        try {
            sdf = new SimpleDateFormat(pattern, locale);
        } catch (IllegalArgumentException e) {
            String msg = "Attempt to format date with invalid pattern " + pattern
                    + ". If you have customized date or time formats in your "
                    + "jamwiki-configuration.xml file please verify that they are "
                    + "valid java.text.SimpleDateFormat patterns.";
            logger.warn(msg, e);
            return null;
        }
    }
    sdf.setTimeZone(tz);
    return sdf.format(date);
}

From source file:ispok.converter.BirthDateConverter.java

@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String string) {

    logger.trace("Entering getAsObject()");

    Locale locale = fc.getViewRoot().getLocale();

    ResourceBundle rb = ResourceBundle.getBundle("ispok/pres/inter/ispok", locale);

    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    SimpleDateFormat sdf = (SimpleDateFormat) df;

    String pattern = sdf.toPattern();
    String localPattern = sdf.toLocalizedPattern();

    logger.debug("pattern: {}", pattern);
    logger.debug("localized pattern: {}", localPattern);

    Date date;/*w ww .  ja  va2 s.  c  om*/

    try {
        date = new SimpleDateFormat(pattern).parse(string);
    } catch (ParseException ex) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "invalid date", pattern);
        throw new ConverterException(msg);
    }

    if (date.after(new Date())) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, rb.getString("birthdate_invalid"),
                rb.getString("birthdate_valid_future"));
        throw new ConverterException(msg);
    }

    Calendar c = Calendar.getInstance();
    c.set(1850, 1, 1);

    if (date.before(c.getTime())) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, rb.getString("birthdate_invalid"),
                rb.getString("birthdate_valid_past"));
        throw new ConverterException(msg);
    }

    return date;
}