显示带有月份名称的漂亮时间戳
Show a pretty timestamp with month name
我有一个 Timestamp
对象,我想展示它。
实际上,我正在使用 new Timestamp(System.currentTimeMillis()).toLocaleString()
,我得到的结果是:
27 déc. 2021 17:54:35
但是:
toLocaleString()
方法已弃用
- 它return是根据电脑的lang值,所以可以这样显示:
Dec 27, 2021 17:54:35 PM
这不是我想要的。在我的个人电脑上,用法语,没问题。但是,在 VPS 上就不行了。
此外,所有找到的帖子都只谈论格式而没有月份名称,这不是我想要的。
如何用法语轻松显示时间戳?
tl;博士
使用添加到旧 class Timestamp
的新转换方法转换为现代 java.time classes.
Timestamp myTimestamp = Timestamp.from( Instant.now() ); // Simulate receiving a `Timestamp` object from old code not yet updated to *java.time*.
System.out.println(
myTimestamp
.toInstant() // Convert from legacy class to modern class.
.atZone( // Adjust into a time zone.
ZoneId.of( "Asia/Tokyo" )
) // Returns a `ZonedDateTime` object.
.format(
DateTimeFormatter
.ofLocalizedDateTime(
FormatStyle.FULL
)
.withLocale(
Locale.FRANCE
)
) // Returns a `String` object, generated text.
);
查看此代码 run live at IdeOne.com。
mardi 28 décembre 2021 à 05:39:58 heure normale du Japon
详情
Timestamp
是可怕的遗留日期时间 classes 之一。这些 class 是几年前的 supplanted by the modern java.time class。切勿使用时间戳、日期、日历、SimpleDateFormat 等
捕获 UTC 中显示的当前时刻,偏移量为零小时-分钟-秒。
Instant instant = Instant.now() ;
捕捉特定时区的当前时刻。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
以自动本地化格式生成文本。指定区域设置以确定翻译中使用的人类语言以及缩写、大写、元素顺序等中使用的文化规范。
Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM ).withLocale( locale ) ;
String output = zdt.format( f ) ;
如果从数据类型类似于 SQL 标准类型 TIMESTAMP WITH TIME ZONE
的数据库列接收时刻,请在 Java 中使用 OffsetDateTime
class使用符合 JDBC 4.2 或更高版本的 JDBC 驱动程序。
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
调整到您想要的时区以产生一个 ZonedDateTime
对象。
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
所有这些都已经在 Stack Overflow 上处理过很多次了。搜索以了解更多信息。
我有一个 Timestamp
对象,我想展示它。
实际上,我正在使用 new Timestamp(System.currentTimeMillis()).toLocaleString()
,我得到的结果是:
27 déc. 2021 17:54:35
但是:
toLocaleString()
方法已弃用- 它return是根据电脑的lang值,所以可以这样显示:
Dec 27, 2021 17:54:35 PM
这不是我想要的。在我的个人电脑上,用法语,没问题。但是,在 VPS 上就不行了。
此外,所有找到的帖子都只谈论格式而没有月份名称,这不是我想要的。
如何用法语轻松显示时间戳?
tl;博士
使用添加到旧 class Timestamp
的新转换方法转换为现代 java.time classes.
Timestamp myTimestamp = Timestamp.from( Instant.now() ); // Simulate receiving a `Timestamp` object from old code not yet updated to *java.time*.
System.out.println(
myTimestamp
.toInstant() // Convert from legacy class to modern class.
.atZone( // Adjust into a time zone.
ZoneId.of( "Asia/Tokyo" )
) // Returns a `ZonedDateTime` object.
.format(
DateTimeFormatter
.ofLocalizedDateTime(
FormatStyle.FULL
)
.withLocale(
Locale.FRANCE
)
) // Returns a `String` object, generated text.
);
查看此代码 run live at IdeOne.com。
mardi 28 décembre 2021 à 05:39:58 heure normale du Japon
详情
Timestamp
是可怕的遗留日期时间 classes 之一。这些 class 是几年前的 supplanted by the modern java.time class。切勿使用时间戳、日期、日历、SimpleDateFormat 等
捕获 UTC 中显示的当前时刻,偏移量为零小时-分钟-秒。
Instant instant = Instant.now() ;
捕捉特定时区的当前时刻。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
以自动本地化格式生成文本。指定区域设置以确定翻译中使用的人类语言以及缩写、大写、元素顺序等中使用的文化规范。
Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM ).withLocale( locale ) ;
String output = zdt.format( f ) ;
如果从数据类型类似于 SQL 标准类型 TIMESTAMP WITH TIME ZONE
的数据库列接收时刻,请在 Java 中使用 OffsetDateTime
class使用符合 JDBC 4.2 或更高版本的 JDBC 驱动程序。
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
调整到您想要的时区以产生一个 ZonedDateTime
对象。
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
所有这些都已经在 Stack Overflow 上处理过很多次了。搜索以了解更多信息。