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

  • 相关阅读:
    opensuse字符和图形界面
    Eclipse编辑器小手段
    切换运行时用户以及用户组
    PHP安装和配置
    Linux程序资源限制简述
    test2234343
    找回Svn和Git不见的文件图标
    SourceInsight使用技巧
    Javascript数组使用方法
    MySQL安装和配置
  • 原文地址:https://www.cnblogs.com/mamuluke/p/5302732.html
Copyright © 2011-2022 走看看