/** * <p>Formats a date/time into a specific pattern in a time zone and locale.</p> * * @param date the date to format, not null * @param pattern the pattern to use to format the date, not null, not null * @param timeZone the time zone to use, may be <code>null</code> * @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 TimeZone timeZone, final Locale locale) { final FastDateFormat df = FastDateFormat.getInstance(pattern, timeZone, locale); return df.format(date); }
/** * <p>Formats a calendar into a specific pattern in a time zone and locale.</p> * * @param calendar the calendar to format, not null * @param pattern the pattern to use to format the calendar, not null * @param timeZone the time zone to use, may be <code>null</code> * @param locale the locale to use, may be <code>null</code> * @return the formatted calendar * @see FastDateFormat#format(Calendar) * @since 2.4 */ public static String format(final Calendar calendar, final String pattern, final TimeZone timeZone, final Locale locale) { final FastDateFormat df = FastDateFormat.getInstance(pattern, timeZone, locale); return df.format(calendar); }
public String format(final long timestampMillis) { CachedTime cached = cachedTime.get(); long timestamp = onSecond ? timestampMillis / 1000 : timestampMillis; if (timestamp != cached.timestamp) { final CachedTime newCachedTime = new CachedTime(timestamp, fastDateFormat.format(timestampMillis)); // 尝试放入cachedTime cachedTime.compareAndSet(cached, newCachedTime); // 与log4j2做法不同,无论是否放入成功,都使用自己的值 cached = newCachedTime; } return cached.formatted; }
public String format(final long timestampMillis) { CachedTime cached = cachedTime.get(); long timestamp = onSecond ? timestampMillis / 1000 : timestampMillis; if (timestamp != cached.timestamp) { final CachedTime newCachedTime = new CachedTime(timestamp, fastDateFormat.format(timestampMillis)); if (cachedTime.compareAndSet(cached, newCachedTime)) { cached = newCachedTime; } else { cached = cachedTime.get(); } } return cached.formatted; }
static public String getCurrentTimeStr() { Calendar c = Calendar.getInstance(); c.setTime(new Date()); FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss"); return format.format(c.getTime()); } }
public static String formatToTimeStr(long millis, String pattern) { return getDateFormat(pattern).format(new Date(millis)); }
public static String dateToString(Date date, String pattern) { return getDateFormat(pattern).format(date); }
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))); }
/** * 格式化日期, 仅用于不固定pattern不固定的情况. * * 否否则直接使用本类中封装好的FastDateFormat. * * FastDateFormat.getInstance()已经做了缓存,不会每次创建对象,但直接使用对象仍然能减少在缓存中的查找. */ public static String formatDate(@NotNull String pattern, long date) { return FastDateFormat.getInstance(pattern).format(date); }
@Test public void isoDateFormat() { Date date = new Date(116, 10, 1, 12, 23, 44); assertThat(DateFormatUtil.ISO_FORMAT.format(date)).contains("2016-11-01T12:23:44.000"); assertThat(DateFormatUtil.ISO_ON_SECOND_FORMAT.format(date)).contains("2016-11-01T12:23:44"); assertThat(DateFormatUtil.ISO_ON_DATE_FORMAT.format(date)).isEqualTo("2016-11-01"); }
/** * 格式化日期, 仅用于pattern不固定的情况. * * 否则直接使用本类中封装好的FastDateFormat. * * FastDateFormat.getInstance()已经做了缓存,不会每次创建对象,但直接使用对象仍然能减少在缓存中的查找. */ public static String formatDate(@NotNull String pattern, @NotNull Date date) { return FastDateFormat.getInstance(pattern).format(date); }
@Test public void testCalendarTimezoneRespected() { final Calendar cal = Calendar.getInstance(timeZone); final SimpleDateFormat sdf = new SimpleDateFormat(PATTERN); sdf.setTimeZone(timeZone); final String expectedValue = sdf.format(cal.getTime()); final String actualValue = FastDateFormat.getInstance(PATTERN, this.timeZone).format(cal); assertEquals(expectedValue, actualValue); }
@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); }
@Test public void testLANG_1152() { final TimeZone utc = FastTimeZone.getGmtTimeZone(); final Date date = new Date(Long.MAX_VALUE); String dateAsString = FastDateFormat.getInstance("yyyy-MM-dd", utc, Locale.US).format(date); assertEquals("292278994-08-17", dateAsString); dateAsString = FastDateFormat.getInstance("dd/MM/yyyy", utc, Locale.US).format(date); assertEquals("17/08/292278994", dateAsString); }
/** * According to LANG-916 (https://issues.apache.org/jira/browse/LANG-916), * the format method did contain a bug: it did not use the TimeZone data. * * This method test that the bug is fixed. */ @Test public void testLang916() throws Exception { final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris")); cal.clear(); cal.set(2009, 9, 16, 8, 42, 16); // calendar fast. { final String value = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss Z", TimeZone.getTimeZone("Europe/Paris")).format(cal); assertEquals("calendar", "2009-10-16T08:42:16 +0200", value); } { final String value = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss Z", TimeZone.getTimeZone("Asia/Kolkata")).format(cal); assertEquals("calendar", "2009-10-16T12:12:16 +0530", value); } { final String value = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss Z", TimeZone.getTimeZone("Europe/London")).format(cal); assertEquals("calendar", "2009-10-16T07:42:16 +0100", value); } }
@SystemDefaults(timezone="UTC") @Test public void testTimeZoneAsZ() throws Exception { final Calendar c = Calendar.getInstance(FastTimeZone.getGmtTimeZone()); final FastDateFormat noColonFormat = FastDateFormat.getInstance("Z"); assertEquals("+0000", noColonFormat.format(c)); final FastDateFormat isoFormat = FastDateFormat.getInstance("ZZ"); assertEquals("Z", isoFormat.format(c)); final FastDateFormat colonFormat = FastDateFormat.getInstance("ZZZ"); assertEquals("+00:00", colonFormat.format(c)); }
@Test @Ignore("for dev only") public void test3() throws Exception { FastDateFormat formatter = org.apache.kylin.common.util.DateFormat.getDateFormat("MMM dd, yyyy hh:mm:ss aa"); System.out.println(formatter.format(new Date())); String timeStr = "Jul 20, 2016 9:59:17 AM"; System.out.println(formatter.parse(timeStr).getTime()); }