List of usage examples for java.text ParsePosition ParsePosition
public ParsePosition(int index)
From source file:org.miloss.fgsms.common.Utility.java
/** * Parses the date/time stamp from a given input parameter using one of the * following formats<br> <ul> <li>mm/dd/yyyy</li> <li>mm/dd/yyyy * hh:mm:ss</li> <li>EEE MMM dd HH:mm:ss zzz yyyy - this is the standard * output from the unix date command</li> <li>EEE mm/dd/yyyy HH:mm:ss.ms - * this is the standard output from the windows command echo %date% * %time%</li> <li>yyyy-MM-ddThh:mm:ss.zzzzzzz</li> <li>Epoch time (ms)</li> * <li>PnYnMnDTnHnMnS - XML lexical representation</li> </ul> * * @param s the input string// w ww.j a va2s. c om * @return a non-null instance of Calendar matching the interpreted date. * @throws Exception if the date cannot be parsed */ public static Calendar parseDateTime(String s) throws Exception { //this is what the calendar widget gives us SimpleDateFormat f1 = new SimpleDateFormat("MM/dd/yyyy"); SimpleDateFormat f2 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); //std unix date format SimpleDateFormat f3 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); //std windows date format SimpleDateFormat f4 = new SimpleDateFormat("EEE MM/dd/yyyy HH:mm:ss.ms"); SimpleDateFormat f5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.zzzzzzz"); Date d = null; try { d = f1.parse(s, new ParsePosition(0)); } catch (Exception ex) { } if (d == null) { try { d = f2.parse(s, new ParsePosition(0)); } catch (Exception ex) { } } if (d == null) { try { d = f3.parse(s, new ParsePosition(0)); } catch (Exception ex) { } } if (d == null) { try { d = f4.parse(s, new ParsePosition(0)); } catch (Exception ex) { } } if (d == null) { try { d = f5.parse(s, new ParsePosition(0)); } catch (Exception ex) { } } if (d != null) { GregorianCalendar gcal = new GregorianCalendar(); gcal.setTime(d); return (gcal); } try { long timestamp = Long.parseLong(s); GregorianCalendar gcal = new GregorianCalendar(); gcal.setTimeInMillis(timestamp); return (gcal); } catch (Exception x) { } throw new Exception("Unable to parse the date, see documentation for correct usage"); }
From source file:com.novartis.opensource.yada.adaptor.OracleAdaptor.java
/** * Enables checking for {@link JDBCAdaptor#ORACLE_TIMESTAMP_FMT} if {@code val} does not conform to {@link JDBCAdaptor#STANDARD_TIMESTAMP_FMT} * @since 5.1.1/*from w ww . ja v a 2s.c om*/ */ @Override protected void setTimestampParameter(PreparedStatement pstmt, int index, char type, String val) throws SQLException { if (EMPTY.equals(val) || val == null) { pstmt.setNull(index, java.sql.Types.DATE); } else { SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_TIMESTAMP_FMT); ParsePosition pp = new ParsePosition(0); Date dateVal = sdf.parse(val, pp); if (dateVal == null) { sdf = new SimpleDateFormat(ORACLE_TIMESTAMP_FMT); pp = new ParsePosition(0); dateVal = sdf.parse(val, pp); } if (dateVal != null) { long t = dateVal.getTime(); java.sql.Timestamp sqlDateVal = new java.sql.Timestamp(t); pstmt.setTimestamp(index, sqlDateVal); } } }
From source file:com.jeeframework.util.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a long primitive. * *@param value The value validation is being performed on. *@param locale The locale to use to parse the number (system default if * null)//from ww w . jav a 2 s . c o m *@return the converted Long value. */ public static Long formatLong(String value, Locale locale) { Long 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() >= Long.MIN_VALUE && num.doubleValue() <= Long.MAX_VALUE) { result = new Long(num.longValue()); } } } return result; }
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 . j a va2s. c o m*/ * @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:mml.handler.scratch.ScratchVersion.java
/** * Convert a string date back to a Date object * @param date the date previously generated by toJSON * @return a Date/* ww w . j a va 2 s . c o m*/ */ static Date toDate(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ParsePosition pp = new ParsePosition(0); return sdf.parse(date, pp); }
From source file:com.anrisoftware.globalpom.format.byteformat.ByteFormat.java
/** * Parses the specified string to computer byte. * <p>//from ww w. ja v a 2 s . c o m * The format follows the pattern: * * <pre> * <value>(byte|kB,MB,...,YB|KiB,MiB,...,YiB) * </pre> * * <p> * <h2>Examples</h2> * <p> * <ul> * <li>{@code "64 byte"} * <li>{@code "1 kb"} * <li>{@code "1 Kib"} * </ul> * * @param multiplier * the unit {@link UnitMultiplier}. * * @return the parsed value. * * @throws ParseException * if the string cannot be parsed to a value. */ public long parse(String source, UnitMultiplier multiplier) throws ParseException { ParsePosition pos = new ParsePosition(0); Long result = parse(source, pos, multiplier); if (pos.getIndex() == 0) { throw log.errorParseValue(source, pos); } return result; }
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a long primitive. * //from www. j a v a 2 s .c o m * @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 Long value. */ public static Long formatLong(String value, Locale locale) { Long 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() >= Long.MIN_VALUE && num.doubleValue() <= Long.MAX_VALUE) { result = new Long(num.longValue()); } } } return result; }
From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a long primitive. * /* ww w . j a v a 2s. co m*/ * @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 Long value. */ public static Long formatLong(String value, Locale locale) { Long 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() >= Long.MIN_VALUE && num.doubleValue() <= Long.MAX_VALUE) { result = Long.valueOf(num.longValue()); } } } return result; }
From source file:gov.nih.nci.cabig.caaers.utils.DateUtils.java
public static Date parseDate(String dateStr, String... parsePatterns) throws ParseException { if (dateStr == null || parsePatterns == null) { throw new IllegalArgumentException("Date and Patterns must not be null"); }// w ww . j av a 2 s . com String strDate = dateStr; //do year correction. (partial year >=50 will be 1999 and <50 will be 2000) String[] parts = StringUtils.split(dateStr, '/'); int len = parts.length; if (len != 3 || parts[0].length() > 2 || parts[1].length() > 2) throw new ParseException("Unable to parse the date " + strDate, -1); String yStr = parts[2]; if (!(yStr.length() == 4 || yStr.length() == 2 || yStr.length() == 10)) throw new ParseException("Unable to parse the date " + strDate, -1); if (yStr.length() == 2 && StringUtils.isNumeric(yStr)) { if (Integer.parseInt(yStr) < 50) yStr = "20" + yStr; else yStr = "19" + yStr; parts[2] = yStr; strDate = StringUtils.join(parts, '/'); } //BJ: date formats are not thread save, so we need to create one each time. SimpleDateFormat parser = null; ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { if (i == 0) { parser = new SimpleDateFormat(parsePatterns[0]); } else { parser.applyPattern(parsePatterns[i]); } pos.setIndex(0); Date date = parser.parse(strDate, pos); if (date != null && pos.getIndex() == strDate.length()) { return date; } } throw new ParseException("Unable to parse the date: " + strDate, -1); }