List of usage examples for java.text ParsePosition ParsePosition
public ParsePosition(int index)
From source file:org.kisoonlineapp.jsf.CalendarConverterInternal.java
@Override public Object getAsObject(FacesContext context, UIComponent component, String value) { final String trimmedValue = StringUtils.trimToNull(value); if (StringUtils.isBlank(trimmedValue)) { return null; }//from w w w . ja va2s.c o m final SimpleDateFormat sdf = createSimpleDateFormat(context, pattern); sdf.setLenient(false); final ParsePosition pos = new ParsePosition(0); final Date date = sdf.parse(trimmedValue, pos); if (pos.getErrorIndex() >= 0 || pos.getIndex() != trimmedValue.length()) { throw new ConverterException("Cannot parse " + trimmedValue); } final Calendar cal = kisoOnlineApp.getNow(); cal.setTime(date); return cal; }
From source file:nz.co.senanque.vaadinsupport.formatting.FormatterBigDecimal.java
public Object parse(String formattedValue) throws Exception { ParsePosition parsePosition = new ParsePosition(0); Object o = nf.parseObject(formattedValue); if (parsePosition.getIndex() != formattedValue.length()) { throw new RuntimeException("Invalid number " + formattedValue); }//from w w w .ja v a 2 s . c om return o; }
From source file:io.horizondb.model.core.util.TimeUtils.java
/** * Parses the specified <code>String</code> representing a date/time. * <p>The supported patterns are: 'yyyy-MM-dd', 'yyyy-MM-dd HH:mm:ss' and * 'yyyy-MM-dd HH:mm:ss.SSS'.</p>//from ww w. j a va2s . c om * * @param timeZone the date TimeZone * @param dateTime the <code>String</code> representing a date/time. * @return the time in millisecond since epoch */ public static long parseDateTime(TimeZone timeZone, String dateTime) { String pattern = getPattern(dateTime); SimpleDateFormat format = new SimpleDateFormat(pattern); Calendar calendar = Calendar.getInstance(timeZone); calendar.clear(); calendar.setLenient(false); format.setCalendar(calendar); Date date = format.parse(dateTime, new ParsePosition(0)); if (date == null) { throw new IllegalArgumentException(format("The value %s cannot be parsed into a valid date", dateTime)); } return date.getTime(); }
From source file:nz.co.senanque.vaadinsupport.formatting.FormatterDouble.java
public Object parse(String formattedValue) throws Exception { ParsePosition parsePosition = new ParsePosition(0); Object o = nf.parseObject(formattedValue, parsePosition); if (parsePosition.getIndex() != formattedValue.length()) { throw new RuntimeException("Invalid number " + formattedValue); }/* w w w .j a v a 2 s . c o m*/ return o; //Double.parseDouble(formattedValue); }
From source file:org.catechis.Transformer.java
/** * We want to transform a long date in this format: "EEE MMM dd HH:mm:ss zzz yyyy" * to a short date of this format: "MM/dd/yy" */// w w w.j a v a 2 s. com public static String simplifyDate(String long_date) { // Read (parse) the argument date into the used format. SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); Date in_date = null; ParsePosition pp = new ParsePosition(0); in_date = sdf.parse(long_date, pp); // transform it into a shorter format. SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy"); String result = formatter.format(in_date); return result; }
From source file:com.jeeframework.util.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a byte primitive. * *@param value The value validation is being performed on. *@param locale The locale to use to parse the number (system default if * null)//w w w. ja v a2 s . c om *@return the converted Byte value. */ public static Byte formatByte(String value, Locale locale) { Byte result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Byte.MIN_VALUE && num.doubleValue() <= Byte.MAX_VALUE) { result = new Byte(num.byteValue()); } } } return result; }
From source file:de.odysseus.calyxo.base.util.ParseUtils.java
private static Object parse(Format format, String value) throws ParseException { ParsePosition pos = new ParsePosition(0); Object result = format.parseObject(value, pos); if (pos.getIndex() < value.length()) throw new ParseException("Cannot parse " + value + " (garbage suffix)!", pos.getIndex()); return result; }//from ww w .j ava 2s .c o m
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a byte primitive. * /*from w ww . j a v a 2 s . com*/ * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if * null) * @return the converted Byte value. */ public static Byte formatByte(String value, Locale locale) { Byte result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Byte.MIN_VALUE && num.doubleValue() <= Byte.MAX_VALUE) { result = new Byte(num.byteValue()); } } } return result; }
From source file:org.sonar.api.utils.DateUtils.java
/** * @param s string in format {@link #DATE_FORMAT} * @throws SonarException when string cannot be parsed *///from w w w .jav a 2s . c o m public static Date parseDate(String s) { ParsePosition pos = new ParsePosition(0); Date result = THREAD_SAFE_DATE_FORMAT.parse(s, pos); if (pos.getIndex() != s.length()) { throw new SonarException("The date '" + s + "' does not respect format '" + DATE_FORMAT + "'"); } return result; }
From source file:org.xlcloud.openstack.model.climate.json.OpenStackDateDeserializer.java
private Date parseDate(String input, String[] datePatterns) throws ParseException { SimpleDateFormat parser = new SimpleDateFormat(); parser.setLenient(true);/*from www. j a v a2 s.co m*/ parser.setTimeZone(DateUtils.UTC_TIME_ZONE); ParsePosition position = new ParsePosition(0); for (String datePattern : datePatterns) { parser.applyPattern(datePattern); Date date = parser.parse(input, position); if (date != null && position.getIndex() == input.length()) { return date; } } throw new ParseException("Unable to parse the date: " + input, -1); }