When dealing with a zone offset, it is more logical to use anOffsetDateTimeas opposed to aZonedDateTime. One approach to adjusting your local time with an offset is to specify the time in UTC and indicate that you desire the local time in another time zone. Thus, an example of this app...
.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 ...
在ZonedDateTime的情况下,字符串必须遵循DateTimeFormatter.ISO_ZONED_DATE_TIME模式,例如2020-06-01T10:15:30+09:00[Asia/Tokyo],如下代码片段所示: ZonedDateTime zonedDateTime = ZonedDateTime.parse("2020-06-01T10:15:30+09:00[Asia/Tokyo]"); 1. 2. 在OffsetDateTime的情况下,字符串必须遵循DateTimeF...
可以使用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...
= 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 ...
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; ...
JDK提供了TimeZone表示时区的概念,但它在Date里并无任何体现,只能使用在格式化器上,这种设计着实让我再一次看不懂了。 罪状六:线程不安全的格式化器 关于Date的格式化,站在架构设计的角度来看,首先不得不吐槽的是Date明明属于java.util包,那么它的格式化器DateFormat为毛却跑到java.text里去了呢?这种依赖管理的什么...
importjava.time.Instant;importjava.time.ZoneId;importjava.time.format.DateTimeFormatter;publicclassFormatTimeWithZone{publicstaticvoidmain(String[]args){ZoneIdzoneId=ZoneId.of("Asia/Shanghai");// 指定新的时区Instantnow=Instant.now();// 获取当前时间的瞬时点DateTimeFormatterformatter=DateTimeFormatter.ofPa...
timestampLocalDateTime datetimeLocalDateTime 时间与日期基础概念 标准时间 GMT 即「格林威治标准时间」( Greenwich Mean Time,简称 G.M.T. ),指位于英国伦敦郊区的皇家格林威治天文台的标准时间,因为本初子午线被定义为通过那里的经线。然而由于地球的不规则自转,导致 GMT 时间有误差,因此目前已不被当作标准时间使用。
1 public void ZoneOffset(){ 2 LocalDateTime datetime = LocalDateTime.of(2018, Month.FEBRUARY, 14, 19, 30); 3 ZoneOffset offset = ZoneOffset.of("+05:30"); 4 OffsetDateTime date = OffsetDateTime.of(datetime, offset); 5 System.out.println("Date and Time with timezone offset in Java : " ...