List of usage examples for java.text ParsePosition ParsePosition
public ParsePosition(int index)
From source file:org.ramadda.util.Utils.java
/** * _more_/*from ww w . j a va2 s .c o m*/ * * @param dttm _more_ * * @return _more_ */ public static Date parseDate(String dttm) { if (dateFormats == null) { String[] formats = { "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd HH:mm:ss z", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "yyyyMMddHHmmss", "yyyyMMddHHmm", "yyyyMMddHH", "yyyyMMdd" }; dateFormats = new ArrayList<SimpleDateFormat>(); for (int i = 0; i < formats.length; i++) { SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(formats[i]); dateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT")); dateFormats.add(dateFormat); } } int cnt = 0; ParsePosition pp = new ParsePosition(0); for (SimpleDateFormat dateFormat : dateFormats) { Date date = dateFormat.parse(dttm, pp); if (date != null) { return date; } } return null; }
From source file:org.orcid.core.manager.impl.OrcidProfileManagerImpl.java
/** * Replace the funding amount string into the desired format * // www.j a v a2 s . c o m * @param updatedOrcidProfile * The profile containing the new funding * */ private void setFundingAmountsWithTheCorrectFormat(OrcidProfile updatedOrcidProfile) throws IllegalArgumentException { FundingList fundings = updatedOrcidProfile.retrieveFundings(); for (Funding funding : fundings.getFundings()) { // If the amount is not empty, update it if (funding.getAmount() != null && StringUtils.isNotBlank(funding.getAmount().getContent())) { String amount = funding.getAmount().getContent(); Locale locale = localeManager.getLocale(); ParsePosition parsePosition = new ParsePosition(0); DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale); DecimalFormatSymbols symbols = numberFormat.getDecimalFormatSymbols(); /** * When spaces are allowed, the grouping separator is the * character 160, which is a non-breaking space So, lets change * it so it uses the default space as a separator * */ if (symbols.getGroupingSeparator() == 160) { symbols.setGroupingSeparator(' '); } numberFormat.setDecimalFormatSymbols(symbols); Number number = numberFormat.parse(amount, parsePosition); String formattedAmount = number.toString(); if (parsePosition.getIndex() != amount.length()) { double example = 1234567.89; NumberFormat numberFormatExample = NumberFormat.getNumberInstance(localeManager.getLocale()); throw new IllegalArgumentException( "The amount: " + amount + " doesn'n have the right format, it should use the format: " + numberFormatExample.format(example)); } funding.getAmount().setContent(formattedAmount); } } }
From source file:cfa.vo.sed.science.stacker.SedStackerFrame.java
private static boolean isNumeric(String str) { NumberFormat formatter = NumberFormat.getInstance(); ParsePosition pos = new ParsePosition(0); formatter.parse(str, pos);/*from ww w .j av a 2 s . c o m*/ return str.length() == pos.getIndex(); }
From source file:org.pentaho.di.core.Const.java
public static int decodeTime(String s, String DateFormat) throws Exception { SimpleDateFormat f = new SimpleDateFormat(DateFormat); TimeZone utcTimeZone = TimeZone.getTimeZone("UTC"); f.setTimeZone(utcTimeZone);// w w w . j a v a 2 s .c o m 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(); }
From source file:de.innovationgate.wgpublisher.webtml.utils.TMLContext.java
public Date stringtodate(String text) { if (text == null) { return null; }//from ww w. j a v a2 s . co m if (text.indexOf(":") == -1) { text += " 0:00"; } SimpleDateFormat dateFormat = new SimpleDateFormat(); dateFormat.setLenient(true); return dateFormat.parse(text, new ParsePosition(0)); }
From source file:de.escidoc.core.test.EscidocTestBase.java
/** * Creates a <code>java.util.Calendar</code> object from an xml dateTime string. * /* w w w . j a va 2 s. c om*/ * @param dateTime * The xml dateTime string. * @return The Calendar object. * @throws ParseException * If the dateTime string can not be correctly parsed. */ public static Calendar getCalendarFromXmlDateString(String dateTime) throws ParseException { Calendar cal = null; if (dateTime.length() >= 20 && dateTime.length() <= 23) { // no timezone // ensure 3 digits for millis int add = 23 - dateTime.length(); while (add > 0) { dateTime += "0"; add--; } dateTime += "+0000"; } else if (dateTime.length() == 19) { // no timezone // not 3 digits for millis // no dot dateTime += ".000+0000"; } // else if (dateTime.length() == 18) { // // no timezone // // not 3 digits for millis // // no dot // dateTime += "0.000+0000"; // } // else if (dateTime.length() == 17) { // // no timezone // // not 3 digits for millis // // no dot // dateTime += "00.000+0000"; // } // else if (dateTime.length() == 16) { // // no timezone // // not 3 digits for millis // // no dot // dateTime += ":00.000+0000"; // } TimeZone gmt = TimeZone.getTimeZone("GMT"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); DateFormat tf = new SimpleDateFormat("HH:mm:ss.SSS"); Date oldModDateDate = df.parse(dateTime); Date oldModDateTime = tf.parse(dateTime, new ParsePosition(11)); // DateFormat f = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss.SSS"); // dateTime = dateTime.replace('T', '-'); // dateTime = dateTime.trim(); cal = Calendar.getInstance(); // cal.setTime(f.parse(dateTime)); cal.setTimeZone(gmt); cal.setTime(oldModDateDate); long oldModDateDateMillis = cal.getTimeInMillis(); cal.setTime(oldModDateTime); long oldModDateTimeMillis = cal.getTimeInMillis(); long oldModDateMillis = oldModDateDateMillis + oldModDateTimeMillis; cal.setTimeInMillis(oldModDateMillis); return cal; }
From source file:com.clark.func.Functions.java
/** * <p>/*from w ww . ja va2 s .com*/ * Parses a string representing a date by trying a variety of different * parsers. * </p> * * <p> * The parse will try each parse pattern in turn. A parse is only deemed * successful if it parses the whole of the input string. If no parse * patterns match, a ParseException is thrown. * </p> * * @param str * the date to parse, not null * @param parsePatterns * the date format patterns to use, see SimpleDateFormat, not * null * @param lenient * Specify whether or not date/time parsing is to be lenient. * @return the parsed date * @throws IllegalArgumentException * if the date string or pattern array is null * @throws ParseException * if none of the date patterns were suitable * @see java.util.Calender#isLenient() */ private static Date parseDateWithLeniency(String str, String[] parsePatterns, boolean lenient) throws ParseException { if (str == null || parsePatterns == null) { throw new IllegalArgumentException("Date and Patterns must not be null"); } SimpleDateFormat parser = new SimpleDateFormat(); parser.setLenient(lenient); ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { String pattern = parsePatterns[i]; // LANG-530 - need to make sure 'ZZ' output doesn't get passed to // SimpleDateFormat if (parsePatterns[i].endsWith("ZZ")) { pattern = pattern.substring(0, pattern.length() - 1); } parser.applyPattern(pattern); pos.setIndex(0); String str2 = str; // LANG-530 - need to make sure 'ZZ' output doesn't hit // SimpleDateFormat as it will ParseException if (parsePatterns[i].endsWith("ZZ")) { str2 = str.replaceAll("([-+][0-9][0-9]):([0-9][0-9])$", "$1$2"); } Date date = parser.parse(str2, pos); if (date != null && pos.getIndex() == str2.length()) { return date; } } throw new ParseException("Unable to parse the date: " + str, -1); }