org.joda.time.format.DateTimeFormatter类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(530)

本文整理了Java中org.joda.time.format.DateTimeFormatter类的一些代码示例,展示了DateTimeFormatter类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateTimeFormatter类的具体详情如下:
包路径:org.joda.time.format.DateTimeFormatter
类名称:DateTimeFormatter

DateTimeFormatter介绍

[英]Controls the printing and parsing of a datetime to and from a string.

This class is the main API for printing and parsing used by most applications. Instances of this class are created via one of three factory classes:

  • DateTimeFormat - formats by pattern and style
  • ISODateTimeFormat - ISO8601 formats
  • DateTimeFormatterBuilder - complex formats created via method calls

An instance of this class holds a reference internally to one printer and one parser. It is possible that one of these may be null, in which case the formatter cannot print/parse. This can be checked via the #isPrinter()and #isParser() methods.

The underlying printer/parser can be altered to behave exactly as required by using one of the decorator modifiers:

  • #withLocale(Locale) - returns a new formatter that uses the specified locale
  • #withZone(DateTimeZone) - returns a new formatter that uses the specified time zone
  • #withChronology(Chronology) - returns a new formatter that uses the specified chronology
  • #withOffsetParsed() - returns a new formatter that returns the parsed time zone offset
    Each of these returns a new formatter (instances of this class are immutable).

The main methods of the class are the printXxx and parseXxx methods. These are used as follows:

// print using the defaults (default locale, chronology/zone of the datetime) 
String dateStr = formatter.print(dt); 
// print using the French locale 
String dateStr = formatter.withLocale(Locale.FRENCH).print(dt); 
// print using the UTC zone 
String dateStr = formatter.withZone(DateTimeZone.UTC).print(dt); 
// parse using the Paris zone 
DateTime date = formatter.withZone(DateTimeZone.forID("Europe/Paris")).parseDateTime(str);

[中]

代码示例

代码示例来源:origin: aws/aws-sdk-java

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;
}

代码示例来源:origin: aws/aws-sdk-java

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;
}

代码示例来源:origin: Activiti/Activiti

@Override
public Date resolveEndDate(String endDateString) {
 return ISODateTimeFormat.dateTimeParser().withZone(DateTimeZone.forTimeZone(clockReader.getCurrentTimeZone())).parseDateTime(endDateString).toCalendar(null).getTime();
}

代码示例来源:origin: prestodb/presto

public static long parseHiveTimestamp(String value, DateTimeZone timeZone)
{
  return HIVE_TIMESTAMP_PARSER.withZone(timeZone).parseMillis(value);
}

代码示例来源:origin: stackoverflow.com

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));

代码示例来源:origin: prestodb/presto

@Deprecated
public static String printTimeWithoutTimeZone(TimeZoneKey timeZoneKey, long value)
{
  return TIME_FORMATTER.withZone(getDateTimeZone(timeZoneKey)).print(value);
}

代码示例来源:origin: aws/aws-sdk-java

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()));
}

代码示例来源:origin: loklak/loklak_server

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;
}

代码示例来源:origin: aws/aws-sdk-java

/**
 * 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);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: aws/aws-sdk-java

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));

代码示例来源:origin: aws/aws-sdk-java

/**
 * 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);
  }
}

代码示例来源:origin: gocd/gocd

public static String formatIso8601ForCCTray(Date date) {
  if (date == null) {
    return null;
  }
  return formatterUtc.print(date.getTime());
}

代码示例来源:origin: SonarSource/sonarqube

@CheckForNull
public static String formatDateTime(@Nullable Date date) {
 if (date != null) {
  return ISODateTimeFormat.dateTime().print(date.getTime());
 }
 return null;
}

代码示例来源:origin: spring-projects/spring-framework

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)));
}

代码示例来源:origin: aws/aws-sdk-java

/**
   * Returns the current time in yyMMdd-hhmmss format.
   */
  public static String yyMMdd_hhmmss() {
    return DateTimeFormat.forPattern("yyMMdd-hhmmss").print(new DateTime());
  }
}

代码示例来源:origin: alibaba/fastjson

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);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@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));
}

代码示例来源:origin: qiurunze123/miaosha

public static Date strToDate(String dateTimeStr){
  DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
  DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
  return dateTime.toDate();
}

代码示例来源:origin: killbill/killbill

@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);
}

相关文章