ISODate implements Cloneable, Comparable<ISODate>, Serializable, XmlEmbeddable { public void setDateAndTime(String isoDate) { String timeAndDate = isoDate; if (timeAndDate == null) { throw new IllegalArgumentException("date string is null"); } int indexOfT = timeAndDate.indexOf('T'); if (indexOfT > -1) { String afterT = timeAndDate.substring(indexOfT + 1); boolean timeZoneInfo = afterT.contains("+") || afterT.contains("-") || afterT.toUpperCase().endsWith("Z"); if (timeZoneInfo) { timeAndDate = parseISODateTime(timeAndDate); } } String[] parts = timeAndDate.toUpperCase().split("T", 2); if (parts.length == 1) { if (parts[0].contains(":")) { parseTime(parts[0]); } else { parseDate(parts[0]); } } else { if (!parts[0].trim().isEmpty()) { parseDate(parts[0]); } if (!parts[1].trim().isEmpty()) { parseTime(parts[1]); } } } ISODate();  ISODate(final long time, final boolean shortDate);  ISODate(final long time);  ISODate(@Nonnull final String isoDate); static String parseISODateTime(final String stringToParse); static String parseISODateTimes(String input1, String input2); static DateTime parseBasicOrFullDateTime(String input1); ISODate clone(); long timeDifferenceInSeconds(ISODate date); @Transient String getDateAsString(); String toString(); Date toDate(); @Transient long getTimeInSeconds(); @XmlValue String getDateAndTime(); void setDateAndTime(String isoDate); @Transient boolean isDateOnly(); @Transient boolean isDateYearOnly(); @Transient boolean isDateYearMonthOnly(); @Transient int getYears(); @Transient int getDays(); @Transient int getMonths(); @Transient int getHours(); @Transient int getMinutes(); @Transient int getSeconds(); @Override int hashCode(); @Override boolean equals(Object obj); @Transient String getTimeAsString(); @Override int compareTo(ISODate o); @Override void addToXml(Element element);  }
@Test public void testSetDateAndTime() { ISODate date = new ISODate(); date.setDateAndTime("1976-06-03T01:02:03"); assertEquals(false, date.isDateOnly()); assertEquals(1976, date.getYears()); assertEquals(6, date.getMonths()); assertEquals(3, date.getDays()); assertEquals(1, date.getHours()); assertEquals(2, date.getMinutes()); assertEquals(3, date.getSeconds()); date = new ISODate(); date.setDateAndTime("T01:02:03"); assertEquals(false, date.isDateOnly()); assertEquals(Calendar.getInstance().get(Calendar.YEAR), date.getYears()); assertEquals(Calendar.getInstance().get(Calendar.MONTH) + 1, date.getMonths()); assertEquals(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), date.getDays()); assertEquals(1, date.getHours()); assertEquals(2, date.getMinutes()); assertEquals(3, date.getSeconds()); date = new ISODate(); date.setDateAndTime("01:02:03"); assertEquals(false, date.isDateOnly()); assertEquals(Calendar.getInstance().get(Calendar.YEAR), date.getYears()); assertEquals(Calendar.getInstance().get(Calendar.MONTH) + 1, date.getMonths()); assertEquals(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), date.getDays()); assertEquals(1, date.getHours()); assertEquals(2, date.getMinutes()); assertEquals(3, date.getSeconds()); date = new ISODate(); date.setDateAndTime("1976-6-3T1:2:3"); assertEquals(false, date.isDateOnly()); assertEquals(1976, date.getYears()); assertEquals(6, date.getMonths()); assertEquals(3, date.getDays()); assertEquals(1, date.getHours()); assertEquals(2, date.getMinutes()); assertEquals(3, date.getSeconds()); date = new ISODate(); date.setDateAndTime("1976-6-3T1:2:3Z"); assertEquals(false, date.isDateOnly()); assertEquals(1976, date.getYears()); assertEquals(6, date.getMonths()); assertEquals(3, date.getDays()); assertEquals(1, date.getHours()); assertEquals(2, date.getMinutes()); assertEquals(3, date.getSeconds()); date = new ISODate(); date.setDateAndTime("1976-6-3T10:02"); assertEquals(false, date.isDateOnly()); assertEquals(1976, date.getYears()); assertEquals(6, date.getMonths()); assertEquals(3, date.getDays()); assertEquals(10, date.getHours()); assertEquals(2, date.getMinutes()); assertEquals(0, date.getSeconds()); }
@Test(expected = IllegalArgumentException.class) public void testSetDateExceptionBecauseOfNull() throws Exception { ISODate isoDate = new ISODate(); isoDate.setDateAndTime(null); }
@Test(expected = IllegalArgumentException.class) public void testSetDateExceptionBecauseOfBadFormat() throws Exception { ISODate isoDate = new ISODate(); isoDate.setDateAndTime("2019hh"); }
@Test(expected = IllegalArgumentException.class) public void testSetDateExceptionBecauseOfEmpty() throws Exception { ISODate isoDate = new ISODate(); isoDate.setDateAndTime(""); }