public CachingDateFormatter(FastDateFormat fastDateFormat) { this.fastDateFormat = fastDateFormat; onSecond = fastDateFormat.getPattern().indexOf("SSS") == -1; long current = System.currentTimeMillis(); this.cachedTime = new AtomicReference<CachedTime>(new CachedTime(current, fastDateFormat.format(current))); }
public CachingDateFormatter(FastDateFormat fastDateFormat) { this.fastDateFormat = fastDateFormat; onSecond = fastDateFormat.getPattern().indexOf("SSS") == -1; long current = System.currentTimeMillis(); this.cachedTime = new AtomicReference<CachedTime>(new CachedTime(current, fastDateFormat.format(current))); }
@Test public void testTimeISO() { testGmtMinus3("T09:11:12", DateFormatUtils.ISO_TIME_FORMAT.getPattern()); testGmtMinus3("T09:11:12-03:00", DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern()); testUTC("T09:11:12Z", DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern()); }
@Test public void testDateTimeISO() throws Exception { testGmtMinus3("2002-02-23T09:11:12", DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()); testGmtMinus3("2002-02-23T09:11:12-03:00", DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()); testUTC("2002-02-23T09:11:12Z", DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()); }
@Test public void testTimeNoTISO() { testGmtMinus3("09:11:12", DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern()); testGmtMinus3("09:11:12-03:00", DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern()); testUTC("09:11:12Z", DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern()); }
@Test public void testDateISO() { testGmtMinus3("2002-02-23", DateFormatUtils.ISO_DATE_FORMAT.getPattern()); testGmtMinus3("2002-02-23-03:00", DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern()); testUTC("2002-02-23Z", DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern()); }
@Test public void testFormatUTC() { final Calendar c = Calendar.getInstance(FastTimeZone.getGmtTimeZone()); c.set(2005, Calendar.JANUARY, 1, 12, 0, 0); assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern())); assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime().getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern())); assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), Locale.US)); assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime().getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), Locale.US)); }
@SuppressWarnings("deprecation") @Test public void testLang530() throws ParseException { final Date d = new Date(); final String isoDateStr = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(d); final Date d2 = DateUtils.parseDate(isoDateStr, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()); // the format loses milliseconds so have to reintroduce them assertEquals("Date not equal to itself ISO formatted and parsed", d.getTime(), d2.getTime() + d.getTime() % 1000); }
@SystemDefaults(timezone="UTC") @Test public void testLang530() throws ParseException { final Date d = new Date(); final String isoDateStr = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(d); final Date d2 = DateUtils.parseDate(isoDateStr, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()); // the format loses milliseconds so have to reintroduce them assertEquals("Date not equal to itself ISO formatted and parsed", d.getTime(), d2.getTime() + d.getTime() % 1000); }
@SuppressWarnings("deprecation") @Test public void testFormatPeriodISO() { final TimeZone timeZone = TimeZone.getTimeZone("GMT-3"); final Calendar base = Calendar.getInstance(timeZone); base.set(1970, Calendar.JANUARY, 1, 0, 0, 0); base.set(Calendar.MILLISECOND, 0); final Calendar cal = Calendar.getInstance(timeZone); cal.set(2002, Calendar.FEBRUARY, 23, 9, 11, 12); cal.set(Calendar.MILLISECOND, 1); String text; // repeat a test from testDateTimeISO to compare extended and not extended. text = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), timeZone); assertEquals("2002-02-23T09:11:12-03:00", text); // test fixture is the same as above, but now with extended format. text = DurationFormatUtils.formatPeriod(base.getTime().getTime(), cal.getTime().getTime(), DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN, false, timeZone); assertEquals("P32Y1M22DT9H11M12.001S", text); // test fixture from example in http://www.w3.org/TR/xmlschema-2/#duration cal.set(1971, Calendar.FEBRUARY, 3, 10, 30, 0); cal.set(Calendar.MILLISECOND, 0); text = DurationFormatUtils.formatPeriod(base.getTime().getTime(), cal.getTime().getTime(), DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN, false, timeZone); assertEquals("P1Y1M2DT10H30M0.000S", text); // want a way to say 'don't print the seconds in format()' or other fields for that matter: // assertEquals("P1Y2M3DT10H30M", text); }
@SystemDefaults(locale="en") @Test public void testSMTP() { TimeZone timeZone = TimeZone.getTimeZone("GMT-3"); Calendar june = createJuneTestDate(timeZone); assertFormats("Sun, 08 Jun 2003 10:11:12 -0300", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone, june); timeZone = FastTimeZone.getGmtTimeZone(); june = createJuneTestDate(timeZone); assertFormats("Sun, 08 Jun 2003 10:11:12 +0000", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone, june); }
final String value = DateFormatUtils.format(cal.getTimeInMillis(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/Paris")); assertEquals("long", "2009-10-16T08:42:16+02:00", value); final String value = DateFormatUtils.format(cal.getTimeInMillis(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Asia/Kolkata")); assertEquals("long", "2009-10-16T12:12:16+05:30", value); final String value = DateFormatUtils.format(cal.getTimeInMillis(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/London")); assertEquals("long", "2009-10-16T07:42:16+01:00", value); final String value = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/Paris")); assertEquals("calendar", "2009-10-16T08:42:16+02:00", value); final String value = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Asia/Kolkata")); assertEquals("calendar", "2009-10-16T12:12:16+05:30", value); final String value = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/London")); assertEquals("calendar", "2009-10-16T07:42:16+01:00", value);
@Test public void testCheckDefaults() { final FastDateFormat format = FastDateFormat.getInstance(); final FastDateFormat medium = FastDateFormat.getDateTimeInstance(FastDateFormat.SHORT, FastDateFormat.SHORT); assertEquals(medium, format); final SimpleDateFormat sdf = new SimpleDateFormat(); assertEquals(sdf.toPattern(), format.getPattern()); assertEquals(Locale.getDefault(), format.getLocale()); assertEquals(TimeZone.getDefault(), format.getTimeZone()); }
@Test public void test_getInstance_String() { final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy"); final FastDateFormat format2 = FastDateFormat.getInstance("MM-DD-yyyy"); final FastDateFormat format3 = FastDateFormat.getInstance("MM-DD-yyyy"); assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2); assertSame(format2, format3); assertEquals("MM/DD/yyyy", format1.getPattern()); assertEquals(TimeZone.getDefault(), format1.getTimeZone()); assertEquals(TimeZone.getDefault(), format2.getTimeZone()); }
/** * Value that must be an ISO formatted timestamp */ public static PactDslJsonRootValue timestamp() { return timestamp(DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()); }
/** * Value that must be formatted as an ISO date */ public static PactDslJsonRootValue date() { return date(DateFormatUtils.ISO_DATE_FORMAT.getPattern()); }
/** * Returns the value in Json Format */ @Override public JsonElement toJsonObject() { String utcTime = DateFormatUtils.formatUTC(getValue(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()); return (JsonElement) new JsonPrimitive(utcTime); }
public CachingDateFormatter(FastDateFormat fastDateFormat) { this.fastDateFormat = fastDateFormat; onSecond = fastDateFormat.getPattern().indexOf("SSS") == -1; long current = System.currentTimeMillis(); this.cachedTime = new AtomicReference<CachedTime>(new CachedTime(current, fastDateFormat.format(current))); }
public static String getDateFormat() { try { return FastDateFormat.getDateInstance( FastDateFormat.SHORT, MgnlContext.getLocale()).getPattern(); } catch (IllegalStateException e) { // this happens if the context is not (yet) set return DateUtil.YYYY_MM_DD; } }
/** * Attribute that must be formatted as an ISO date * @param name attribute name */ public PactDslJsonBody date(String name) { String pattern = DateFormatUtils.ISO_DATE_FORMAT.getPattern(); generators.addGenerator(Category.BODY, matcherKey(name), new DateGenerator(pattern)); body.put(name, DateFormatUtils.ISO_DATE_FORMAT.format(new Date(DATE_2000))); matchers.addRule(matcherKey(name), matchDate(pattern)); return this; }