List of usage examples for java.text SimpleDateFormat toPattern
public String toPattern()
From source file:org.sakaiproject.tool.assessment.ui.listener.util.TimeUtil.java
/** * Convert a String representation of date and time to Date on the server timezone *//* w w w . ja v a 2 s . com*/ public Date getServerDateTime(SimpleDateFormat ndf, String clientString) { Date serverDate = null; try { if ((m_client_timezone != null) && (m_server_timezone != null) && (!m_client_timezone.hasSameRules(m_server_timezone))) { serverDate = convertFromTimeZone1StringToServerDate(ndf, clientString, m_client_timezone); } else { serverDate = ndf.parse(clientString); } } catch (ParseException e) { log.warn( "can not parse the string, " + clientString + ", into a Date using format: " + ndf.toPattern()); if (log.isDebugEnabled()) { e.printStackTrace(); } } return serverDate; }
From source file:gov.nih.nci.cagrid.sdk4query.processor.PublicDataCQL2ParameterizedHQL.java
private java.lang.Object valueToObject(String className, String value) throws QueryProcessingException { LOG.debug("Converting \"" + value + "\" to object of type " + className); if (className.equals(String.class.getName())) { return value; }/*from ww w.j a v a2 s . co m*/ if (className.equals(Integer.class.getName())) { return Integer.valueOf(value); } if (className.equals(Long.class.getName())) { return Long.valueOf(value); } if (className.equals(Double.class.getName())) { return Double.valueOf(value); } if (className.equals(Float.class.getName())) { return Float.valueOf(value); } if (className.equals(Boolean.class.getName())) { return Boolean.valueOf(value); } if (className.equals(Character.class.getName())) { if (value.length() == 1) { return Character.valueOf(value.charAt(0)); } else { throw new QueryProcessingException( "The value \"" + value + "\" of length " + value.length() + " is not a valid character"); } } if (className.equals(Date.class.getName())) { // try time, then dateTime, then just date List<SimpleDateFormat> formats = new ArrayList<SimpleDateFormat>(3); formats.add(new SimpleDateFormat("HH:mm:ss")); formats.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")); formats.add(new SimpleDateFormat("yyyy-MM-dd")); Date date = null; Iterator<SimpleDateFormat> formatIter = formats.iterator(); while (date == null && formatIter.hasNext()) { SimpleDateFormat formatter = formatIter.next(); try { date = formatter.parse(value); } catch (ParseException ex) { LOG.debug(value + " was not parsable by pattern " + formatter.toPattern()); } } if (date == null) { throw new QueryProcessingException("Unable to parse date value \"" + value + "\""); } return date; } throw new QueryProcessingException("No conversion for type " + className); }
From source file:com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter.java
private String doConvertToString(Map<String, Object> context, Object value) { String result = null;// w ww. j av a 2 s. c o m if (value instanceof int[]) { int[] x = (int[]) value; List<Integer> intArray = new ArrayList<Integer>(x.length); for (int aX : x) { intArray.add(Integer.valueOf(aX)); } result = StringUtils.join(intArray, ", "); } else if (value instanceof long[]) { long[] x = (long[]) value; List<Long> longArray = new ArrayList<Long>(x.length); for (long aX : x) { longArray.add(Long.valueOf(aX)); } result = StringUtils.join(longArray, ", "); } else if (value instanceof double[]) { double[] x = (double[]) value; List<Double> doubleArray = new ArrayList<Double>(x.length); for (double aX : x) { doubleArray.add(new Double(aX)); } result = StringUtils.join(doubleArray, ", "); } else if (value instanceof boolean[]) { boolean[] x = (boolean[]) value; List<Boolean> booleanArray = new ArrayList<Boolean>(x.length); for (boolean aX : x) { booleanArray.add(new Boolean(aX)); } result = StringUtils.join(booleanArray, ", "); } else if (value instanceof Date) { DateFormat df = null; if (value instanceof java.sql.Time) { df = DateFormat.getTimeInstance(DateFormat.MEDIUM, getLocale(context)); } else if (value instanceof java.sql.Timestamp) { SimpleDateFormat dfmt = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, getLocale(context)); df = new SimpleDateFormat(dfmt.toPattern() + MILLISECOND_FORMAT); } else { df = DateFormat.getDateInstance(DateFormat.SHORT, getLocale(context)); } result = df.format(value); } else if (value instanceof String[]) { result = StringUtils.join((String[]) value, ", "); } return result; }
From source file:edu.ku.brc.specify.tests.PreferenceTest.java
/** * Tests the Date formating//from w w w .ja va 2s. c o m */ public void testDateFormatCache() { SimpleDateFormat format = new SimpleDateFormat("MM/DD/yyyy"); Date date = Calendar.getInstance().getTime(); AppPrefsCache.register(format, "ui", "formatting", "dateTest"); String newFormat = "yyyy/MM/DD"; AppPreferences.getRemote().put("ui.formatting.dateTest", newFormat); //try { // AppPreferences.getInstance().flush(); //} catch (BackingStoreException ex) {} log.info("New Date Format: " + format.toPattern()); log.info(format.format(date)); log.info("Actual pref value[" + AppPrefsCache.getValue("ui", "formatting", "dateTest") + "]"); log.info("newFormat[" + newFormat + "]"); assertTrue(AppPrefsCache.getValue("ui", "formatting", "dateTest").equals(newFormat)); assertTrue(format.toPattern().equals(newFormat)); }
From source file:com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter.java
private Object doConvertToDate(Map<String, Object> context, Object value, Class toType) { Date result = null;// w ww .j a v a 2s . co m if (value instanceof String && value != null && ((String) value).length() > 0) { String sa = (String) value; Locale locale = getLocale(context); DateFormat df = null; if (java.sql.Time.class == toType) { df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale); } else if (java.sql.Timestamp.class == toType) { Date check = null; SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale); SimpleDateFormat fullfmt = new SimpleDateFormat(dtfmt.toPattern() + MILLISECOND_FORMAT, locale); SimpleDateFormat dfmt = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale); SimpleDateFormat[] fmts = { fullfmt, dtfmt, dfmt }; for (SimpleDateFormat fmt : fmts) { try { check = fmt.parse(sa); df = fmt; if (check != null) { break; } } catch (ParseException ignore) { } } } else if (java.util.Date.class == toType) { Date check = null; DateFormat[] dfs = getDateFormats(locale); for (DateFormat df1 : dfs) { try { check = df1.parse(sa); df = df1; if (check != null) { break; } } catch (ParseException ignore) { } } } //final fallback for dates without time if (df == null) { df = DateFormat.getDateInstance(DateFormat.SHORT, locale); } try { df.setLenient(false); // let's use strict parsing (XW-341) result = df.parse(sa); if (!(Date.class == toType)) { try { Constructor constructor = toType.getConstructor(new Class[] { long.class }); return constructor.newInstance(new Object[] { Long.valueOf(result.getTime()) }); } catch (Exception e) { throw new XWorkException( "Couldn't create class " + toType + " using default (long) constructor", e); } } } catch (ParseException e) { throw new XWorkException("Could not parse date", e); } } else if (Date.class.isAssignableFrom(value.getClass())) { result = (Date) value; } return result; }
From source file:org.freeplane.features.format.FormatController.java
private PatternFormat createLocalPattern(String name, int dateStyle, Integer timeStyle) { final SimpleDateFormat simpleDateFormat = (SimpleDateFormat) (timeStyle == null ? SimpleDateFormat.getDateInstance(dateStyle, locale) : SimpleDateFormat.getDateTimeInstance(dateStyle, timeStyle, locale)); final String dStyle = PatternFormat.STYLE_DATE; final String dType = IFormattedObject.TYPE_DATE; return createFormat(simpleDateFormat.toPattern(), dStyle, dType, name, locale); }
From source file:net.zcarioca.zcommons.config.data.DatePropertyConverter.java
/** * {@inheritDoc}/*from w ww. j av a 2 s . com*/ */ @Override public Date convertPropertyValue(String value, BeanPropertyInfo beanPropertyInfo) throws ConfigurationException { SimpleDateFormat simpleDateFormat = getSimpleDateFormat(beanPropertyInfo); if (simpleDateFormat == null) { throw new ConfigurationException(String.format( "To convert a value to a date the field '%s' or class '%s' must be annotated with an @ConfigurableDateFormat", beanPropertyInfo.getPropertyName(), beanPropertyInfo.getBeanType().getSimpleName())); } try { return StringUtils.isBlank(value) ? null : simpleDateFormat.parse(value); } catch (ParseException exc) { throw new ConfigurationException( String.format("Could not format property %s of %s, value %s did not fit provided format %s", beanPropertyInfo.getPropertyName(), beanPropertyInfo.getBeanType().getSimpleName(), value, simpleDateFormat.toPattern())); } }
From source file:org.streets.commons.util.FastDateFormat.java
/** * <p>Gets a date formatter instance using the specified style, time * zone and locale.</p>//from w ww. j av a 2 s .c o m * * @param style date style: FULL, LONG, MEDIUM, or SHORT * @param timeZone optional time zone, overrides time zone of * formatted date * @param locale optional locale, overrides system locale * @return a localized standard date formatter * @throws IllegalArgumentException if the Locale has no date * pattern defined */ public static synchronized FastDateFormat getDateInstance(int style, TimeZone timeZone, Locale locale) { Object key = new Integer(style); if (timeZone != null) { key = new Pair(key, timeZone); } if (locale != null) { key = new Pair(key, locale); } FastDateFormat format = (FastDateFormat) cDateInstanceCache.get(key); if (format == null) { if (locale == null) { locale = Locale.getDefault(); } try { SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateInstance(style, locale); String pattern = formatter.toPattern(); format = getInstance(pattern, timeZone, locale); cDateInstanceCache.put(key, format); } catch (ClassCastException ex) { throw new IllegalArgumentException("No date pattern for locale: " + locale); } } return format; }
From source file:org.streets.commons.util.FastDateFormat.java
/** * <p>Gets a date/time formatter instance using the specified style, * time zone and locale.</p>//from ww w . j a v a 2 s .co m * * @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT * @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT * @param timeZone optional time zone, overrides time zone of * formatted date * @param locale optional locale, overrides system locale * @return a localized standard date/time formatter * @throws IllegalArgumentException if the Locale has no date/time * pattern defined */ public static synchronized FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle, TimeZone timeZone, Locale locale) { Object key = new Pair(new Integer(dateStyle), new Integer(timeStyle)); if (timeZone != null) { key = new Pair(key, timeZone); } if (locale != null) { key = new Pair(key, locale); } FastDateFormat format = (FastDateFormat) cDateTimeInstanceCache.get(key); if (format == null) { if (locale == null) { locale = Locale.getDefault(); } try { SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale); String pattern = formatter.toPattern(); format = getInstance(pattern, timeZone, locale); cDateTimeInstanceCache.put(key, format); } catch (ClassCastException ex) { throw new IllegalArgumentException("No date time pattern for locale: " + locale); } } return format; }
From source file:DateFormatUtils.java
/** * <p>Gets a date formatter instance using the specified style, time * zone and locale.</p>// w w w. j a v a2 s .co m * * @param style date style: FULL, LONG, MEDIUM, or SHORT * @param timeZone optional time zone, overrides time zone of * formatted date * @param locale optional locale, overrides system locale * @return a localized standard date formatter * @throws IllegalArgumentException if the Locale has no date * pattern defined */ public static synchronized FastDateFormat getDateInstance(int style, TimeZone timeZone, Locale locale) { Object key = new Integer(style); if (timeZone != null) { key = new Pair(key, timeZone); } if (locale == null) { locale = Locale.getDefault(); } key = new Pair(key, locale); FastDateFormat format = (FastDateFormat) cDateInstanceCache.get(key); if (format == null) { try { SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateInstance(style, locale); String pattern = formatter.toPattern(); format = getInstance(pattern, timeZone, locale); cDateInstanceCache.put(key, format); } catch (ClassCastException ex) { throw new IllegalArgumentException("No date pattern for locale: " + locale); } } return format; }