List of usage examples for java.text ParsePosition ParsePosition
public ParsePosition(int index)
From source file:org.catechis.DomartinTest.java
private long getMilliseconds(String str_date) { SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); Date date = null;//from w w w . j av a 2s . co m ParsePosition pp = new ParsePosition(0); date = sdf.parse(str_date, pp); long time = date.getTime(); return time; }
From source file:com.sunchenbin.store.feilong.core.text.DateFormatUtil.java
/** * ?./* w w w .ja v a 2 s .c o m*/ * * @param dateString * the date string * @param pattern * the pattern * @param locale * the locale * @return the date * @see SimpleDateFormat * @see SimpleDateFormat#parse(String) * @see SimpleDateFormat#parse(String, ParsePosition) */ public static Date parse(String dateString, String pattern, Locale locale) { if (Validator.isNullOrEmpty(dateString)) { throw new NullPointerException("param dateString can not NullOrEmpty"); } SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, locale); ParsePosition parsePosition = new ParsePosition(0); return simpleDateFormat.parse(dateString, parsePosition); }
From source file:org.sonar.api.utils.DateUtils.java
/** * @param s string in format {@link #DATETIME_FORMAT} * @throws SonarException when string cannot be parsed *//* ww w.j av a 2 s . c o m*/ public static Date parseDateTime(String s) { ParsePosition pos = new ParsePosition(0); Date result = THREAD_SAFE_DATETIME_FORMAT.parse(s, pos); if (pos.getIndex() != s.length()) { throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'"); } return result; }
From source file:org.noorganization.instalist.server.api.ProductResource.java
/** * Get a list of products.// www . j a v a2 s . co m * @param _groupId The id of the group containing the products. * @param _changedSince Optional. Limits the request to the 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 getProducts(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { List<Product> foundProducts; 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<Product> foundProductsQuery = manager.createQuery( "select p from " + "Product p where p.group = :group and p.updated > :updated", Product.class); foundProductsQuery.setParameter("group", group); foundProductsQuery.setParameter("updated", changedSince); foundProducts = foundProductsQuery.getResultList(); TypedQuery<DeletedObject> foundDeletedListsQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.updated > :updated and" + " do.type = :type", DeletedObject.class); foundDeletedListsQuery.setParameter("group", group); foundDeletedListsQuery.setParameter("updated", changedSince); foundDeletedListsQuery.setParameter("type", DeletedObject.Type.PRODUCT); foundDeleted = foundDeletedListsQuery.getResultList(); } else { TypedQuery<Product> foundProductsQuery = manager .createQuery("select p from " + "Product p where p.group = :group", Product.class); foundProductsQuery.setParameter("group", group); foundProducts = foundProductsQuery.getResultList(); TypedQuery<DeletedObject> foundDeletedProductsQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); foundDeletedProductsQuery.setParameter("group", group); foundDeletedProductsQuery.setParameter("type", DeletedObject.Type.PRODUCT); foundDeleted = foundDeletedProductsQuery.getResultList(); } manager.close(); ArrayList<ProductInfo> rtn = new ArrayList<ProductInfo>(foundProducts.size() + foundDeleted.size()); for (Product current : foundProducts) { ProductInfo toAdd = new ProductInfo(); toAdd.setUUID(current.getUUID()); toAdd.setName(current.getName()); toAdd.setDefaultAmount(current.getDefaultAmount()); toAdd.setStepAmount(current.getStepAmount()); if (current.getUnit() != null) toAdd.setUnitUUID(current.getUnit().getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(false); rtn.add(toAdd); } for (DeletedObject current : foundDeleted) { ProductInfo toAdd = new ProductInfo(); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(true); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }
From source file:org.noorganization.instalist.server.api.ListResource.java
/** * Get a list of shopping-lists./*from w w w . j av a 2 s. c o m*/ * @param _groupId The id of the group, containing the lists. * @param _changedSince Requests only the 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 getLists(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { List<ShoppingList> foundLists; 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<ShoppingList> foundListsQuery = manager.createQuery( "select sl from " + "ShoppingList sl where sl.group = :group and sl.updated > :updated", ShoppingList.class); foundListsQuery.setParameter("group", group); foundListsQuery.setParameter("updated", changedSince); foundLists = foundListsQuery.getResultList(); TypedQuery<DeletedObject> foundDeletedListsQuery = manager .createQuery("select do from" + " DeletedObject do where do.group = :group and " + "do.updated > :updated and do.type = :type", DeletedObject.class); foundDeletedListsQuery.setParameter("group", group); foundDeletedListsQuery.setParameter("updated", changedSince); foundDeletedListsQuery.setParameter("type", DeletedObject.Type.LIST); foundDeleted = foundDeletedListsQuery.getResultList(); } else { TypedQuery<ShoppingList> foundListsQuery = manager .createQuery("select sl from " + "ShoppingList sl where sl.group = :group", ShoppingList.class); foundListsQuery.setParameter("group", group); foundLists = foundListsQuery.getResultList(); TypedQuery<DeletedObject> foundDeletedListsQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); foundDeletedListsQuery.setParameter("group", group); foundDeletedListsQuery.setParameter("type", DeletedObject.Type.LIST); foundDeleted = foundDeletedListsQuery.getResultList(); } manager.close(); ArrayList<ListInfo> rtn = new ArrayList<ListInfo>(foundLists.size() + foundDeleted.size()); for (ShoppingList current : foundLists) { ListInfo toAdd = new ListInfo(); toAdd.setUUID(current.getUUID()); toAdd.setName(current.getName()); if (current.getCategory() != null) toAdd.setCategoryUUID(current.getCategory().getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(false); rtn.add(toAdd); } for (DeletedObject current : foundDeleted) { ListInfo toAdd = new ListInfo(); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(true); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }
From source file:org.noorganization.instalist.server.api.IngredientResource.java
/** * Get a list of ingredients./* w ww . j a v a 2 s . co m*/ * @param _groupId The id of the group containing various ingredients. * @param _changedSince Limits the result 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 getIngredients(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { List<Ingredient> ingedients; List<DeletedObject> deletedIngredients; 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<Ingredient> IngredientsQuery = manager.createQuery( "select i from " + "Ingredient i where i.group = :group and i.updated > :updated", Ingredient.class); IngredientsQuery.setParameter("group", group); IngredientsQuery.setParameter("updated", changedSince); ingedients = IngredientsQuery.getResultList(); TypedQuery<DeletedObject> deletedIngredientsQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.updated > :updated and " + "do.type = :type", DeletedObject.class); deletedIngredientsQuery.setParameter("group", group); deletedIngredientsQuery.setParameter("updated", changedSince); deletedIngredientsQuery.setParameter("type", DeletedObject.Type.INGREDIENT); deletedIngredients = deletedIngredientsQuery.getResultList(); } else { ingedients = new ArrayList<Ingredient>(group.getIngredients()); TypedQuery<DeletedObject> deletedIngredientsQuery = manager.createQuery( "select do from " + "DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); deletedIngredientsQuery.setParameter("group", group); deletedIngredientsQuery.setParameter("type", DeletedObject.Type.INGREDIENT); deletedIngredients = deletedIngredientsQuery.getResultList(); } manager.close(); ArrayList<IngredientInfo> rtn = new ArrayList<IngredientInfo>( ingedients.size() + deletedIngredients.size()); for (Ingredient current : ingedients) { IngredientInfo toAdd = new IngredientInfo().withDeleted(false); toAdd.setUUID(current.getUUID()); toAdd.setProductUUID(current.getProduct().getUUID()); toAdd.setRecipeUUID(current.getRecipe().getUUID()); toAdd.setAmount(current.getAmount()); toAdd.setLastChanged(Date.from(current.getUpdated())); rtn.add(toAdd); } for (DeletedObject current : deletedIngredients) { IngredientInfo toAdd = new IngredientInfo(); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(true); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }
From source file:org.noorganization.instalist.server.api.TaggedProductResource.java
/** * Get a list of tagged products./*from w w w .j a v a2s . c o m*/ * @param _groupId The id of the group containing various tagged products. * @param _changedSince Limits the result 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 getTaggedProducts(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { List<TaggedProduct> taggedProducts; List<DeletedObject> deletedTaggedProducts; 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<TaggedProduct> taggedProductQuery = manager.createQuery( "select tp from " + "TaggedProduct tp where tp.group = :group and tp.updated > :updated", TaggedProduct.class); taggedProductQuery.setParameter("group", group); taggedProductQuery.setParameter("updated", changedSince); taggedProducts = taggedProductQuery.getResultList(); TypedQuery<DeletedObject> deletedTaggedProductQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.updated > :updated and " + "do.type = :type", DeletedObject.class); deletedTaggedProductQuery.setParameter("group", group); deletedTaggedProductQuery.setParameter("updated", changedSince); deletedTaggedProductQuery.setParameter("type", DeletedObject.Type.TAGGEDPRODUCT); deletedTaggedProducts = deletedTaggedProductQuery.getResultList(); } else { taggedProducts = new ArrayList<TaggedProduct>(group.getTaggedProducts()); TypedQuery<DeletedObject> deletedIngredientsQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); deletedIngredientsQuery.setParameter("group", group); deletedIngredientsQuery.setParameter("type", DeletedObject.Type.TAGGEDPRODUCT); deletedTaggedProducts = deletedIngredientsQuery.getResultList(); } manager.close(); ArrayList<TaggedProductInfo> rtn = new ArrayList<TaggedProductInfo>( taggedProducts.size() + deletedTaggedProducts.size()); for (TaggedProduct current : taggedProducts) { TaggedProductInfo toAdd = new TaggedProductInfo().withDeleted(false); toAdd.setUUID(current.getUUID()); toAdd.setProductUUID(current.getProduct().getUUID()); toAdd.setTagUUID(current.getTag().getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); rtn.add(toAdd); } for (DeletedObject current : deletedTaggedProducts) { TaggedProductInfo toAdd = new TaggedProductInfo(); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(true); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }
From source file:jp.go.nict.langrid.management.web.utility.DateUtil.java
/** * //from ww w.j a v a 2s .c o m * */ public static Date getDate(String text) throws ParseException { ParsePosition pp = new ParsePosition(0); if (text == null) { throw new ParseException("", pp.getIndex()); } int textLength = text.length(); for (SimpleDateFormat format : formats) { format.setLenient(false); pp = new ParsePosition(0); Date ret = format.parse(text, pp); if (textLength == pp.getIndex()) { return ret; } } throw new ParseException("", pp.getIndex()); }
From source file:gov.vha.isaac.ucum.UCUMParser.java
/** * Execute the Eclipse units of measure parser * @param value//from w w w . ja v a2 s . co m * @param textToParseForUnit * @return */ public static ParsedUCUM uomoParser(String value, String textToParseForUnit) { try { // Not sure if this is threadsafe, so no reuse LocalUnitFormatImpl uomoParser = new LocalUnitFormatImpl(); org.unitsofmeasurement.unit.Unit<?> unit = uomoParser.parse(textToParseForUnit, new ParsePosition(0)); if (unit != null) { return new ParsedUCUM(textToParseForUnit, value, unit); } } catch (Exception e) { // noop } return null; }
From source file:jp.co.opentone.bsol.framework.core.util.DateUtil.java
/** * ???(?????????null)./*from www. jav a2s.c o m*/ * @param format ? * @param date * @return ?? */ public static Date convertStringToDate(String format, String date) { if (StringUtils.isEmpty(date)) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setLenient(false); ParsePosition pos = new ParsePosition(0); Date d = sdf.parse(date, pos); // ???????????? // ??????????? if (pos.getIndex() != date.length()) { return null; } return d; }