List of usage examples for java.text ParsePosition ParsePosition
public ParsePosition(int index)
From source file:org.noorganization.instalistsynch.controller.local.dba.impl.ClientLogDbController.java
@Override public Date getLeastRecentUpdateTimeForUuid(String _clientUuid) { Cursor cursor = mContentResolver.query(Uri.withAppendedPath(InstalistProvider.BASE_CONTENT_URI, "log"), LogInfo.COLUMN.ALL_COLUMNS, LogInfo.COLUMN.ITEM_UUID + " LIKE ? ", new String[] { _clientUuid }, LogInfo.COLUMN.ACTION_DATE + " DESC "); if (cursor == null) { return null; }/* w w w . j a v a 2 s .c o m*/ if (cursor.getCount() == 0) { cursor.close(); return null; } cursor.moveToFirst(); String date = cursor.getString(cursor.getColumnIndex(LogInfo.COLUMN.ACTION_DATE)); try { return ISO8601Utils.parse(date, new ParsePosition(0)); } catch (ParseException e) { e.printStackTrace(); return null; } finally { cursor.close(); } }
From source file:org.ms123.common.data.query.JPASelectBuilderPostgresql.java
protected Object getDate(Object data, Map<String, Object> rule) { try {/*from w ww . ja v a2 s . c o m*/ Date d = new Date(); try { if (data instanceof Long) { d = new Date((Long) data); } else { d = new Date(Long.valueOf((String) rule.get("data"))); } } catch (Exception e) { try { if (data instanceof String) { d = new SimpleDateFormat( (((String) data).indexOf("T") > 0) ? "yyyy-MM-dd'T'HH:mm" : "yyyy-MM-dd") .parse((String) data, new ParsePosition(0)); if (d == null) d = new Date(); } } catch (Exception e1) { System.out.println("E:" + e); } } String op = (String) rule.get("op"); if (op.equals("eq") || op.equals("ceq") || op.equals("neq")) { String day = d.getDate() + ""; String month = d.getMonth() + ""; String year = (d.getYear() + 1900) + ""; data = day + "/" + month + "/" + year; } else if (op.equals("in") || op.equals("inn")) { } else { int paramCount = m_queryBuilder.getParamCount(); data = ":param" + paramCount; System.out.println("param" + paramCount + ":" + d); m_queryBuilder.getQueryParams().put("param" + paramCount, d); m_queryBuilder.incParamCount(); } } catch (Exception e) { e.printStackTrace(); data = "''"; } return data; }
From source file:org.webguitoolkit.ui.controls.util.conversion.NumberAllConverter.java
/** * check if the input string is a legal number. It does NOT return a Numberobject. * Itseem that this is a bug in BeanUtilBean class (maybe we shopuld update) * Try: BeanUtilBean.setProperty( Pump, "hours", new Float(24)) * with Pump.setHours(int value) taking an int as input. * You will see, that it will store '0', because it converts the float into string, * then back to int, and get an parseexception. * /*from w w w . j av a 2 s .co m*/ * @return the input string * @exception ConversionException if the input if not a legal number */ public Object parse(String textRep) throws ConversionException { // no mandatory field, empty means value zero. if (StringUtils.isEmpty(textRep)) return new Float(0f); formatter = new DecimalFormat(pattern, new DecimalFormatSymbols(TextService.getLocale())); ParsePosition pos = new ParsePosition(0); lastNumber = formatter.parse(textRep, pos); // to avoid null pointer -return float with 0 if (lastNumber == null) throw new ConversionException( TextService.getString("converter.NumberConverter.message@Cannot convert into number.")); if (pos.getIndex() < textRep.length()) { throw new ConversionException( TextService.getString("converter.NumberConverter.message@Cannot convert into number.")); } // must decode into string using default locale of system // because it is going to be translated back! // why is this fucking BeanUtil converter stuff so complicated and error-prone? return lastNumber.toString(); }
From source file:org.eredlab.g4.ccl.net.ftp.parser.FTPTimestampParserImpl.java
/** * Implements the one {@link FTPTimestampParser#parseTimestamp(String) method} * in the {@link FTPTimestampParser FTPTimestampParser} interface * according to this algorithm://from w ww .j a v a 2 s. com * * If the recentDateFormat member has been defined, try to parse the * supplied string with that. If that parse fails, or if the recentDateFormat * member has not been defined, attempt to parse with the defaultDateFormat * member. If that fails, throw a ParseException. * * @see org.apache.commons.net.ftp.parser.FTPTimestampParser#parseTimestamp(java.lang.String) */ /* (non-Javadoc) * */ public Calendar parseTimestamp(String timestampStr) throws ParseException { Calendar now = Calendar.getInstance(); now.setTimeZone(this.getServerTimeZone()); Calendar working = Calendar.getInstance(); working.setTimeZone(this.getServerTimeZone()); ParsePosition pp = new ParsePosition(0); Date parsed = null; if (this.recentDateFormat != null) { parsed = recentDateFormat.parse(timestampStr, pp); } if (parsed != null && pp.getIndex() == timestampStr.length()) { working.setTime(parsed); working.set(Calendar.YEAR, now.get(Calendar.YEAR)); if (working.after(now)) { working.add(Calendar.YEAR, -1); } } else { pp = new ParsePosition(0); parsed = defaultDateFormat.parse(timestampStr, pp); // note, length checks are mandatory for us since // SimpleDateFormat methods will succeed if less than // full string is matched. They will also accept, // despite "leniency" setting, a two-digit number as // a valid year (e.g. 22:04 will parse as 22 A.D.) // so could mistakenly confuse an hour with a year, // if we don't insist on full length parsing. if (parsed != null && pp.getIndex() == timestampStr.length()) { working.setTime(parsed); } else { throw new ParseException("Timestamp could not be parsed with older or recent DateFormat", pp.getIndex()); } } return working; }
From source file:com.anrisoftware.globalpom.version.VersionFormat.java
/** * @see #parse(String, ParsePosition)/*from w w w . j a va2 s . co m*/ */ public Version parse(String source) throws ParseException { ParsePosition pos = new ParsePosition(0); Version result = parse(source, pos); if (pos.getIndex() == 0) { throw log.errorParse(source, pos); } return result; }
From source file:org.noorganization.instalist.server.api.TagResource.java
/** * Get a list of tags./*from w w w . j av a2s .co m*/ * @param _groupId The id of the group containing the tags. * @param _changedSince Limits the request to elements that changed since the given date. ISO * 8601 time e.g. 2016-01-19T11:54:07+0100. Optional. */ @GET @TokenSecured @Produces({ "application/json" }) public Response getTags(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { Instant changedSince = null; try { if (_changedSince != null) changedSince = ISO8601Utils.parse(_changedSince, new ParsePosition(0)).toInstant(); } catch (ParseException _e) { return ResponseFactory.generateBadRequest(CommonEntity.INVALID_CHANGEDATE); } EntityManager manager = DatabaseHelper.getInstance().getManager(); List<Tag> tags; List<DeletedObject> deletedTags; DeviceGroup group = manager.find(DeviceGroup.class, _groupId); if (changedSince != null) { TypedQuery<Tag> tagQuery = manager.createQuery( "select t from Tag t where " + "t.group = :group and t.updated > :updated", Tag.class); tagQuery.setParameter("group", group); tagQuery.setParameter("updated", changedSince); tags = tagQuery.getResultList(); TypedQuery<DeletedObject> deletedRecipesQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.updated > :updated and " + "do.type = :type", DeletedObject.class); deletedRecipesQuery.setParameter("group", group); deletedRecipesQuery.setParameter("updated", changedSince); deletedRecipesQuery.setParameter("type", DeletedObject.Type.TAG); deletedTags = deletedRecipesQuery.getResultList(); } else { tags = new ArrayList<Tag>(group.getTags()); TypedQuery<DeletedObject> deletedRecipesQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); deletedRecipesQuery.setParameter("group", group); deletedRecipesQuery.setParameter("type", DeletedObject.Type.TAG); deletedTags = deletedRecipesQuery.getResultList(); } manager.close(); ArrayList<TagInfo> rtn = new ArrayList<TagInfo>(tags.size() + deletedTags.size()); for (Tag current : tags) { TagInfo toAdd = new TagInfo().withDeleted(false); toAdd.setUUID(current.getUUID()); toAdd.setName(current.getName()); toAdd.setLastChanged(Date.from(current.getUpdated())); rtn.add(toAdd); } for (DeletedObject current : deletedTags) { TagInfo toAdd = new TagInfo().withDeleted(true); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }
From source file:org.noorganization.instalist.server.api.RecipeResource.java
/** * Get a list of recipes.//from ww w . jav a 2s .c o m * @param _groupId The id of the group containing the recipes. * @param _changedSince Limits the request to elements that changed since the given date. ISO * 8601 time e.g. 2016-01-19T11:54:07+0100. Optional. */ @GET @TokenSecured @Produces({ "application/json" }) public Response getRecipes(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { Instant changedSince = null; try { if (_changedSince != null) changedSince = ISO8601Utils.parse(_changedSince, new ParsePosition(0)).toInstant(); } catch (ParseException _e) { return ResponseFactory.generateBadRequest(CommonEntity.INVALID_CHANGEDATE); } EntityManager manager = DatabaseHelper.getInstance().getManager(); List<Recipe> recipes; List<DeletedObject> deletedRecipes; DeviceGroup group = manager.find(DeviceGroup.class, _groupId); if (changedSince != null) { TypedQuery<Recipe> recipeQuery = manager.createQuery( "select r from Recipe r where " + "r.group = :group and r.updated > :updated", Recipe.class); recipeQuery.setParameter("group", group); recipeQuery.setParameter("updated", changedSince); recipes = recipeQuery.getResultList(); TypedQuery<DeletedObject> deletedRecipesQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.updated > :updated and " + "do.type = :type", DeletedObject.class); deletedRecipesQuery.setParameter("group", group); deletedRecipesQuery.setParameter("updated", changedSince); deletedRecipesQuery.setParameter("type", DeletedObject.Type.RECIPE); deletedRecipes = deletedRecipesQuery.getResultList(); } else { recipes = new ArrayList<Recipe>(group.getRecipes()); TypedQuery<DeletedObject> deletedRecipesQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); deletedRecipesQuery.setParameter("group", group); deletedRecipesQuery.setParameter("type", DeletedObject.Type.RECIPE); deletedRecipes = deletedRecipesQuery.getResultList(); } manager.close(); ArrayList<RecipeInfo> rtn = new ArrayList<RecipeInfo>(recipes.size() + deletedRecipes.size()); for (Recipe current : recipes) { RecipeInfo toAdd = new RecipeInfo().withDeleted(false); toAdd.setUUID(current.getUUID()); toAdd.setName(current.getName()); toAdd.setLastChanged(Date.from(current.getUpdated())); rtn.add(toAdd); } for (DeletedObject current : deletedRecipes) { RecipeInfo toAdd = new RecipeInfo().withDeleted(true); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }
From source file:org.noorganization.instalist.server.api.UnitResource.java
/** * Get a list of units./*from w ww . j av a 2s. co m*/ * @param _groupId The id of the group containing the requested units. * @param _changedSince Requests only the elements that changed since the given date. ISO * 8601 time e.g. 2016-01-19T11:54:07+01:00 */ @GET @TokenSecured @Produces({ "application/json" }) public Response getUnits(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { Instant changedSince = null; try { if (_changedSince != null) changedSince = ISO8601Utils.parse(_changedSince, new ParsePosition(0)).toInstant(); } catch (ParseException _e) { return ResponseFactory.generateBadRequest(CommonEntity.INVALID_CHANGEDATE); } EntityManager manager = DatabaseHelper.getInstance().getManager(); DeviceGroup group = manager.find(DeviceGroup.class, _groupId); List<Unit> resultUnits; List<DeletedObject> resultDeletedUnits; if (changedSince == null) { resultUnits = new ArrayList<Unit>(group.getUnits()); TypedQuery<DeletedObject> deletedUnitsQuery = manager.createQuery( "select do from " + "DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); deletedUnitsQuery.setParameter("group", group); deletedUnitsQuery.setParameter("type", DeletedObject.Type.UNIT); resultDeletedUnits = deletedUnitsQuery.getResultList(); } else { TypedQuery<Unit> unitsQuery = manager.createQuery( "select u from Unit u where " + "u.group = :group and u.updated > :updated", Unit.class); unitsQuery.setParameter("group", group); unitsQuery.setParameter("updated", changedSince); resultUnits = unitsQuery.getResultList(); TypedQuery<DeletedObject> deletedUnitsQuery = manager.createQuery("select do from " + "DeletedObject do where do.group = :group and do.type = :type and " + "do.updated > :updated", DeletedObject.class); deletedUnitsQuery.setParameter("group", group); deletedUnitsQuery.setParameter("type", DeletedObject.Type.UNIT); deletedUnitsQuery.setParameter("updated", changedSince); resultDeletedUnits = deletedUnitsQuery.getResultList(); } manager.close(); List<UnitInfo> rtn = new ArrayList<UnitInfo>(resultUnits.size() + resultDeletedUnits.size()); for (Unit current : resultUnits) { UnitInfo info = new UnitInfo().withDeleted(false); info.setName(current.getName()); info.setUUID(current.getUUID()); info.setLastChanged(Date.from(current.getUpdated())); rtn.add(info); } for (DeletedObject current : resultDeletedUnits) { UnitInfo info = new UnitInfo().withDeleted(true); info.setUUID(current.getUUID()); info.setLastChanged(Date.from(current.getUpdated())); rtn.add(info); } return ResponseFactory.generateOK(rtn); }
From source file:org.noorganization.instalist.server.api.CategoriesResource.java
/** * Get a list of categories./*from ww w . j a v a 2s. c om*/ * * @param _groupId The id of the group. * @param _changedSince Optional. Requests only the elements that changed since the given date. * ISO 8601 time e.g. 2016-01-19T11:54:07+01:00 */ @GET @TokenSecured @Produces({ "application/json" }) public Response getCategories(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { try { Instant changedSince = null; if (_changedSince != null) { try { changedSince = ISO8601Utils.parse(_changedSince, new ParsePosition(0)).toInstant(); } catch (ParseException _e) { return ResponseFactory.generateBadRequest(CommonEntity.INVALID_DATA); } } List<Category> categories; List<DeletedObject> deletedCategories; EntityManager manager = DatabaseHelper.getInstance().getManager(); DeviceGroup group = manager.find(DeviceGroup.class, _groupId); if (changedSince != null) { TypedQuery<Category> categoriesQuery = manager.createQuery( "select c from Category c " + "where c.group = :groupid and c.updated > :updated", Category.class); categoriesQuery.setParameter("groupid", group); categoriesQuery.setParameter("updated", changedSince); categories = categoriesQuery.getResultList(); TypedQuery<DeletedObject> deletedCategoriesQuery = manager .createQuery("select do " + "from DeletedObject do where do.group = :groupid and " + "do.type = :type and do.updated > :updated", DeletedObject.class); deletedCategoriesQuery.setParameter("groupid", group); deletedCategoriesQuery.setParameter("updated", changedSince); deletedCategoriesQuery.setParameter("type", DeletedObject.Type.CATEGORY); deletedCategories = deletedCategoriesQuery.getResultList(); } else { TypedQuery<Category> categoriesQuery = manager .createQuery("select c from Category c " + "where c.group = :groupid", Category.class); categoriesQuery.setParameter("groupid", group); categories = categoriesQuery.getResultList(); TypedQuery<DeletedObject> deletedCategoriesQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :groupid and " + "do.type = :type", DeletedObject.class); deletedCategoriesQuery.setParameter("groupid", group); deletedCategoriesQuery.setParameter("type", DeletedObject.Type.CATEGORY); deletedCategories = deletedCategoriesQuery.getResultList(); } manager.close(); List<CategoryInfo> rtnPayload = new ArrayList<CategoryInfo>( categories.size() + deletedCategories.size()); for (Category currentCat : categories) { CategoryInfo info = new CategoryInfo(); info.setUUID(currentCat.getUUID()); info.setName(currentCat.getName()); info.setLastChanged(Date.from(currentCat.getUpdated())); info.setDeleted(false); rtnPayload.add(info); } for (DeletedObject currentCat : deletedCategories) { CategoryInfo info = new CategoryInfo(); info.setUUID(currentCat.getUUID()); info.setLastChanged(Date.from(currentCat.getUpdated())); info.setDeleted(true); rtnPayload.add(info); } return ResponseFactory.generateOK(rtnPayload); } catch (Exception _e) { _e.printStackTrace(); throw _e; } }
From source file:org.noorganization.instalist.server.api.EntryResource.java
/** * Get a list of listEntries.//ww w . j av a 2s.c o m * @param _groupId The id of the group containing various list-entries. * @param _changedSince Limits the result to elements that changed since the given date. ISO * 8601 time e.g. 2016-01-19T11:54:07+0100 */ @GET @TokenSecured @Produces({ "application/json" }) public Response getEntries(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { List<ListEntry> foundEntries; List<DeletedObject> foundDeleted; EntityManager manager = DatabaseHelper.getInstance().getManager(); DeviceGroup group = manager.find(DeviceGroup.class, _groupId); if (_changedSince != null) { Instant changedSince; try { changedSince = ISO8601Utils.parse(_changedSince, new ParsePosition(0)).toInstant(); } catch (ParseException _e) { manager.close(); return ResponseFactory.generateBadRequest(CommonEntity.INVALID_CHANGEDATE); } TypedQuery<ListEntry> foundEntriesQuery = manager.createQuery( "select le from " + "ListEntry le where le.group = :group and le.updated > :updated", ListEntry.class); foundEntriesQuery.setParameter("group", group); foundEntriesQuery.setParameter("updated", changedSince); foundEntries = foundEntriesQuery.getResultList(); TypedQuery<DeletedObject> foundDeletedEntriesQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.updated > :updated and " + "do.type = :type", DeletedObject.class); foundDeletedEntriesQuery.setParameter("group", group); foundDeletedEntriesQuery.setParameter("updated", changedSince); foundDeletedEntriesQuery.setParameter("type", DeletedObject.Type.LISTENTRY); foundDeleted = foundDeletedEntriesQuery.getResultList(); } else { foundEntries = new ArrayList<ListEntry>(group.getListEntries()); TypedQuery<DeletedObject> deletedEntriesQuery = manager.createQuery( "select do from " + "DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); deletedEntriesQuery.setParameter("group", group); deletedEntriesQuery.setParameter("type", DeletedObject.Type.LISTENTRY); foundDeleted = deletedEntriesQuery.getResultList(); } manager.close(); ArrayList<EntryInfo> rtn = new ArrayList<EntryInfo>(foundEntries.size() + foundDeleted.size()); for (ListEntry current : foundEntries) { EntryInfo toAdd = new EntryInfo().withDeleted(false); toAdd.setUUID(current.getUUID()); toAdd.setProductUUID(current.getProduct().getUUID()); toAdd.setListUUID(current.getList().getUUID()); toAdd.setAmount(current.getAmount()); toAdd.setPriority(current.getPriority()); toAdd.setStruck(current.getStruck()); toAdd.setLastChanged(Date.from(current.getUpdated())); rtn.add(toAdd); } for (DeletedObject current : foundDeleted) { EntryInfo toAdd = new EntryInfo(); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(true); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }