zoukankan      html  css  js  c++  java
  • Java中的夏令时问题

    因为在用C#做项目的时候被夏令时坑过一回,所以这次将在java中的时区转换信息做一下记录,很简单

    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    inputFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    outputFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
    
    
    String datetime = "2016-03-20 12:00:00";
    
    System.out.println("In china :" + inputFormat.format(inputFormat.parse(datetime)));
    System.out.println("In New York :" + outputFormat.format(inputFormat.parse(datetime)));
    
    datetime = "2016-02-20 12:00:00";
    
    System.out.println("In china :" + inputFormat.format(inputFormat.parse(datetime)));
    System.out.println("In New York :" + outputFormat.format(inputFormat.parse(datetime)));
    
    
    System.out.println(TimeZone.getDefault().getID());
    

    经过两次输出可以看到,进入夏令时的3月20日与未进入夏令时的2月20日,时差分别是12和13小时,所以直接用TimeZone是一个很好的做法。代码的最后一句可以获取当地环境的时区id

  • 相关阅读:
    【npm】mac下node环境搭建
    pair求解迷宫的最短路径(bfs)
    dos窗口启动关闭Mysql
    二维差分模板
    一维差分模板
    DOS命令
    迷宫搜索dfs实现
    DFS 迷宫问题
    BFS广搜解决迷宫问题(跟着B站大佬手撸)
    蓝桥杯省赛模拟赛
  • 原文地址:https://www.cnblogs.com/mamuluke/p/5302732.html
Copyright © 2011-2022 走看看