制作DateFormat Threadsafe。使用什么,同步或线程本地 [英] Making DateFormat Threadsafe. What to use, synchronized or Thread local

查看:146
本文介绍了制作DateFormat Threadsafe。使用什么,同步或线程本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让以下代码线程安全。实现它的最佳方法是什么?

I want to make following code thread safe. What is the best way to achieve it?

private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance();

public static final String eventTypeToDateTimeString(long timestamp)
{
   return DATE_FORMAT.format(new Date(timestamp));
}


推荐答案

避免遗留日期时间classes



与最早版本的Java捆绑在一起的麻烦的旧日期时间类已经被java.time类所取代。 java.time类是线程安全并使用不可变对象

用java.time类型替换格式化程序和日期类型以自动获得线程安全。

Replace your formatter and date types with java.time types to automatically get thread-safety.

定义 DateTimeFormatter 全球如果需要的话。该类可以自动本地化生成的字符串,也可以指定某种格式。

Define your DateTimeFormatter globally if so desired. That class can automatically localize the string being generated, or you can specify a certain format.


  • 指定 FormatStyle 以确定缩写的长度。

  • 指定 区域设置 确定(a)翻译日期名称,月份名称等的人类语言,以及(b)决定缩写,大小写,标点符号等问题的文化规范。

  • 为调整时刻的时区指定 ZoneId

  • Specify a FormatStyle to determine length of abbreviation.
  • Specify a Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, and such.
  • Specify a ZoneId for a time zone in which to adjust the moment.

即时 类代表中时间轴上的一个时刻UTC ,分辨率纳秒 ZonedDateTime class将 Instant 调整为特定时区。

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds. The ZonedDateTime class adjusts an Instant into a particular time zone.

您的代码,已翻译为java.time类。在实际工作中,我会将其分解为多行,并将陷入异常。

Your code, translated to java.time classes. In real work I would break this out into multiple lines, and would trap for exceptions.

private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.CANADA_FRENCH ) ;
private static final ZoneId ZONE_ID = ZoneId.of( "America/Montreal" );

public static final String eventTypeToDateTimeString(long timestamp)
{
   return Instant.ofEpochMilli( timestamp ).atZone( ZONE_ID ).format( DATE_TIME_FORMATTER );
}



不要将日期时间跟踪为从纪元开始的数量



我不建议传递 long 作为表示日期时间值的方法。由于人类无法辨别日期时间值的含义,因此使调试和记录困难。相反,传递java.time类型,例如 即时 。使用java.time类型可提供类型安全性,并使您的代码更加自我记录。

Do not track date-time as count-from-epoch

I do not advise passing around a long as a way of representing date-time values. Makes debugging and logging difficulty as a human cannot discern the meaning of the date-time value. Instead, pass around the java.time types such as Instant. Using the java.time types provides type-safety and makes your code more self-documenting.

java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如 java.util.Date 日历 ,& SimpleDateFormat

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time 项目,现已进入维护模式 ,建议迁移到 java.time classes。

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial 。并搜索Stack Overflow以获取许多示例和解释。规范是 JSR 310

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

您可以直接与数据库交换 java.time 对象。使用符合 JDBC驱动程序 / jeps / 170rel =nofollow noreferrer> JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql。* 类。

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

从哪里获取java.time班?

Where to obtain the java.time classes?


  • Java SE 8 Java SE 9 Java SE 10 Java SE 11 及更高版本 - 部分带有捆绑实现的标准Java API。


    • Java 9增加了一些小功能和修复。

    • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

      ThreeTen-Extra 项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在这里找到一些有用的课程,例如 Interval YearWeek YearQuarter 更多

      这篇关于制作DateFormat Threadsafe。使用什么,同步或线程本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆