• java 获取格林威治时间(GMT) 时区 0时区 东八区【全网最详细】


    省流:

    1. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    2. dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    3. String GMT_Time = dateFormat.format(new Date());

    正文:

    背景

    系统联调的时候,我们有时候会说,时间需要使用格林威治时间,即GMT时间。

    如何取格林威治时间呢?

    代码

    这是以前的方法,但已过时,不推荐使用。 

    1. String time = new Date().toGMTString();
    2. //打印:10 Nov 2023 03:14:19 GMT

    推荐使用如下方法:

    1. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    2. dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    3. String GMT_Time = dateFormat.format(new Date());
    4. //打印: 现在:2023-11-10 11:14:19 格林威治时间为:2023-11-10 03:14:19

    注:以上方法,无论你在哪个环境(CST时间,GMT时间),取的都是GMT时间。

    解读 

    1. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    2. String now_time = dateFormat.format(new Date());

    我们常用的是上面这段代码,拿到当前时间。要获取GMT时间,就给 SimpleDateFormat 设置时区即可。

    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    我们都知道,new Date()拿到的是计算机系统所在时区的时间。例如我们在国内,就是使用东八区时间,即 GMT+8。

    补充:

    GMT时间,即0时区的时间,本初子午线上的时间。

    GMT+8 的意思是:GMT时间+8小时

    北京时间 = GMT时间+8小时

    ==========================分割线===========================

    文章到此已经结束,以下是紫薯布丁

    String time = new Date().toGMTString();

    //打印:10 Nov 2023 02:16:16 GMT

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            String GMT_Time = dateFormat.format(new Date());

    //打印:    现在:2023-11-10 11:14:19  格林威治时间为:2023-11-10 03:14:19
     

    GMT时间,即0时区的时间,本初子午线上的时间。

    GMT+8 的意思是:GMT时间+8小时

    北京时间 = GMT时间+8小时

    前端页面将时间显示成数字:

    "createTime": 1698706491111

    是因为没有将Date类型转换成字符串,直接将Date类型的数据传给前端了。

    要么在出参字段上加上以下注解

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

    要么使用SimpleDateFormat做转换成
    new SimpleDateFormat("yyyy-MM-dd").format(date);

            //设置为0
            Calendar calendar = Calendar.getInstance();
            // 时
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            // 分
            calendar.set(Calendar.MINUTE, 0);
            // 秒
            calendar.set(Calendar.SECOND, 0);
            // 毫秒
            calendar.set(Calendar.MILLISECOND, 0);
            
            //获取设置完之后的时间
            Date date = calendar.getTime();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = dateFormat.format(date);

    //核心思想就是:将Date类型转换成long值,相减,得到差值,再除以一天的毫秒值。
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date startTime = dateFormat.parse("2023-11-09 22:37:01.123");
        Date endTime =dateFormat.parse("2023-11-10 22:37:01.123");
        long starTime = startTime.getTime();
        long endTime = endTime.getTime();
        long diff = endTime-starTime;
        long day = diff/24/60/60/1000;//差值除以一天的毫秒数
        System.out.println("相差天数为:"+ day);
        //如果要保留小数
        double d = (double)diff/24/60/60/1000;

  • 相关阅读:
    clickhouse的Distributed分布式表查询的性能陷阱
    WebGL 选中物体
    C++封装-类和对象
    6.DesignForPlacement\ExportHighlightedList
    2023年浙江工业大学MPA提前批招生通知
    深度清洁:使用npm prune命令优化你的Node.js项目
    p19.matplotlib:Animation 动画
    【Qt常用关键字知识拓展】
    mysql面试题52:MySQL中你是否做过主从一致性校验,如果有,怎么做的,如果没有,你打算怎么做?
    Python 创建或读取 Excel 文件
  • 原文地址:https://blog.csdn.net/u011149152/article/details/134326800