private Date parseDate(String str, String[] parsePatterns, Locale locale) throws ParseException { if ((str == null) || (parsePatterns == null)) { throw new IllegalArgumentException("Date and Patterns must not be null"); } SimpleDateFormat parser = null; ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { if (i == 0) { parser = new SimpleDateFormat(parsePatterns[0], locale); } else { parser.applyPattern(parsePatterns[i]); } pos.setIndex(0); Date date = parser.parse(str, pos); if ((date != null) && (pos.getIndex() == str.length())) { return date; } } throw new ParseException("Unable to parse the date: " + str, -1); } }
private Date _parseDateFromLong(String longStr, ParsePosition pos) throws ParseException { long ts; try { ts = NumberInput.parseLong(longStr); } catch (NumberFormatException e) { throw new ParseException(String.format( "Timestamp value %s out of 64-bit value range", longStr), pos.getErrorIndex()); } return new Date(ts); }
StringBuilder dateText = new StringBuilder( text.substring( pos.getIndex() ) ); ParsePosition positionError = new ParsePosition( 0 ); tempDate = super.parse( dateText.toString(), positionError ); if ( tempDate != null ) { pos.setErrorIndex( pos.getIndex() ); return null; int startNanosecondsPosition = positionError.getErrorIndex(); int endNanosecondsPosition = endNanosecondPatternPosition - startNanosecondPatternPosition + 1 + startNanosecondsPosition; String nanoseconds = String.valueOf( dateText.subSequence( startNanosecondsPosition, endNanosecondsPosition ) ); dateText.delete( startNanosecondsPosition, endNanosecondsPosition ); ParsePosition position = new ParsePosition( 0 ); dateText.append( NANOSECOND_PLACEHOLDER ); tempDate = super.parse( dateText.toString(), position ); if ( tempDate == null ) { pos.setErrorIndex( position.getErrorIndex() ); return null; String result = timestampFormatDate + '.' + nanoseconds; Timestamp res = Timestamp.valueOf( timestampFormatDate + '.' + nanoseconds ); pos.setIndex( pos.getIndex() + result.length() ); return res;
/** * <p>Parsing is not supported.</p> * * @param source the string to parse * @param pos the parsing position * @return <code>null</code> as not supported */ public Object parseObject(String source, ParsePosition pos) { pos.setIndex(0); pos.setErrorIndex(0); return null; }
public static boolean isNumeric(String str) { NumberFormat formatter = NumberFormat.getInstance(); ParsePosition pos = new ParsePosition(0); formatter.parse(str, pos); return str.length() == pos.getIndex(); }
private long visitDateTimeConstructor() { DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH); return new Date().getTime(); } else if (token.getType() == JsonTokenType.STRING) { verifyToken(JsonTokenType.RIGHT_PAREN); String s = token.getValue(String.class); ParsePosition pos = new ParsePosition(0); Date dateTime = format.parse(s, pos); if (dateTime != null && pos.getIndex() == s.length()) { return dateTime.getTime(); } else { throw new JsonParseException("JSON reader expected a date in 'EEE MMM dd yyyy HH:mm:ss z' format but found '%s'.", s); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); calendar.set(Calendar.YEAR, (int) values[0]); calendar.set(Calendar.MONTH, (int) values[1]); calendar.set(Calendar.DAY_OF_MONTH, (int) values[2]); calendar.set(Calendar.HOUR_OF_DAY, (int) values[3]);
public static String MakePrettyDate(String getCreatedTime) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ", Locale.US); ParsePosition pos = new ParsePosition(0); long then = formatter.parse(getCreatedTime, pos).getTime(); long now = new Date().getTime(); long seconds = (now - then) / 1000; long minutes = seconds / 60; long hours = minutes / 60; long days = hours / 24; String friendly = null; long num = 0; if (days > 0) { num = days; friendly = days + " day"; } else if (hours > 0) { num = hours; friendly = hours + " hour"; } else if (minutes > 0) { num = minutes; friendly = minutes + " minute"; } else { num = seconds; friendly = seconds + " second"; } if (num > 1) { friendly += "s"; } String postTimeStamp = friendly + " ago"; return postTimeStamp; }
private long visitISODateTimeConstructor() { verifyToken(JsonTokenType.LEFT_PAREN); JsonToken token = popToken(); if (token.getType() == JsonTokenType.RIGHT_PAREN) { return new Date().getTime(); } else if (token.getType() != JsonTokenType.STRING) { throw new JsonParseException("JSON reader expected a string but found '%s'.", token.getValue()); } verifyToken(JsonTokenType.RIGHT_PAREN); String[] patterns = {"yyyy-MM-dd", "yyyy-MM-dd'T'HH:mm:ssz", "yyyy-MM-dd'T'HH:mm:ss.SSSz"}; SimpleDateFormat format = new SimpleDateFormat(patterns[0], Locale.ENGLISH); ParsePosition pos = new ParsePosition(0); String s = token.getValue(String.class); if (s.endsWith("Z")) { s = s.substring(0, s.length() - 1) + "GMT-00:00"; } for (final String pattern : patterns) { format.applyPattern(pattern); format.setLenient(true); pos.setIndex(0); Date date = format.parse(s, pos); if (date != null && pos.getIndex() == s.length()) { return date.getTime(); } } throw new JsonParseException("Invalid date format."); }
public static int decodeTime( String s, String dateFormat ) throws Exception { SimpleDateFormat f = new SimpleDateFormat( dateFormat ); TimeZone utcTimeZone = TimeZone.getTimeZone( "UTC" ); f.setTimeZone( utcTimeZone ); f.setLenient( false ); ParsePosition p = new ParsePosition( 0 ); Date d = f.parse( s, p ); if ( d == null ) { throw new Exception( "Invalid time value " + dateFormat + ": \"" + s + "\"." ); } return (int) d.getTime(); }
Calendar working = (Calendar) serverTime.clone(); working.setTimeZone(getServerTimeZone()); // is this needed? String year = Integer.toString(now.get(Calendar.YEAR)); String timeStampStrPlusYear = timestampStr + " " + year; SimpleDateFormat hackFormatter = new SimpleDateFormat(recentDateFormat.toPattern() + " yyyy", recentDateFormat.getDateFormatSymbols()); hackFormatter.setLenient(false); hackFormatter.setTimeZone(recentDateFormat.getTimeZone()); ParsePosition pp = new ParsePosition(0); parsed = hackFormatter.parse(timeStampStrPlusYear, pp); if (parsed != null && pp.getIndex() == timeStampStrPlusYear.length()) { working.setTime(parsed); if (working.after(now)) { // must have been last year instead ParsePosition pp = new ParsePosition(0); parsed = defaultDateFormat.parse(timestampStr, pp); if (parsed != null && pp.getIndex() == timestampStr.length()) { working.setTime(parsed); } else { throw new ParseException( "Timestamp '"+timestampStr+"' could not be parsed using a server time of " +serverTime.getTime().toString(), pp.getErrorIndex());
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST"); pdt.setStartRule(DateFields.APRIL, 1, DateFields.SUNDAY, 2*60*60*1000); pdt.setEndRule(DateFields.OCTOBER, -1, DateFields.SUNDAY, 2*60*60*1000); // Format the current time. SimpleDateFormat formatter = new SimpleDateFormat ("yyyy.mm.dd e 'at' hh:mm:ss a zzz"); Date currentTime_1 = new Date(); String dateString = formatter.format(currentTime_1); // Parse the previous string back into a Date. ParsePosition pos = new ParsePosition(0); Date currentTime_2 = formatter.parse(dateString, pos);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); ParsePosition pp = new ParsePosition(0); theDate = df.parse(s, pp); } catch (Exception e) { throw badTimestampString(s); int position = pp.getIndex(); int remaining = s.length() - position; int theNanos; Timestamp theTimestamp = new Timestamp(theDate.getTime()); theTimestamp.setNanos(theNanos);
} else if (b.containsField("$date")) { if (b.get("$date") instanceof Number) { o = new Date(((Number) b.get("$date")).longValue()); } else { SimpleDateFormat format = new SimpleDateFormat(_msDateFormat); format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT"))); o = format.parse(b.get("$date").toString(), new ParsePosition(0)); format = new SimpleDateFormat(_secDateFormat); format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT"))); o = format.parse(b.get("$date").toString(), new ParsePosition(0));
Calendar now = Calendar.getInstance(); now.set(Calendar.MILLISECOND, 0); computedValue = FormatAndPrecision.MILLISECOND.getFormat().format(now.getTime()); ParsePosition pos = new ParsePosition(0); Date time = f.getFormat().parse(computedValue, pos); if (pos.getIndex() == computedValue.length()) { DateRange range = f.expand(time); if (range.getMinValue().equals(range.getMaxValue())) { return range.getMinValue(); } else { throw new ParseException("Invalid date: " + value, 0);
int offset = pParsePosition.getIndex(); int length = pString.length(); SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); try { Date d = df.parse(pString.substring(offset)); pParsePosition.setIndex(length); Calendar c = Calendar.getInstance(); c.setTime(d); return c; } catch (ParseException e) { pParsePosition.setErrorIndex(offset); return null; ++offset; } else { pParsePosition.setErrorIndex(offset); return null; pParsePosition.setErrorIndex(offset); return null; ++offset; } else { pParsePosition.setErrorIndex(offset); return null; pParsePosition.setErrorIndex(offset);
@Test public void testInputHasMoreCharacters() { final FastDateParser parser = new FastDateParser("MM/dd", TimeZone.getDefault(), Locale.getDefault()); final ParsePosition parsePosition = new ParsePosition(0); final Date date = parser.parse("3/23/61", parsePosition); assertEquals(4, parsePosition.getIndex()); final Calendar calendar = Calendar.getInstance(); calendar.setTime(date); assertEquals(2, calendar.get(Calendar.MONTH)); assertEquals(23, calendar.get(Calendar.DATE)); }
SimpleDateFormat sf = new SimpleDateFormat(format); sf.setTimeZone(TimeZone.getTimeZone("UTC")); //sf.setCalendar(Calendar.getInstance()); ParsePosition pp = new ParsePosition(0); Date date = sf.parse(Value,pp); if (pp.getIndex() == 0) { Log.e(TAG,"Can't getDate with format:\""+format+"\" and value:\""+Value + "\" at char index:"+pp.getErrorIndex()); return Calendar.getInstance(); } Calendar cal = Calendar.getInstance(); cal.setTime(date);
Exception fail = null; try { int offset = pos.getIndex(); Calendar calendar = new GregorianCalendar(year, month - 1, day); pos.setIndex(offset); return calendar.getTime(); timezone = TimeZone.getTimeZone(timezoneId); String act = timezone.getID(); if (!act.equals(timezoneId)) { if (!cleaned.equals(timezoneId)) { throw new IndexOutOfBoundsException("Mismatching time zone indicator: "+timezoneId+" given, resolves to " +timezone.getID()); calendar.set(Calendar.MILLISECOND, milliseconds); pos.setIndex(offset); return calendar.getTime(); msg = "("+fail.getClass().getName()+")"; ParseException ex = new ParseException("Failed to parse date " + input + ": " + msg, pos.getIndex()); ex.initCause(fail); throw ex;
ParsePosition position = new ParsePosition(0); Date result = STANDARD_DATE_FORMAT.get().parse(value, position); if (position.getIndex() == value.length()) { DateFormat format = BROWSER_COMPATIBLE_DATE_FORMATS[i]; if (format == null) { format = new SimpleDateFormat(BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS[i], Locale.US); format.setTimeZone(UTC); BROWSER_COMPATIBLE_DATE_FORMATS[i] = format; position.setIndex(0); result = format.parse(value, position); if (position.getIndex() != 0) {