@Test public void testParse() { I18N.bindLocale(Locale.FRENCH); try { final DateFormat dateFormat = I18N.createDateFormat(); assertEquals("parse1", periodRange.getPeriod(), Range.parse(periodRange.getValue(), dateFormat).getPeriod()); assertTrue("parse2", isSameDay(customRange.getStartDate(), Range.parse(customRange.getValue(), dateFormat).getStartDate())); assertTrue("parse3", isSameDay(customRange.getEndDate(), Range.parse(customRange.getValue(), dateFormat).getEndDate())); assertNotNull("parse4", Range .parse("xxxxxx" + Range.CUSTOM_PERIOD_SEPARATOR + "01/01/2010", dateFormat)); assertNotNull("parse5", Range .parse("01/01/2010" + Range.CUSTOM_PERIOD_SEPARATOR + "xxxxxx", dateFormat)); assertNotNull("parse6", Range.parse("01/01/2010" + Range.CUSTOM_PERIOD_SEPARATOR, dateFormat)); assertNotNull("parse6b", Range.parse("01/01/2011", dateFormat)); final Calendar calendar = Calendar.getInstance(); final int currentYear = calendar.get(Calendar.YEAR); Range range = Range.parse("01/01/2000" + Range.CUSTOM_PERIOD_SEPARATOR + "01/01/2030", dateFormat); calendar.setTime(range.getStartDate()); assertTrue("parse7", calendar.get(Calendar.YEAR) >= currentYear - 2); calendar.setTime(range.getEndDate()); assertTrue("parse7", calendar.get(Calendar.YEAR) <= currentYear); range = Range.parse("01/01/2030" + Range.CUSTOM_PERIOD_SEPARATOR + "01/01/2030", dateFormat); calendar.setTime(range.getStartDate()); assertTrue("parse8", calendar.get(Calendar.YEAR) <= currentYear); } finally { I18N.unbindLocale(); } }
public static Range parse(String value, DateFormat dateFormat) { final int index = value.indexOf(CUSTOM_PERIOD_SEPARATOR); if (index == -1) { try { return Period.valueOfIgnoreCase(value).getRange(); } catch (final IllegalArgumentException e) { return Period.JOUR.getRange(); } } Date startDate; try { startDate = dateFormat.parse(value.substring(0, index)); } catch (final ParseException e) { startDate = new Date(); } Date endDate; if (index < value.length() - 1) { try { endDate = dateFormat.parse(value.substring(index + 1)); } catch (final ParseException e) { endDate = new Date(); } } else { endDate = new Date(); } return createCustomRange(startDate, endDate); }
Range implements Serializable { public static Range parse(String value, DateFormat dateFormat) { final int index = value.indexOf(CUSTOM_PERIOD_SEPARATOR); if (index == -1) { try { return Period.valueOfIgnoreCase(value).getRange(); } catch (final IllegalArgumentException e) { return Period.JOUR.getRange(); } } Date startDate; try { startDate = dateFormat.parse(value.substring(0, index)); } catch (final ParseException e) { startDate = new Date(); } Date endDate; if (index < value.length() - 1) { try { endDate = dateFormat.parse(value.substring(index + 1)); } catch (final ParseException e) { endDate = new Date(); } } else { endDate = new Date(); } return createCustomRange(startDate, endDate); } }
Range implements Serializable { public static Range parse(String value, DateFormat dateFormat) { final int index = value.indexOf(CUSTOM_PERIOD_SEPARATOR); if (index == -1) { try { return Period.valueOfIgnoreCase(value).getRange(); } catch (final IllegalArgumentException e) { return Period.JOUR.getRange(); } } Date startDate; try { startDate = dateFormat.parse(value.substring(0, index)); } catch (final ParseException e) { startDate = new Date(); } Date endDate; if (index < value.length() - 1) { try { endDate = dateFormat.parse(value.substring(index + 1)); } catch (final ParseException e) { endDate = new Date(); } } else { endDate = new Date(); } return createCustomRange(startDate, endDate); } private  Range(Period period, Date startDate, Date endDate); }
Range implements Serializable { public static Range parse(String value, DateFormat dateFormat) { final int index = value.indexOf(CUSTOM_PERIOD_SEPARATOR); if (index == -1) { try { return Period.valueOfIgnoreCase(value).getRange(); } catch (final IllegalArgumentException e) { return Period.JOUR.getRange(); } } Date startDate; try { startDate = dateFormat.parse(value.substring(0, index)); } catch (final ParseException e) { startDate = new Date(); } Date endDate; if (index < value.length() - 1) { try { endDate = dateFormat.parse(value.substring(index + 1)); } catch (final ParseException e) { endDate = new Date(); } } else { endDate = new Date(); } return createCustomRange(startDate, endDate); } private  Range(Period period, Date startDate, Date endDate); static Range createCustomRange(Date startDate, Date endDate); static Range parse(String value, DateFormat dateFormat); Period getPeriod(); Date getStartDate(); Date getEndDate(); String getValue(); String getLabel(); @Override String toString(); }
Range implements Serializable { public static Range parse(String value, DateFormat dateFormat) { final int index = value.indexOf(CUSTOM_PERIOD_SEPARATOR); if (index == -1) { try { return Period.valueOfIgnoreCase(value).getRange(); } catch (final IllegalArgumentException e) { return Period.JOUR.getRange(); } } Date startDate; try { startDate = dateFormat.parse(value.substring(0, index)); } catch (final ParseException e) { startDate = new Date(); } Date endDate; if (index < value.length() - 1) { try { endDate = dateFormat.parse(value.substring(index + 1)); } catch (final ParseException e) { endDate = new Date(); } } else { endDate = new Date(); } return createCustomRange(startDate, endDate); } private  Range(Period period, Date startDate, Date endDate); static Range createCustomRange(Date startDate, Date endDate); static Range parse(String value, DateFormat dateFormat); Period getPeriod(); Date getStartDate(); Date getEndDate(); String getValue(); String getLabel(); @Override String toString(); static final char CUSTOM_PERIOD_SEPARATOR; }