XsDateTimeFormat format = new XsDateTimeFormat(); ParsePosition pos = new ParsePosition(0); Calendar cal = (Calendar) format.parseObject(arg0, pos, lenient); if (cal == null) { throw new IllegalArgumentException(
year = -year; append(pBuffer, year, 4); pBuffer.append('-'); append(pBuffer, cal.get(Calendar.MONTH) + 1, 2); pBuffer.append('-'); append(pBuffer, cal.get(Calendar.DAY_OF_MONTH), 2); if (parseTime) { pBuffer.append('T'); append(pBuffer, cal.get(Calendar.HOUR_OF_DAY), 2); pBuffer.append(':'); append(pBuffer, cal.get(Calendar.MINUTE), 2); pBuffer.append(':'); append(pBuffer, cal.get(Calendar.SECOND), 2); int millis = cal.get(Calendar.MILLISECOND); if (millis > 0) { pBuffer.append('.'); append(pBuffer, millis, 3); int hours = minutes / 60; minutes -= hours * 60; append(pBuffer, hours, 2); pBuffer.append(':'); append(pBuffer, minutes, 2);
public String printDateTime(Calendar arg0) { return new XsDateTimeFormat().format(arg0); }
public Object parseObject(String pString, ParsePosition pParsePosition) { return parseObject(pString, pParsePosition, false); }
offset = parseInt(pString, offset, digits); if (digits.length() < 4) { pParsePosition.setErrorIndex(offset); offset = parseInt(pString, offset, digits); if (digits.length() != 2) { pParsePosition.setErrorIndex(offset); offset = parseInt(pString, offset, digits); if (digits.length() != 2) { pParsePosition.setErrorIndex(offset); if (parseTime) { int offsetBefore = offset; offset = parseInt(pString, offset, digits); if (digits.length() != 2) { offset = parseInt(pString, offsetBefore, digits); length = pString.length(); } else { offset = parseInt(pString, offset, digits); if (digits.length() != 2) { offset = parseInt(pString, offsetBefore, digits); length = pString.length(); } else { offset = parseInt(pString, offset, digits);
public Object parseObject(String source, boolean lenient) throws ParseException { ParsePosition pos = new ParsePosition(0); Object result = parseObject(source, pos, lenient); if (pos.getIndex() == 0) { throw new ParseException("Format.parseObject(String) failed", pos.getErrorIndex()); } return result; }
@Test(expected = ParseException.class) public void testWrongDate() throws ParseException { // Create the parser XsDateTimeFormat format = new XsDateTimeFormat(); // Set a date String time = "2014-10-12T09:00Z"; // Parse the Date Object parseObject = format.parseObject(time); }
@Test public void testExtendedFractionalSeconds() throws ParseException { XsDateTimeFormat format = new XsDateTimeFormat(); // parse a timestamp with extended decimals String time = "2014-10-13T05:34:02.1109963Z"; Object parseObject = format.parseObject(time, true); assertTrue(parseObject instanceof Calendar); // Get the date Calendar cal = (Calendar) parseObject; // Ensure the parsing is correct int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); int hours = cal.get(Calendar.HOUR); int mins = cal.get(Calendar.MINUTE); int sec = cal.get(Calendar.SECOND); int millis = cal.get(Calendar.MILLISECOND); // check assertEquals(year, 2014); assertEquals(month, 9); assertEquals(day, 13); assertEquals(hours, 5); assertEquals(mins, 34); assertEquals(sec, 02); assertEquals(millis, 111); }
@Test public void testWrongDateLenientNoMins() throws ParseException { // Create the parser XsDateTimeFormat format = new XsDateTimeFormat(); // Set a date String time = "2014-10-12T09Z"; // Parse the Date Object parseObject = format.parseObject(time, true); assertTrue(parseObject instanceof Calendar); // Get the date Calendar cal = (Calendar) parseObject; // Ensure the parsing is correct int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); int hours = cal.get(Calendar.HOUR); int mins = cal.get(Calendar.MINUTE); int sec = cal.get(Calendar.SECOND); int millis = cal.get(Calendar.MILLISECOND); // check assertEquals(year, 2014); assertEquals(month, 9); assertEquals(day, 12); assertEquals(hours, 9); assertEquals(mins, 0); assertEquals(sec, 0); assertEquals(millis, 0); }
@Test public void testWrongDateLenientIncomplete() throws ParseException { // Create the parser XsDateTimeFormat format = new XsDateTimeFormat(); // Set a date String time = "2014-10-12T0Z"; // Parse the Date Object parseObject = format.parseObject(time, true); assertTrue(parseObject instanceof Calendar); // Get the date Calendar cal = (Calendar) parseObject; // Ensure the parsing is correct int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); int hours = cal.get(Calendar.HOUR); int mins = cal.get(Calendar.MINUTE); int sec = cal.get(Calendar.SECOND); int millis = cal.get(Calendar.MILLISECOND); // check assertEquals(year, 2014); assertEquals(month, 9); assertEquals(day, 12); assertEquals(hours, 0); assertEquals(mins, 0); assertEquals(sec, 0); assertEquals(millis, 0); }
@Test public void testDate() throws ParseException { // Create the parser XsDateTimeFormat format = new XsDateTimeFormat(); // Set a date String time = "2014-10-12T09:00:00.000Z"; // Parse the Date Object parseObject = format.parseObject(time); assertTrue(parseObject instanceof Calendar); // Get the date Calendar cal = (Calendar) parseObject; // Ensure the parsing is correct int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); int hours = cal.get(Calendar.HOUR); int mins = cal.get(Calendar.MINUTE); int sec = cal.get(Calendar.SECOND); int millis = cal.get(Calendar.MILLISECOND); // check assertEquals(year, 2014); assertEquals(month, 9); assertEquals(day, 12); assertEquals(hours, 9); assertEquals(mins, 0); assertEquals(sec, 0); assertEquals(millis, 0); }
@Test public void testWrongDateLenientNoSec() throws ParseException { // Create the parser XsDateTimeFormat format = new XsDateTimeFormat(); // Set a date String time = "2014-10-12T09:00Z"; // Parse the Date Object parseObject = format.parseObject(time, true); assertTrue(parseObject instanceof Calendar); // Get the date Calendar cal = (Calendar) parseObject; // Ensure the parsing is correct int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); int hours = cal.get(Calendar.HOUR); int mins = cal.get(Calendar.MINUTE); int sec = cal.get(Calendar.SECOND); int millis = cal.get(Calendar.MILLISECOND); // check assertEquals(year, 2014); assertEquals(month, 9); assertEquals(day, 12); assertEquals(hours, 9); assertEquals(mins, 0); assertEquals(sec, 0); assertEquals(millis, 0); }
@Test public void testWrongDateLenientIncompleteMins() throws ParseException { // Create the parser XsDateTimeFormat format = new XsDateTimeFormat(); // Set a date String time = "2014-10-12T05:0Z"; // Parse the Date Object parseObject = format.parseObject(time, true); assertTrue(parseObject instanceof Calendar); // Get the date Calendar cal = (Calendar) parseObject; // Ensure the parsing is correct int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); int hours = cal.get(Calendar.HOUR); int mins = cal.get(Calendar.MINUTE); int sec = cal.get(Calendar.SECOND); int millis = cal.get(Calendar.MILLISECOND); // check assertEquals(year, 2014); assertEquals(month, 9); assertEquals(day, 12); assertEquals(hours, 5); assertEquals(mins, 0); assertEquals(sec, 0); assertEquals(millis, 0); }
@Test public void testDefaultFormatDates() throws ParseException { XsDateTimeFormat format = new XsDateTimeFormat(); TimeZone originalTimeZone = TimeZone.getDefault(); TimeZone tz = TimeZone.getTimeZone("GMT"); Locale.setDefault(Locale.ENGLISH); String t = "Tue Apr 25 13:13:14 UTC 2017"; Object parseObject = format.parseObject(t, true); assertTrue(parseObject instanceof Calendar); parseObject = format.parseObject(t, true); assertTrue(parseObject instanceof Calendar); parseObject = format.parseObject(t, true); assertTrue(parseObject instanceof Calendar);