private static boolean checkFormatRfc822Date() throws ParseException { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss z", Locale.US); sdf.setTimeZone(new SimpleTimeZone(0, "GMT")); String expected = sdf.format(date); String actual = DateUtils.rfc822DateFormat.print(date.getTime()); if (expected.equals(actual)) { Date expectedDate = sdf.parse(expected); Date actualDate2 = new Date(DateUtils.rfc822DateFormat.parseMillis(actual)); return expectedDate.equals(actualDate2); } return false; }
private static boolean checkAlternateIso8601DateFormat() throws ParseException { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); sdf.setTimeZone(new SimpleTimeZone(0, "GMT")); String expected = sdf.format(date); String actual = DateUtils.alternateIso8601DateFormat.print(date .getTime()); if (expected.equals(actual)) { Date expectedDate = sdf.parse(expected); DateTime actualDateTime = DateUtils.alternateIso8601DateFormat .parseDateTime(actual); return expectedDate.getTime() == actualDateTime.getMillis(); } return false; }
@Override public Date resolveEndDate(String endDateString) { return ISODateTimeFormat.dateTimeParser().withZone(DateTimeZone.forTimeZone(clockReader.getCurrentTimeZone())).parseDateTime(endDateString).toCalendar(null).getTime(); }
public static long parseHiveTimestamp(String value, DateTimeZone timeZone) { return HIVE_TIMESTAMP_PARSER.withZone(timeZone).parseMillis(value); }
String dateTime = "11/15/2013 08:00:00"; // Format for input DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss"); // Parsing the date DateTime jodatime = dtf.parseDateTime(dateTime); // Format for output DateTimeFormatter dtfOut = DateTimeFormat.forPattern("MM/dd/yyyy"); // Printing the date System.out.println(dtfOut.print(jodatime));
private static boolean checkTT0031561767() throws ParseException { String input = "Fri, 16 May 2014 23:56:46 GMT"; Date date = new Date(DateUtils.rfc822DateFormat.parseMillis(input)); return input.equals(DateUtils.rfc822DateFormat.print(date.getTime())); }
public static Date parseDate(Object d, Date dflt) { if (d == null) return dflt; if (d instanceof Long) return new Date(((Long) d).longValue()); if (d instanceof String) return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime((String) d).toDate(); if (d instanceof Date) return (Date) d; assert false; return dflt; }
/** * Parses the specified date string as a compressedIso8601DateFormat ("yyyyMMdd'T'HHmmss'Z'") and returns the Date * object. * * @param dateString * The date string to parse. * * @return The parsed Date object. */ public static Date parseCompressedISO8601Date(String dateString) { try { return new Date(compressedIso8601DateFormat.parseMillis(dateString)); } catch (RuntimeException ex) { throw handleException(ex); } }
@Test public void dateToStringWithFormat() { JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar(); registrar.setDateTimeFormatter(org.joda.time.format.DateTimeFormat.shortDateTime()); setup(registrar); Date date = new Date(); Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.class), TypeDescriptor.valueOf(String.class)); String expected = JodaTimeContextHolder.getFormatter(org.joda.time.format.DateTimeFormat.shortDateTime(), Locale.US).print(new DateTime(date)); assertEquals(expected, actual); }
String temp = tempDateStringForJodaTime(dateString); try { if (temp.equals(dateString)) { return new Date(iso8601DateFormat.parseMillis(dateString)); final long milliLess365Days = iso8601DateFormat.parseMillis(temp); final long milli = milliLess365Days + MILLI_SECONDS_OF_365_DAYS; if (milli < 0) { // overflow! return new Date(iso8601DateFormat.parseMillis(dateString)); return new Date(milli); } catch (IllegalArgumentException e) { try { return new Date(alternateIso8601DateFormat.parseMillis(dateString));
/** * Formats the specified date as an ISO 8601 string. * * @param date * The date to format. * * @return The ISO 8601 string representing the specified date. */ public static String formatISO8601Date(Date date) { try { return iso8601DateFormat.print(date.getTime()); } catch(RuntimeException ex) { throw handleException(ex); } }
private void testJodaStylePatterns(String style, Locale locale, Date date) throws Exception { DateFormatter formatter = new DateFormatter(); formatter.setTimeZone(UTC); formatter.setStylePattern(style); DateTimeFormatter jodaFormatter = DateTimeFormat.forStyle(style).withLocale(locale).withZone(DateTimeZone.UTC); String jodaPrinted = jodaFormatter.print(date.getTime()); assertThat("Unable to print style pattern " + style, formatter.print(date, locale), is(equalTo(jodaPrinted))); assertThat("Unable to parse style pattern " + style, formatter.parse(jodaPrinted, locale), is(equalTo(date))); }
/** * Returns the current time in yyMMdd-hhmmss format. */ public static String yyMMdd_hhmmss() { return DateTimeFormat.forPattern("yyMMdd-hhmmss").print(new DateTime()); } }
private void write(SerializeWriter out, ReadablePartial object, String format) { DateTimeFormatter formatter; if (format == formatter_iso8601_pattern) { formatter = formatter_iso8601; } else { formatter = DateTimeFormat.forPattern(format); } String text = formatter.print(object); out.writeString(text); } }
@Test public void createDateTimeFormatterWithTimeZone() { factory.setPattern("yyyyMMddHHmmss Z"); factory.setTimeZone(TEST_TIMEZONE); DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(TEST_TIMEZONE); DateTime dateTime = new DateTime(2009, 10, 21, 12, 10, 00, 00, dateTimeZone); String offset = (TEST_TIMEZONE.equals(NEW_YORK) ? "-0400" : "+0200"); assertThat(factory.createDateTimeFormatter().print(dateTime), is("20091021121000 " + offset)); }
public static Date strToDate(String dateTimeStr){ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT); DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr); return dateTime.toDate(); }
@Test(groups = "fast") public void testComputeUTCDateTimeFromLocalDateC() { final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTimeC); final DateTimeZone timeZone = DateTimeZone.forOffsetHours(8); refreshCallContext(effectiveDateTime, timeZone); final LocalDate endDate = new LocalDate(2013, 01, 20); final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate); assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0); }