buguge - Keep it simple,stupid

知识就是力量,但更重要的,是运用知识的能力why buguge?

导航

时区/时间戳

时区

指地球上的一块区域使用的同一时间定义,以经度划分,每个时区横跨15经度,总共24个时区,东西各12个时区。

 

格林威治时间(GMT)

格林威治时间(Greenwich Mean Time,缩写为GMT)是以英国伦敦当地时间的标准,用作全球标准时间的参考基准。

上述说的24个时区,分别以 GMT-12 到 GMT+12 来表示。 如果北京时间可表示为 GMT+8。

 

java中new Date()不区分时区

如下代码

Date time = new Date();// calendar.getTime();
System.out.println(time+"\t"+time.getTime()+"\t"+TimeZone.getDefault()); // 系统默认时区是 Asia/Shanghai,即 GMT+8东八区北京时间
TimeZone.setDefault(TimeZone.getTimeZone("GMT+7")); // 修改当前时区 为 GMT+7
System.out.println(time+"\t"+time.getTime()+"\t"+TimeZone.getDefault());

运行结果:

Fri Mar 22 10:30:44 CST 2024 1711074644748 sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=31,lastRule=null]
Fri Mar 22 09:30:44 GMT+07:00 2024 1711074644748 sun.util.calendar.ZoneInfo[id="GMT+07:00",offset=25200000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

 

在Java中,new Date() 创建的是一个代表某个时间点的日期对象,它不直接关联时区信息。实际上,Date 类本身不储存时区信息,它只是以UTC(世界协调时间)来表示时间。当使用new Date()时,它会基于当前系统时区给出当前时间。

在使用System.out.println打印Date时,会调用Date#toString方法,该方法会获取系统的默认时区来进行时间的转换。

// ↓ java.util.Date.java

public String toString() {
    // "EEE MMM dd HH:mm:ss zzz yyyy";
    BaseCalendar.Date date = normalize();
    StringBuilder sb = new StringBuilder(28);
    int index = date.getDayOfWeek();
    …
}

private final BaseCalendar.Date normalize() {
    if (cdate == null) {
        BaseCalendar cal = getCalendarSystem(fastTime);
        cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,
                                                        TimeZone.getDefaultRef());
        return cdate;
    }

    …
}


// ↓ java.util.TimeZone.java

static TimeZone getDefaultRef() {
    TimeZone defaultZone = defaultTimeZone;
    if (defaultZone == null) {
        // Need to initialize the default time zone.
        defaultZone = setDefaultZone();
        assert defaultZone != null;
    }
    // Don't clone here.
    return defaultZone;
}
View Code

 

如果需要处理涉及时区的日期和时间,可以使用java.time包下的类,比如ZonedDateTimeInstant 和 LocalDateTime

例如,使用ZonedDateTime来创建一个带时区的日期时间对象:

import java.time.ZonedDateTime;
import java.time.ZoneId;
 
public class TimeZoneExample {
    public static void main(String[] args) {
        // 获取当前日期时间,并指定时区
        ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println("Zoned DateTime in Shanghai: " + zonedDateTime);
 
        // 转换为其他时区
        ZonedDateTime zonedDateTimeInNewYork = zonedDateTime.withZoneSameInstant(ZoneId.of("America/New_York"));
        System.out.println("Zoned DateTime in New York: " + zonedDateTimeInNewYork);
    }
}

 

 

时间戳

指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数,注意“现在”这个词,如果是格林威治时间 ,现在指的是格林威治当前的时间;如果是北京时间,现在指的是北京当前的时间。

时间戳和时区有没有关系

二者没有关系。时间戳在哪个时区都是一样的,我们可以通过时间戳和时区去计算当前时区的时间。

 

 

java中如何显示不同时区的时间 https://m.elecfans.com/article/836296.html

https://www.jianshu.com/p/b9a7739ff569

posted on 2024-03-22 13:42  buguge  阅读(22)  评论(0编辑  收藏  举报