根据语言环境在java中格式化日期时间

问题描述 投票:0回答:1

我希望根据每个日期时间组件的标准(下面的对象)格式化日期和时间。并且它们之间的分隔符应根据区域设置来获取。有没有办法在java中实现这一点

   { year: numeric, month: long, day: two-digit, weekday: short }   
   { year: numeric, month: twoDigit, day: twoDigit }
   For example consider second object and locale Locale.US and Locale.FRANCE.
   For Locale.US date should be 26/03/2024
   For Locale.France date should be 26.03.2024
java datetime
1个回答
0
投票

要表示日期,请使用

LocalDate
类。

LocalDate localDate = LocalDate.of( 2024 , Month.MARCH , 26 ) ;

要生成文本,请使用

DateTimeFormatter
对象。

要指定特定格式,请定义格式化模式。

DateTimeFormatter f = DateTimeFormatter.ofPattern ( "dd/MM/uuuu" ) ;
String output = localDate.format( f ) ;

要在生成文本时自动本地化,请使用

DateTimeFormatter.ofLocalizedDate

© www.soinside.com 2019 - 2024. All rights reserved.