Refine search
/** * <p>Formats a date/time into a specific pattern in a locale.</p> * * @param millis the date to format expressed in milliseconds * @param pattern the pattern to use to format the date, not null * @param locale the locale to use, may be <code>null</code> * @return the formatted date */ public static String format(final long millis, final String pattern, final Locale locale) { return format(new Date(millis), pattern, null, locale); }
@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)); }
@Test public void testConstructor() { assertNotNull(new DateFormatUtils()); final Constructor<?>[] cons = DateFormatUtils.class.getDeclaredConstructors(); assertEquals(1, cons.length); assertTrue(Modifier.isPublic(cons[0].getModifiers())); assertTrue(Modifier.isPublic(DateFormatUtils.class.getModifiers())); assertFalse(Modifier.isFinal(DateFormatUtils.class.getModifiers())); }
private void assertFormats(final String expectedValue, final String pattern, final TimeZone timeZone, final Calendar cal) { assertEquals(expectedValue, DateFormatUtils.format(cal.getTime(), pattern, timeZone)); assertEquals(expectedValue, DateFormatUtils.format(cal.getTime().getTime(), pattern, timeZone)); assertEquals(expectedValue, DateFormatUtils.format(cal, pattern, timeZone)); }
/** * <p>Formats a date/time into a specific pattern in a locale.</p> * * @param date the date to format, not null * @param pattern the pattern to use to format the date, not null * @param locale the locale to use, may be <code>null</code> * @return the formatted date */ public static String format(final Date date, final String pattern, final Locale locale) { return format(date, pattern, null, locale); }
public String humanizeTimestamp(long timestamp) { if (taskDatePattern.isPresent() && timeZone.isPresent()) { return DateFormatUtils.format(timestamp, taskDatePattern.get(), timeZone.get()); } else if (taskDatePattern.isPresent()) { return DateFormatUtils.formatUTC(timestamp, taskDatePattern.get()); } else if (timeZone.isPresent()) { return DateFormatUtils.format(timestamp, DEFAULT_TIMESTAMP_FORMAT, timeZone.get()); } else { return DateFormatUtils.format(timestamp, DEFAULT_TIMESTAMP_FORMAT); } }
/** * <p>Formats a date/time into a specific pattern using the UTC time zone.</p> * * @param millis the date to format expressed in milliseconds * @param pattern the pattern to use to format the date, not null * @return the formatted date */ public static String formatUTC(long millis, String pattern) { return DateFormatUtils. formatUTC(millis, pattern); }
@Test public void testFormat() { final Calendar c = Calendar.getInstance(FastTimeZone.getGmtTimeZone()); c.set(2005, Calendar.JANUARY, 1, 12, 0, 0); c.setTimeZone(TimeZone.getDefault()); final StringBuilder buffer = new StringBuilder (); final int year = c.get(Calendar.YEAR); final int month = c.get(Calendar.MONTH) + 1; final int day = c.get(Calendar.DAY_OF_MONTH); final int hour = c.get(Calendar.HOUR_OF_DAY); buffer.append (year); buffer.append(month); buffer.append(day); buffer.append(hour); assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH")); assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime().getTime(), "yyyyMdH")); assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH", Locale.US)); assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime().getTime(), "yyyyMdH", Locale.US)); }
/** * <p>Formats a date/time into a specific pattern using the UTC time zone.</p> * * @param date the date to format, not null * @param pattern the pattern to use to format the date, not null * @return the formatted date */ public static String formatUTC(final Date date, final String pattern) { return format(date, pattern, UTC_TIME_ZONE, null); }
public String humanizeTimestamp(long timestamp) { if (taskDatePattern.isPresent() && timeZone.isPresent()) { return DateFormatUtils.format(timestamp, taskDatePattern.get(), timeZone.get()); } else if (taskDatePattern.isPresent()) { return DateFormatUtils.formatUTC(timestamp, taskDatePattern.get()); } else if (timeZone.isPresent()) { return DateFormatUtils.format(timestamp, DEFAULT_TIMESTAMP_FORMAT, timeZone.get()); } else { return DateFormatUtils.format(timestamp, DEFAULT_TIMESTAMP_FORMAT); } }
/** * <p>Formats a date/time into a specific pattern using the UTC time zone.</p> * * @param date the date to format, not null * @param pattern the pattern to use to format the date, not null * @param locale the locale to use, may be <code>null</code> * @return the formatted date */ public static String formatUTC(Date date, String pattern, Locale locale) { return DateFormatUtils.formatUTC(date, pattern, locale); }
/** * <p>Formats a date/time into a specific pattern.</p> * * @param millis the date to format expressed in milliseconds * @param pattern the pattern to use to format the date, not null * @return the formatted date */ public static String format(final long millis, final String pattern) { return format(new Date(millis), pattern, null, null); }
@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); }
/** * <p>Formats a calendar into a specific pattern.</p> * * @param calendar the calendar to format, not null * @param pattern the pattern to use to format the calendar, not null * @return the formatted calendar * @see FastDateFormat#format(Calendar) * @since 2.4 */ public static String format(final Calendar calendar, final String pattern) { return format(calendar, pattern, null, null); }
/** * <p>Formats a date/time into a specific pattern using the UTC time zone.</p> * * @param date the date to format, not null * @param pattern the pattern to use to format the date, not null * @return the formatted date */ public static String formatUTC(Date date, String pattern) { return DateFormatUtils.formatUTC(date, pattern); }
/** * <p>Formats a date/time into a specific pattern using the UTC time zone.</p> * * @param millis the date to format expressed in milliseconds * @param pattern the pattern to use to format the date, not null * @return the formatted date */ public static String formatUTC(final long millis, final String pattern) { return format(new Date(millis), pattern, UTC_TIME_ZONE, null); }
/** * <p>Formats a date/time into a specific pattern in a time zone.</p> * * @param date the date to format, not null * @param pattern the pattern to use to format the date, not null * @param timeZone the time zone to use, may be <code>null</code> * @return the formatted date */ public static String format(final Date date, final String pattern, final TimeZone timeZone) { return format(date, pattern, timeZone, null); }
/** * <p>Formats a date/time into a specific pattern using the UTC time zone.</p> * * @param millis the date to format expressed in milliseconds * @param pattern the pattern to use to format the date, not null * @param locale the locale to use, may be <code>null</code> * @return the formatted date */ public static String formatUTC(long millis, String pattern, Locale locale) { return DateFormatUtils.formatUTC(millis, pattern, locale); }
/** * <p>Formats a date/time into a specific pattern using the UTC time zone.</p> * * @param millis the date to format expressed in milliseconds * @param pattern the pattern to use to format the date, not null * @param locale the locale to use, may be <code>null</code> * @return the formatted date */ public static String formatUTC(final long millis, final String pattern, final Locale locale) { return format(new Date(millis), pattern, UTC_TIME_ZONE, locale); }
/** * <p>Formats a date/time into a specific pattern using the UTC time zone.</p> * * @param date the date to format, not null * @param pattern the pattern to use to format the date, not null * @param locale the locale to use, may be <code>null</code> * @return the formatted date */ public static String formatUTC(final Date date, final String pattern, final Locale locale) { return format(date, pattern, UTC_TIME_ZONE, locale); }