importjava.time.ZonedDateTime;importjava.time.format.DateTimeFormatter;importjava.time.ZoneId;publicclassFormatDateTimeExample{publicstaticvoidmain(String[]args){ZonedDateTimezonedDateTime=ZonedDateTime.now(ZoneId.of("America/New_York"));// 定义格式DateTimeFormatterformatter=DateTimeFormatter.ofPattern("yyyy-...
可以使用withZone方法为格式化器指定时区。 import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { LocalDateTime localDateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPatt...
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai")); Date d2 = new Date(); Instant i2 = Instant.now(); ZonedDateTime z2 = ZonedDateTime.now(); LocalDateTime l2 = LocalDateTime.now(); TimeZone.setDefault(TimeZone.getTimeZone("Australia/Darwin")); Date d3 = new Date(); Inst...
.withZone(ZoneOffset.ofHours(9));// 2024-09-02 21:45:49System.out.println(utc9Formatter.format(localDateTime));// 2024-09-02 22:45:49System.out.println(utc9Formatter.format(zonedDateTime));// 2024-09-02 22:45:49System.out.println(utc9Formatter.format(offsetDateTime));// 2024-09-02 ...
DateTime dt=newDateTime(); String date=dt.toString(pattern);returndate; }/*** 按照时区转换时间 * *@paramdate *@paramtimeZone * 时区 *@paramparrten *@return*/@NullablepublicstaticString format(Date date, TimeZone timeZone, String parrten) {if(date ==null) {returnnull; ...
= ZonedDateTime.parse("2020-06-01T10:15:30+09:00[Asia/Tokyo]"); 在OffsetDateTime的情况下,字符串必须遵循DateTimeFormatter.ISO_OFFSET_DATE_TIME模式,例如2007-12-03T10:15:30+01:00,如下代码片段所示: 代码语言:javascript 复制 OffsetDateTime offsetDateTime ...
JDK提供了TimeZone表示时区的概念,但它在Date里并无任何体现,只能使用在格式化器上,这种设计着实让我再一次看不懂了。 罪状六:线程不安全的格式化器 关于Date的格式化,站在架构设计的角度来看,首先不得不吐槽的是Date明明属于java.util包,那么它的格式化器DateFormat为毛却跑到java.text里去了呢?这种依赖管理的什么...
LocalDate date = LocalDate.now(); String text = date.format(formatter); LocalDate parsedDate = LocalDate.parse(text, formatter); このフォーマットに加え、必要なLocale、Chronology、ZoneId、およびDecimalStyleを持つフォーマッタを作成できます。 withLocaleメソッドは、ロケールをオーバーラ...
LocalDate date = LocalDate.now(); String text = date.format(formatter); LocalDate parsedDate = LocalDate.parse(text, formatter); このフォーマットに加え、必要なLocale、Chronology、ZoneId、およびDecimalStyleを持つフォーマッタを作成できます。 withLocaleメソッドは、ロケールをオーバーラ...
The#withZone withZonemethod returns a new formatter that overrides the zone. If overridden, the date-time value is converted to a ZonedDateTime with the requested ZoneId before formatting. During parsing the ZoneId is applied before the value is returned. ...