/** * Uses the parser Format instance. * * @param source the String source * @param pos the ParsePosition containing the position to parse from, will * be updated according to parsing success (index) or failure * (error index) * @return the parsed Object * @see Format#parseObject(String, ParsePosition) */ @Override public Object parseObject(final String source, final ParsePosition pos) { return parser.parseObject(source, pos); }
/** * Uses the parser Format instance. * * @param source the String source * @param pos the ParsePosition containing the position to parse from, will * be updated according to parsing success (index) or failure * (error index) * @return the parsed Object * @see Format#parseObject(String, ParsePosition) */ public Object parseObject(String source, ParsePosition pos) { return parser.parseObject(source, pos); }
String input = "Aug 31 09:53:19 2011"; DateFormat df = new SimpleDateFormat("MMM dd HH:mm:ss yyyy", Locale.US); System.out.println(df.parseObject(input));
public class DateLabelFormatter extends AbstractFormatter { private String datePattern = "yyyy-MM-dd"; private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern); @Override public Object stringToValue(String text) throws ParseException { return dateFormatter.parseObject(text); } @Override public String valueToString(Object value) throws ParseException { if (value != null) { Calendar cal = (Calendar) value; return dateFormatter.format(cal.getTime()); } return ""; } }
/** * Parses the specified string using the rules of this format. * * @param string * the string to parse. * @return the object resulting from the parse. * @throws ParseException * if an error occurs during parsing. */ public Object parseObject(String string) throws ParseException { ParsePosition position = new ParsePosition(0); Object result = parseObject(string, position); if (position.getIndex() == 0) { throw new ParseException("Parse failure", position.getErrorIndex()); } return result; }
public static Object getFiscalDate( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { if ( ArgList.length == 2 ) { try { if ( isNull( ArgList ) ) { return null; } else if ( isUndefined( ArgList ) ) { return undefinedValue; } java.util.Date dIn = (java.util.Date) ArgList[0]; Calendar startDate = Calendar.getInstance(); Calendar fisStartDate = Calendar.getInstance(); Calendar fisOffsetDate = Calendar.getInstance(); startDate.setTime( dIn ); Format dfFormatter = new SimpleDateFormat( "dd.MM.yyyy" ); String strOffsetDate = (String) ArgList[1] + String.valueOf( startDate.get( Calendar.YEAR ) ); java.util.Date dOffset = (java.util.Date) dfFormatter.parseObject( strOffsetDate ); fisOffsetDate.setTime( dOffset ); String strFisStartDate = "01.01." + String.valueOf( startDate.get( Calendar.YEAR ) + 1 ); fisStartDate.setTime( (java.util.Date) dfFormatter.parseObject( strFisStartDate ) ); int iDaysToAdd = (int) ( ( startDate.getTimeInMillis() - fisOffsetDate.getTimeInMillis() ) / 86400000 ); fisStartDate.add( Calendar.DATE, iDaysToAdd ); return fisStartDate.getTime(); } catch ( Exception e ) { throw new RuntimeException( e.toString() ); } } else { throw new RuntimeException( "The function call getFiscalDate requires 2 arguments." ); } }
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.GERMAN); df.setParseBigDecimal(true); BigDecimal bd = (BigDecimal) df.parseObject(numberString); System.out.println(bd.toString()); } catch (ParseException e) {
/** * <p>Parse the value with the specified <code>Format</code>.</p> * * @param value The value to be parsed. * @param formatter The Format to parse the value with. * @return The parsed value if valid or <code>null</code> if invalid. */ protected Object parse(String value, Format formatter) { ParsePosition pos = new ParsePosition(0); Object parsedValue = formatter.parseObject(value, pos); if (pos.getErrorIndex() > -1) { return null; } if (isStrict() && pos.getIndex() < value.length()) { return null; } if (parsedValue != null) { parsedValue = processParsedValue(parsedValue, formatter); } return parsedValue; }
public static Object getFiscalDate( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length == 2 ) { try { if ( isNull( ArgList ) ) { return null; } else if ( isUndefined( ArgList ) ) { return Context.getUndefinedValue(); } java.util.Date dIn = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class ); Calendar startDate = Calendar.getInstance(); Calendar fisStartDate = Calendar.getInstance(); Calendar fisOffsetDate = Calendar.getInstance(); startDate.setTime( dIn ); Format dfFormatter = new SimpleDateFormat( "dd.MM.yyyy" ); String strOffsetDate = Context.toString( ArgList[1] ) + String.valueOf( startDate.get( Calendar.YEAR ) ); java.util.Date dOffset = (java.util.Date) dfFormatter.parseObject( strOffsetDate ); fisOffsetDate.setTime( dOffset ); String strFisStartDate = "01.01." + String.valueOf( startDate.get( Calendar.YEAR ) + 1 ); fisStartDate.setTime( (java.util.Date) dfFormatter.parseObject( strFisStartDate ) ); int iDaysToAdd = (int) ( ( startDate.getTimeInMillis() - fisOffsetDate.getTimeInMillis() ) / 86400000 ); fisStartDate.add( Calendar.DATE, iDaysToAdd ); return fisStartDate.getTime(); } catch ( Exception e ) { throw Context.reportRuntimeError( e.toString() ); } } else { throw Context.reportRuntimeError( "The function call getFiscalDate requires 2 arguments." ); } }
public Object parseObject( String source, ParsePosition pos ) { int initialIndex = pos.getIndex(); Object result = fDelegate.parseObject( source, pos ); if ( result != null && pos.getIndex() < source.length() ) { int errorIndex = pos.getIndex();
@Override public void run() { for(int j= 0; j<NROUNDS; ++j) { try { final Date date= new Date(); final long t0= System.currentTimeMillis(); final String formattedDate= printer.format(date); totalElapsed.addAndGet(0, System.currentTimeMillis() - t0); final long t1 = System.currentTimeMillis(); final Object pd= parser.parseObject(formattedDate); totalElapsed.addAndGet(1, System.currentTimeMillis() - t1); if(!date.equals(pd)) { failures.incrementAndGet(); } } catch (final Exception e) { failures.incrementAndGet(); e.printStackTrace(); } } } });
protected void parseHelperSuccess(Format f, String parseString, Object expectedResult) { Object result; // First check the parseObject(String) method. try { result = f.parseObject(parseString); result.equals(expectedResult); if (expectedResult == null) { assertTrue(result == expectedResult); } else { assertEquals(result, expectedResult); } } catch (ParseException e) { fail(); } } }
parse = format.parseObject(string, internalPos); if (internalPos.getErrorIndex() != -1) { position.setErrorIndex(offset);
oRC = dfFormatter.parseObject( sArg1 ); sArg2 = (String) ArgList[1]; Format dfFormatter = new SimpleDateFormat( sArg2 ); oRC = dfFormatter.parseObject( sArg1 ); } catch ( Exception e ) { throw new RuntimeException( "Could not apply the given format " Locale dfLocale = EnvUtil.createLocale( sArg3 ); dfFormatter = new SimpleDateFormat( sArg2, dfLocale ); oRC = dfFormatter.parseObject( sArg1 ); } else { throw new RuntimeException( "Locale " + sArg3 + " is not 2 characters long." );
return localSimpleDateFormat.get().parseObject(source);
oRC = dfFormatter.parseObject( sArg1 ); sArg2 = Context.toString( ArgList[1] ); Format dfFormatter = new SimpleDateFormat( sArg2 ); oRC = dfFormatter.parseObject( sArg1 ); } catch ( Exception e ) { throw Context.reportRuntimeError( "Could not apply the given format " Locale dfLocale = EnvUtil.createLocale( sArg3 ); dfFormatter = new SimpleDateFormat( sArg2, dfLocale ); oRC = dfFormatter.parseObject( sArg1 ); } else { throw Context.reportRuntimeError( "Locale " + sArg3 + " is not 2 characters long." );
/** * Parses text from a string to produce an object. This method delegates the work to the * {@linkplain Format format} specified at construction time. This method to not perform any * logging. * * @param text The text to parse. * @param position Index and error index information. * @return An object parsed from the string, or {@code null} in case of error. */ public Object parseObject(final String text, final ParsePosition position) { return format.parseObject(text, position); }
public static Time parseTime(String timeValue) { try { return (Time)timeFormat.get().parseObject(timeValue); } catch (ParseException e) { throw new IllegalDataException(e); } }
private static Date toDate(String dateString) { try { return (Date)format.parseObject(dateString); } catch (ParseException e) { throw new RuntimeException(e); } }
/** * Formats an object and parse the result. The format output is compared with the expected * output. */ private static void assertFormat(final String expected, final Object value, final Format format) throws ParseException { final String label = value.toString(); final String text = format.format(value); assertEquals("Formatting of \"" + label + '"', expected, text); assertEquals("Parsing of \"" + label + '"', value, format.parseObject(text)); }