org.joda.time.format.DateTimeFormatter.withLocale()方法的使用及代码示例

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

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

DateTimeFormatter.withLocale介绍

[英]Returns a new formatter with a different locale that will be used for printing and parsing.

A DateTimeFormatter is immutable, so a new instance is returned, and the original is unaltered and still usable.
[中]返回将用于打印和分析的具有不同区域设置的新格式化程序。
DateTimeFormatter是不可变的,因此会返回一个新实例,而原始实例是不变的,并且仍然可用。

代码示例

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

private DateTimeFormatter applyLocale(DateTimeFormatter dateTimeFormatter) {
  return dateTimeFormatter.withLocale(Locale.US);
}

代码示例来源:origin: joda-time/joda-time

/**
 * Output the date using the specified format pattern.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) throws IllegalArgumentException {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

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

/**
 * Obtain a DateTimeFormatter with user-specific settings applied to the given base Formatter.
 * @param formatter the base formatter that establishes default formatting rules
 * (generally user independent)
 * @param locale the current user locale (may be {@code null} if not known)
 * @return the user-specific DateTimeFormatter
 */
public static DateTimeFormatter getFormatter(DateTimeFormatter formatter, @Nullable Locale locale) {
  DateTimeFormatter formatterToUse = (locale != null ? formatter.withLocale(locale) : formatter);
  JodaTimeContext context = getJodaTimeContext();
  return (context != null ? context.getFormatter(formatterToUse) : formatterToUse);
}

代码示例来源:origin: joda-time/joda-time

/**
 * Output the date using the specified format pattern.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) throws IllegalArgumentException {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

代码示例来源:origin: joda-time/joda-time

/**
 * Output the date using the specified format pattern.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) throws IllegalArgumentException {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

代码示例来源:origin: joda-time/joda-time

/**
 * Output the time using the specified format pattern.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) throws IllegalArgumentException {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

代码示例来源:origin: joda-time/joda-time

/**
 * Output the year-month using the specified format pattern.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) throws IllegalArgumentException {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

代码示例来源:origin: joda-time/joda-time

/**
 * Output the instant using the specified format pattern.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @return the formatted string, not null
 * @see  org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) throws IllegalArgumentException {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

代码示例来源:origin: joda-time/joda-time

/**
 * Output the month-day using the specified format pattern.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) throws IllegalArgumentException {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

代码示例来源:origin: JodaOrg/joda-time

/**
 * Output the month-day using the specified format pattern.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) throws IllegalArgumentException {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

代码示例来源:origin: JodaOrg/joda-time

/**
 * Output the time using the specified format pattern.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) throws IllegalArgumentException {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

代码示例来源:origin: JodaOrg/joda-time

/**
 * Output the date using the specified format pattern.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) throws IllegalArgumentException {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

代码示例来源:origin: JodaOrg/joda-time

/**
 * Output the date using the specified format pattern.
 *
 * @param pattern  the pattern specification, null means use <code>toString</code>
 * @param locale  Locale to use, null means default
 * @see org.joda.time.format.DateTimeFormat
 */
public String toString(String pattern, Locale locale) throws IllegalArgumentException {
  if (pattern == null) {
    return toString();
  }
  return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
}

代码示例来源:origin: org.springframework/spring-context

/**
 * Obtain a DateTimeFormatter with user-specific settings applied to the given base Formatter.
 * @param formatter the base formatter that establishes default formatting rules
 * (generally user independent)
 * @param locale the current user locale (may be {@code null} if not known)
 * @return the user-specific DateTimeFormatter
 */
public static DateTimeFormatter getFormatter(DateTimeFormatter formatter, @Nullable Locale locale) {
  DateTimeFormatter formatterToUse = (locale != null ? formatter.withLocale(locale) : formatter);
  JodaTimeContext context = getJodaTimeContext();
  return (context != null ? context.getFormatter(formatterToUse) : formatterToUse);
}

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

private static Slice dateFormat(ISOChronology chronology, Locale locale, long timestamp, Slice formatString)
{
  DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString)
      .withChronology(chronology)
      .withLocale(locale);
  return utf8Slice(formatter.print(timestamp));
}

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

private static Slice formatDatetime(ISOChronology chronology, Locale locale, long timestamp, Slice formatString)
{
  try {
    return utf8Slice(DateTimeFormat.forPattern(formatString.toStringUtf8())
        .withChronology(chronology)
        .withLocale(locale)
        .print(timestamp));
  }
  catch (IllegalArgumentException e) {
    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
  }
}

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

public CustomDateTimeJsonFieldDecoder(DecoderColumnHandle columnHandle)
{
  this.columnHandle = requireNonNull(columnHandle, "columnHandle is null");
  if (!SUPPORTED_TYPES.contains(columnHandle.getType())) {
    throwUnsupportedColumnType(columnHandle);
  }
  checkArgument(columnHandle.getFormatHint() != null, "format hint not defined for column '%s'", columnHandle.getName());
  try {
    formatter = DateTimeFormat.forPattern(columnHandle.getFormatHint()).withLocale(Locale.ENGLISH).withZoneUTC();
  }
  catch (IllegalArgumentException e) {
    throw new PrestoException(
        GENERIC_USER_ERROR,
        format("invalid joda pattern '%s' passed as format hint for column '%s'", columnHandle.getFormatHint(), columnHandle.getName()));
  }
}

代码示例来源: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: prestodb/presto

@Description("parses the specified date/time by the given format")
@ScalarFunction
@LiteralParameters({"x", "y"})
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long parseDatetime(ConnectorSession session, @SqlType("varchar(x)") Slice datetime, @SqlType("varchar(y)") Slice formatString)
{
  try {
    return packDateTimeWithZone(parseDateTimeHelper(
        DateTimeFormat.forPattern(formatString.toStringUtf8())
            .withChronology(getChronology(session.getTimeZoneKey()))
            .withOffsetParsed()
            .withLocale(session.getLocale()),
        datetime.toStringUtf8()));
  }
  catch (IllegalArgumentException e) {
    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
  }
}

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

@ScalarFunction
@LiteralParameters({"x", "y"})
@SqlType(StandardTypes.TIMESTAMP)
public static long dateParse(ConnectorSession session, @SqlType("varchar(x)") Slice dateTime, @SqlType("varchar(y)") Slice formatString)
{
  DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString)
      .withChronology(session.isLegacyTimestamp() ? getChronology(session.getTimeZoneKey()) : UTC_CHRONOLOGY)
      .withLocale(session.getLocale());
  try {
    return formatter.parseMillis(dateTime.toStringUtf8());
  }
  catch (IllegalArgumentException e) {
    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
  }
}

相关文章