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() 

Source Link

Document

Gets the date formatter with the default formatting style for the default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:org.opendatakit.briefcase.util.WebUtils.java

/**
 * Parse a string into a datetime value. Tries the common Http formats, the
 * iso8601 format (used by Javarosa), the default formatting from
 * Date.toString(), and a time-only format.
 * //from  w w  w  .jav a2s .  com
 * @param value
 * @return
 */
public static final Date parseDate(String value) {
    if (value == null || value.length() == 0)
        return null;

    String[] iso8601Pattern = new String[] { PATTERN_ISO8601 };

    String[] localizedParsePatterns = new String[] {
            // try the common HTTP date formats that have time zones
            PATTERN_RFC1123, PATTERN_RFC1036, PATTERN_DATE_TOSTRING };

    String[] localizedNoTzParsePatterns = new String[] {
            // ones without timezones... (will assume UTC)
            PATTERN_ASCTIME };

    String[] tzParsePatterns = new String[] { PATTERN_ISO8601, PATTERN_ISO8601_DATE, PATTERN_ISO8601_TIME };

    String[] noTzParsePatterns = new String[] {
            // ones without timezones... (will assume UTC)
            PATTERN_ISO8601_WITHOUT_ZONE, PATTERN_NO_DATE_TIME_ONLY,
            PATTERN_YYYY_MM_DD_DATE_ONLY_NO_TIME_DASH };

    Date d = null;
    // iso8601 parsing is sometimes off-by-one when JR does it...
    d = parseDateSubset(value, iso8601Pattern, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try to parse with the JavaRosa parsers
    d = DateUtils.parseDateTime(value);
    if (d != null)
        return d;
    d = DateUtils.parseDate(value);
    if (d != null)
        return d;
    d = DateUtils.parseTime(value);
    if (d != null)
        return d;
    // try localized and english text parsers (for Web headers and interactive filter spec.)
    d = parseDateSubset(value, localizedParsePatterns, Locale.ENGLISH, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedNoTzParsePatterns, Locale.ENGLISH, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedNoTzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try other common patterns that might not quite match JavaRosa parsers
    d = parseDateSubset(value, tzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, noTzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try the locale- and timezone- specific parsers
    {
        DateFormat formatter = DateFormat.getDateTimeInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    {
        DateFormat formatter = DateFormat.getDateInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    {
        DateFormat formatter = DateFormat.getTimeInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    throw new IllegalArgumentException("Unable to parse the date: " + value);
}

From source file:org.apache.click.util.RequestTypeConverter.java

/**
 * Return the time value in milliseconds of the given date value string,
 * or Long.MIN_VALUE if the date could not be determined.
 *
 * @param value the date value string/* w  ww . java 2s  . co  m*/
 * @return the time value in milliseconds or Long.MIN_VALUE if not determined
 */
protected long getTimeFromDateString(String value) {
    Validate.notNull(value, "Null value string");

    value = value.trim();

    if (value.length() == 0) {
        return Long.MIN_VALUE;
    }

    if (isTimeValue(value)) {
        return Long.parseLong(value);
    }

    java.util.Date date = createDateFromSqlString(value);
    if (date != null) {
        return date.getTime();
    }

    try {
        DateFormat format = DateFormat.getDateInstance();

        date = format.parse(value);

        return date.getTime();

    } catch (ParseException pe) {
        return Long.MIN_VALUE;
    }
}

From source file:org.betaconceptframework.astroboa.portal.utility.CalendarUtils.java

/**
 * Get a string of a certain date pattern form (e.g. YYYY-MM-DD i.e. 2008-03-29) and converts it to
 * a calendar object. // ww w . j  ava2  s.  c o  m
 * @param dateString
 * @param dateFormatPattern
 * @return
 */
public Calendar convertDateStringToCalendar(String dateString, String dateFormatPattern, TimeZone zone,
        Locale locale) {
    SimpleDateFormat dateFormat = (SimpleDateFormat) DateFormat.getDateInstance();
    dateFormat.setLenient(false); // be strict in the formatting
    // apply accepted pattern
    dateFormat.applyPattern(dateFormatPattern);

    // get a date object
    try {
        Date date = dateFormat.parse(dateString);
        if (date != null) {
            Calendar calendar = new GregorianCalendar(zone, locale);
            calendar.setTime(date);
            return calendar;
        } else {
            return null;
        }
    } catch (Exception e) {
        logger.warn("The date string:" + dateString + " has an invalid format. The valid form is:"
                + dateFormatPattern, e);
        return null;
    }

}

From source file:com.swordlord.gozer.datatypeformat.DataTypeHelper.java

/**
 * Return compatible class for typedValue based on untypedValueClass 
 * /*  w w w . j  a  va  2s . c o m*/
 * @param untypedValueClass
 * @param typedValue
 * @return
 */
public static Object fromDataType(Class<?> untypedValueClass, Object typedValue) {
    Log LOG = LogFactory.getLog(DataTypeHelper.class);

    if (typedValue == null) {
        return null;
    }

    if (untypedValueClass == null) {
        return typedValue;
    }

    if (ClassUtils.isAssignable(typedValue.getClass(), untypedValueClass)) {
        return typedValue;
    }

    String strTypedValue = null;
    boolean isStringTypedValue = typedValue instanceof String;

    Number numTypedValue = null;
    boolean isNumberTypedValue = typedValue instanceof Number;

    Boolean boolTypedValue = null;
    boolean isBooleanTypedValue = typedValue instanceof Boolean;

    Date dateTypedValue = null;
    boolean isDateTypedValue = typedValue instanceof Date;

    if (isStringTypedValue) {
        strTypedValue = (String) typedValue;
    }
    if (isNumberTypedValue) {
        numTypedValue = (Number) typedValue;
    }
    if (isBooleanTypedValue) {
        boolTypedValue = (Boolean) typedValue;
    }
    if (isDateTypedValue) {
        dateTypedValue = (Date) typedValue;
    }

    Object v = null;
    if (String.class.equals(untypedValueClass)) {
        v = ObjectUtils.toString(typedValue);
    } else if (BigDecimal.class.equals(untypedValueClass)) {
        if (isStringTypedValue) {
            v = NumberUtils.createBigDecimal(strTypedValue);
        } else if (isNumberTypedValue) {
            v = new BigDecimal(numTypedValue.doubleValue());
        } else if (isBooleanTypedValue) {
            v = new BigDecimal(BooleanUtils.toInteger(boolTypedValue.booleanValue()));
        } else if (isDateTypedValue) {
            v = new BigDecimal(dateTypedValue.getTime());
        }
    } else if (Boolean.class.equals(untypedValueClass)) {
        if (isStringTypedValue) {
            v = BooleanUtils.toBooleanObject(strTypedValue);
        } else if (isNumberTypedValue) {
            v = BooleanUtils.toBooleanObject(numTypedValue.intValue());
        } else if (isDateTypedValue) {
            v = BooleanUtils.toBooleanObject((int) dateTypedValue.getTime());
        }
    } else if (Byte.class.equals(untypedValueClass)) {
        if (isStringTypedValue) {
            v = Byte.valueOf(strTypedValue);
        } else if (isNumberTypedValue) {
            v = new Byte(numTypedValue.byteValue());
        } else if (isBooleanTypedValue) {
            v = new Byte((byte) BooleanUtils.toInteger(boolTypedValue.booleanValue()));
        } else if (isDateTypedValue) {
            v = new Byte((byte) dateTypedValue.getTime());
        }
    } else if (byte[].class.equals(untypedValueClass)) {
        if (isStringTypedValue) {
            v = strTypedValue.getBytes();
        }
    } else if (Double.class.equals(untypedValueClass)) {
        if (isStringTypedValue) {
            v = NumberUtils.createDouble(strTypedValue);
        } else if (isNumberTypedValue) {
            v = new Double(numTypedValue.doubleValue());
        } else if (isBooleanTypedValue) {
            v = new Double(BooleanUtils.toInteger(boolTypedValue.booleanValue()));
        } else if (isDateTypedValue) {
            v = new Double(dateTypedValue.getTime());
        }
    } else if (Float.class.equals(untypedValueClass)) {
        if (isStringTypedValue) {
            v = NumberUtils.createFloat(strTypedValue);
        } else if (isNumberTypedValue) {
            v = new Float(numTypedValue.floatValue());
        } else if (isBooleanTypedValue) {
            v = new Float(BooleanUtils.toInteger(boolTypedValue.booleanValue()));
        } else if (isDateTypedValue) {
            v = new Float(dateTypedValue.getTime());
        }
    } else if (Short.class.equals(untypedValueClass)) {
        if (isStringTypedValue) {
            v = NumberUtils.createInteger(strTypedValue);
        } else if (isNumberTypedValue) {
            v = new Integer(numTypedValue.intValue());
        } else if (isBooleanTypedValue) {
            v = BooleanUtils.toIntegerObject(boolTypedValue.booleanValue());
        } else if (isDateTypedValue) {
            v = new Integer((int) dateTypedValue.getTime());
        }
    } else if (Integer.class.equals(untypedValueClass)) {
        if (isStringTypedValue) {
            v = NumberUtils.createInteger(strTypedValue);
        } else if (isNumberTypedValue) {
            v = new Integer(numTypedValue.intValue());
        } else if (isBooleanTypedValue) {
            v = BooleanUtils.toIntegerObject(boolTypedValue.booleanValue());
        } else if (isDateTypedValue) {
            v = new Integer((int) dateTypedValue.getTime());
        }
    } else if (Long.class.equals(untypedValueClass)) {
        if (isStringTypedValue) {
            v = NumberUtils.createLong(strTypedValue);
        } else if (isNumberTypedValue) {
            v = new Long(numTypedValue.longValue());
        } else if (isBooleanTypedValue) {
            v = new Long(BooleanUtils.toInteger(boolTypedValue.booleanValue()));
        } else if (isDateTypedValue) {
            v = new Long(dateTypedValue.getTime());
        }
    } else if (java.sql.Date.class.equals(untypedValueClass)) {
        if (isNumberTypedValue) {
            v = new java.sql.Date(numTypedValue.longValue());
        } else if (isDateTypedValue) {
            v = new java.sql.Date(dateTypedValue.getTime());
        }
    } else if (java.sql.Time.class.equals(untypedValueClass)) {
        if (isNumberTypedValue) {
            v = new java.sql.Time(numTypedValue.longValue());
        } else if (isDateTypedValue) {
            v = new java.sql.Time(dateTypedValue.getTime());
        }
    } else if (java.sql.Timestamp.class.equals(untypedValueClass)) {
        if (isNumberTypedValue) {
            v = new java.sql.Timestamp(numTypedValue.longValue());
        } else if (isDateTypedValue) {
            v = new java.sql.Timestamp(dateTypedValue.getTime());
        }
    } else if (Date.class.equals(untypedValueClass)) {
        if (isNumberTypedValue) {
            v = new Date(numTypedValue.longValue());
        } else if (isStringTypedValue) {
            try {
                v = DateFormat.getDateInstance().parse(strTypedValue);
            } catch (ParseException e) {
                LOG.error("Unable to parse the date : " + strTypedValue);
                LOG.debug(e.getMessage());
            }
        }
    }
    return v;
}

From source file:op.tools.SYSCalendar.java

public static void handleDateFocusLost(FocusEvent evt, LocalDate min, LocalDate max) {
    LocalDate dt;/* w  w w .ja  va2  s . c o  m*/
    if (max == null) {
        max = new LocalDate();
    }
    try {
        dt = new LocalDate(parseDate(((JTextField) evt.getSource()).getText()));
    } catch (NumberFormatException ex) {
        OPDE.getDisplayManager().addSubMessage(new DisplayMessage(SYSTools.xx("misc.msg.wrongdate")));
        dt = new LocalDate();
    }
    if (dt.isAfter(max)) {
        dt = new LocalDate();
        DisplayMessage dm = new DisplayMessage(
                dt.isAfter(max) ? SYSTools.xx("misc.msg.futuredate") : SYSTools.xx("misc.msg.wrongdate"));
        OPDE.getDisplayManager().addSubMessage(dm);
    }
    if (dt.isBefore(min)) {
        dt = new LocalDate();
        OPDE.getDisplayManager().addSubMessage(new DisplayMessage(SYSTools.xx("misc.msg.DateTooOld")));
    }

    ((JTextField) evt.getSource()).setText(DateFormat.getDateInstance().format(dt.toDate()));
}

From source file:org.betaconceptframework.astroboa.portal.utility.CalendarUtils.java

public String convertDateToString(Date date, String dateFormatPattern) {
    SimpleDateFormat dateFormat = (SimpleDateFormat) DateFormat.getDateInstance();
    dateFormat.setLenient(false); // be strict in the formatting
    // apply accepted pattern
    dateFormat.applyPattern(dateFormatPattern);

    return dateFormat.format(date);
}

From source file:com.vrs.reqdroid.fragments.HipotesesFragment.java

/**
 * Atribui a data atual./*from   www  .  j  a  va2 s .  c  om*/
 *
 * @return a data atual
 */
private static String getDataATual() {
    return DateFormat.getDateInstance().format(new Date());
}

From source file:op.care.nursingprocess.PnlSchedule.java

private void initPanel() {

    pnlBemerkung.setBorder(new TitledBorder(SYSTools.xx(internalClassID + ".bordertitle4textfield")));

    ArrayList<Date> timelist = SYSCalendar.getTimeList();
    cmbUhrzeit.setModel(new DefaultComboBoxModel(timelist.toArray()));
    cmbUhrzeit.setRenderer(SYSCalendar.getTimeRenderer());

    spinTaeglich.setModel(new SpinnerNumberModel(1, 1, 365, 1));
    spinWoche.setModel(new SpinnerNumberModel(1, 1, 52, 1));
    spinMonat.setModel(new SpinnerNumberModel(1, 1, 12, 1));
    spinMonatTag.setModel(new SpinnerNumberModel(1, 1, 31, 1));

    spinTaeglich.setValue(Math.max(is.getTaeglich(), 1));
    spinWoche.setValue(Math.max(is.getWoechentlich(), 1));
    spinMonat.setValue(Math.max(is.getMonatlich(), 1));
    spinMonatTag.setValue(Math.max(is.getTagNum(), 1));

    tabWdh.setSelectedIndex(TAB_DAILY);/*from   w  w w. j  a va 2s.com*/

    if (is.getWoechentlich() > 0) {
        cbMon.setSelected(is.getMon() > 0);
        cbDie.setSelected(is.getDie() > 0);
        cbMit.setSelected(is.getMit() > 0);
        cbDon.setSelected(is.getDon() > 0);
        cbFre.setSelected(is.getFre() > 0);
        cbSam.setSelected(is.getSam() > 0);
        cbSon.setSelected(is.getSon() > 0);
        tabWdh.setSelectedIndex(TAB_WEEKLY);
    }

    if (is.getMonatlich() > 0) {
        if (is.getTagNum() > 0) {

            spinMonatTag.setValue(is.getTagNum());
            cmbTag.setSelectedIndex(0);
        } else {

            if (is.getMon() > 0) {
                cmbTag.setSelectedIndex(1);
                spinMonatTag.setValue(is.getMon());
            } else if (is.getDie() > 0) {
                cmbTag.setSelectedIndex(2);
                spinMonatTag.setValue(is.getDie());
            } else if (is.getMit() > 0) {
                cmbTag.setSelectedIndex(3);
                spinMonatTag.setValue(is.getMit());
            } else if (is.getDon() > 0) {
                cmbTag.setSelectedIndex(4);
                spinMonatTag.setValue(is.getDon());
            } else if (is.getFre() > 0) {
                cmbTag.setSelectedIndex(5);
                spinMonatTag.setValue(is.getFre());
            } else if (is.getSam() > 0) {
                cmbTag.setSelectedIndex(6);
                spinMonatTag.setValue(is.getSam());
            } else if (is.getSon() > 0) {
                cmbTag.setSelectedIndex(7);
                spinMonatTag.setValue(is.getSon());
            }
        }
        tabWdh.setSelectedIndex(TAB_MONTHLY);
    }

    txtLDate.setText(DateFormat.getDateInstance()
            .format(new Date(Math.max(is.getLDatum().getTime(), new DateMidnight().getMillis()))));

    txtNachtMo.setText(is.getNachtMo().toString());
    txtMorgens.setText(is.getMorgens().toString());
    txtMittags.setText(is.getMittags().toString());
    txtNachmittags.setText(is.getNachmittags().toString());
    txtAbends.setText(is.getAbends().toString());
    txtNachtAb.setText(is.getNachtAb().toString());
    txtUhrzeit.setText(is.getUhrzeitAnzahl().toString());

    txtMorgens.setBackground(SYSConst.lightblue);
    txtMittags.setBackground(SYSConst.gold7);
    txtNachmittags.setBackground(SYSConst.melonrindgreen);
    txtAbends.setBackground(SYSConst.bermuda_sand);
    txtNachtAb.setBackground(SYSConst.bluegrey);

    Date now = null;
    if (is.getUhrzeitAnzahl() > 0) {
        now = is.getUhrzeit();
    } else {
        now = new Date();
    }

    for (Date zeit : timelist) {
        if (SYSCalendar.compareTime(zeit, now) >= 0) {
            now = zeit;
            break;
        }
    }
    cmbUhrzeit.setSelectedItem(now);
    lblUhrzeit.setText(SYSTools.xx("misc.msg.Number"));

    txtBemerkung.setText(is.getBemerkung());

    lblMinutes.setText(SYSTools.xx("misc.msg.Minute(s)"));
    txtMinutes.setText(is.getDauer().toPlainString());

    tbFloating = GUITools.getNiceToggleButton(SYSTools.xx(internalClassID + ".floatinginterventions"));
    tbFloating.setSelected(is.isFloating());
    panelMain.add(tbFloating, CC.xy(3, 5));

    splitRegularPos = SYSTools.showSide(splitRegular,
            is.getUhrzeit() != null ? SYSTools.RIGHT_LOWER_SIDE : SYSTools.LEFT_UPPER_SIDE);
}

From source file:op.care.prescription.DlgRegular.java

private void rbDateItemStateChanged(ItemEvent e) {
    txtTo.setEnabled(e.getStateChange() == ItemEvent.SELECTED);
    if (e.getStateChange() == ItemEvent.SELECTED) {
        txtTo.setText(DateFormat.getDateInstance().format(new Date()));
    }//w  ww  .  j  a  va2  s.  c om
}

From source file:com.dpcsoftware.mn.App.java

public String dateToUser(String pattern, String date) {
    SimpleDateFormat localFormater;
    if (pattern == null) {
        localFormater = (SimpleDateFormat) DateFormat.getDateInstance();
    } else/*  ww w.  j a  va 2s . c  o m*/
        localFormater = new SimpleDateFormat(pattern, Locale.getDefault());

    try {
        return localFormater.format(getDateDbFormater("yyyy-MM-dd").parse(date));
    } catch (Exception e) {
        return null;
    }
}