这几天开发中遇到时间转换出错的问题,特总结如下:
 
==============================================================================
String 到Date转换
==============================================================================
 
 
 在使用“SimpleDateFormat ”进行带星期的日期字符串函数转换时需要指定时区,如果不指定时区将会抛出异常
 
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
 
 
String da1 = "Tue 02-Apr-2013 19:00";
SimpleDateFormat sdf_wrong = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm");
Date d1 = sdf_wrong.parse(da1);//抛出异常;
 
SimpleDateFormat sdf_right = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm",Locale.ENGLISH);
Date d1 = sdf_right.parse(da1);//正确;
 
函数参数返回值说明:
public SimpleDateFormat(String pattern,
Locale locale)
Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the given locale. Note: This constructor may not support all locales. For full coverage, use the factory methods in the DateFormat class.
Parameters:
pattern - the pattern describing the date and time format
locale - the locale whose date format symbols should be used
Throws:
NullPointerException - if the given pattern or locale is null
IllegalArgumentException - if the given pattern is invalid
函数内部实现:
    /**
     * Constructs a <code>SimpleDateFormat</code> using the given pattern and
     * the default date format symbols for the default
     * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
     * <b>Note:</b> This constructor may not support all locales.
     * For full coverage, use the factory methods in the {@link DateFormat}
     * class.
     * <p>This is equivalent to calling
     * {@link #SimpleDateFormat(String, Locale)
     *     SimpleDateFormat(pattern, Locale.getDefault(Locale.Category.FORMAT))}.
     *
     * @see java.util.Locale#getDefault(java.util.Locale.Category)
     * @see java.util.Locale.Category#FORMAT
     * @param pattern the pattern describing the date and time format
     * @exception NullPointerException if the given pattern is null
     * @exception IllegalArgumentException if the given pattern is invalid
     */
    public SimpleDateFormat(String pattern)
    {
        this(pattern, Locale.getDefault(Locale.Category.FORMAT));
    }
 
    /**
     * Constructs a <code>SimpleDateFormat</code> using the given pattern and
     * the default date format symbols for the given locale.
     * <b>Note:</b> This constructor may not support all locales.
     * For full coverage, use the factory methods in the {@link DateFormat}
     * class.
     *
     * @param pattern the pattern describing the date and time format
     * @param locale the locale whose date format symbols should be used
     * @exception NullPointerException if the given pattern or locale is null
     * @exception IllegalArgumentException if the given pattern is invalid
     */
    public SimpleDateFormat(String pattern, Locale locale)
    {
        if (pattern == null || locale == null) {
            throw new NullPointerException();
        }
 
        initializeCalendar(locale);
        this.pattern = pattern;
        this.formatData = DateFormatSymbols.getInstanceRef(locale);
        this.locale = locale;
        initialize(locale);
    }
 
 
======================================================================================
Date转换为String
======================================================================================
1.现象
startTime = DateUtil.formatDate(child.getStartTime(), "yyyy-MM-dd");  
 
 
传入时间为2015-09-19 00:00:00   而  startTime结果为  2015-09-18
 
 
2.原因分析
 
DateUtil.formatDate默认设置的时区为GMT ,而本地默认时区为CST UT+8:00  这样导致两者相差 8小时(GMT比CST慢8个小时 ) 
 
所以通过该函数转换出的时间看到会相差一天。
 
3.解决方案
 
String startTime = SimpleDateFormat.getDateInstance().format(date);
 
或者
 
String startTime4 = DateFormat.getDateInstance().format(date);
 
避免时区转换问题
 
 
PS: GMT和CST区别
 
GMT 
Greenwich Mean Time 格林尼治标准时间

很多unix下用date命令都能看到当前的时区。很多unix下中国时区都是用CST表示的。但是这个表示方法非常不合理。因为CST同时代表了下面4个时区。

CST Central Standard Time (USA) UT-6:00
CST Central Standard Time (Australia) UT+9:30
CST China Standard Time UT+8:00
CST Cuba Standard Time UT-4:00

 
 
参考示例:一
 
=====================================================================================================
 
 

CET(欧洲中部时间)

欧洲中部时间(英語:Central European Time,CET)是比世界标准时间(UTC)早一个小时的时区名称之一。它被大部分欧洲国家和部分北非国家采用。

冬季时间为UTC+1,夏季欧洲夏令时为UTC+2。

WET(欧洲西部时间)

欧洲西部时间(Western European Time,缩写WET)和世界标准时间(UTC)相同。

EET(欧洲东部时间)

欧洲东部时间(Eastern European Time,缩写EET)是比世界标准时间(UTC)早二个小时的时区名称之一。它被部分欧洲国家、北非国家和中东国家采用。

UTC(世界标准时间)

协调世界时,又称世界标准时间或世界協調時間,简称UTC(从英文「Coordinated Universal Time」/法文「Temps Universel Cordonné」而来),是最主要的世界時間標準,其以原子时秒长为基础,在时刻上尽量接近于格林尼治平時。

GMT(格林尼治平时)

格林尼治平时(又称格林尼治平均时间或格林尼治标准时间,旧译格林威治标准时间;英语:Greenwich Mean Time,GMT)是指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。

自1924年2月5日开始,格林尼治天文台每隔一小时会向全世界发放调时信息。

理论上来说,格林尼治标准时间的正午是指当太阳横穿格林尼治子午线时(也就是在格林尼治上空最高点时)的时间。由于地球在它的椭圆轨道里的运动速度不均匀,这个时刻可能与实际的太阳时有误差,最大误差达16分钟。

由于地球每天的自转是有些不规则的,而且正在缓慢减速,因此格林尼治时间已经不再被作为标准时间使用。现在的标准时间,是由原子钟报时的协调世界时(UTC)

CST(北京时间)

北京时间,China Standard Time,中国标准时间。在时区划分上,属东八区,比协调世界时早8小时,记为UTC+8。

不过这个CST这个缩写比较纠结的是它可以同时代表四个不同的时间

  • Central Standard Time (USA) UT-6:00
  • Central Standard Time (Australia) UT+9:30
  • China Standard Time UT+8:00
  • Cuba Standard Time UT-4:00

因此你平时编写程序过程中也有可能遇到javascript客户端时间和服务端时间不一致的问题,编程的时候还是要多注意。

ISO 日期和时间的表示方法

国际标准化组织的国际标准ISO 8601是日期和时间的表示方法,全称为《数据存储和交换形式·信息交换·日期和时间的表示方法》。目前是第三版ISO8601:2004以替代第一版ISO8601:1988與第二版ISO8601:2000。

这是仅仅就只是一个日期时间的表示方法,我们用这样的方法表示一个UTC时间。

=============================================================================================

参考示例:二

CST和GMT时间的区别

问题描述 
今天遇到一个奇怪的问题,在服务器端通过 Java 获取当前时间为 Fri Aug 28 09:37:46 CST 2009, 转化为GMT时间为:28 Aug 2009 01:37:46 GMT,也就是说GMT时间加上 8 个小时等于CST表示的时间, 那这个CST不就是北京时间么,因为我们是在东八区的。

一切看起来很正常,不过在客户端用JavaScript解析这个时间就有问题了:

1
2
// Fri Aug 28 2009 23:37:46 GMT+0800
new Date('Fri Aug 28 09:37:46 CST 2009').toString();

好奇怪,这次GMT和CST表示的时间居然相差整整 14 个小时?

百度一下

找到这篇文章,问题已经很明了。

GMT(Greenwich Mean Time)代表格林尼治标准时间,这个大家都知道。 
而CST却同时可以代表如下 4 个不同的时区:

  • Central Standard Time (USA) UT-6:00
  • Central Standard Time (Australia) UT+9:30
  • China Standard Time UT+8:00
  • Cuba Standard Time UT-4:00

可见,CST可以同时表示美国,澳大利亚,中国,古巴四个国家的标准时间。

前面提到的通过 Java 获取的CST时间用的是China Standard Time,而客户端JavaScript则默认采用的是美国的中部时间。

所以将 Fri Aug 28 09:37:46 CST 2009 加上 6 个小时,再加上 8 个小时,就等于 Fri Aug 28 2009 23:37:46 GMT+0800

可见,在以后的编程中为了避免错误,还是不要使用CST时间,而尽量采用GMT时间。

 
 
问题:
1.SimpleDateFormat 中YYYY 导致的week year问题
 
代码:
 
       String time  = "2016-04-12";
// String time2 = "2015-12-31";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat df2 = new SimpleDateFormat("YYYY-MM-dd"); try {
System.out.println("The date yyyy:"+df.parse(time).toString());
System.out.println("The date YYYY:"+df2.parse(time).toString());
} catch (ParseException e) {
e.printStackTrace();
}

OutPut:

 
The date yyyy:Tue Apr 12 00:00:00 CST 2016
The date YYYY:Sun Dec 27 00:00:00 CST 2015
 
 
参考资料:
 
 

Date and Time Patterns

Date and time formats are specified by date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved):

Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 199696
Y Week year Year 200909
M Month in year Month JulyJul07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text TuesdayTue
u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard TimePSTGMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08-0800-08:00

Java 时间转换问题总结的更多相关文章

  1. inner join ,left join ,right join 以及java时间转换

    1.inner join ,left join 与 right join (from 百度知道) 例表aaid adate1    a12    a23    a3表bbid  bdate1     ...

  2. JAVA 时间转换、获取

    /** * 将字符串格式的时间转换成Timestamp * * @param time * @param formatStyle * @return */ public static Timestam ...

  3. Java时间转换类实现

    Java时间类型非常的差,首先版本问题,本人使用java在Android平台开发.很多Data类的方法已经不提倡使用,一个时间,居然要使用Calendar.DateFormat等类共同编码,非常麻烦. ...

  4. Java时间转换

    package com.fh.util; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseE ...

  5. Java时间转换的一个特性

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date codedat ...

  6. java 时间转换

    public static int timestrtosec(String time) {        if (Strings.isNullOrEmpty(time)) {            r ...

  7. java 时间转换去杠

    public static String minusHyphen(String dateParam){ if(dateParam ==null) return null; if(dateParam.i ...

  8. java时间类型的转换/获取当前时间/将时间转换成String/将String转换成时间

    对于我的脑子,我已经服气了...写了N遍的东西,就是记不住...既然记不住那就记下来... 利用java获取当前的时间(String类型,年-月-日 时:分:秒) //我要获取当前的日期 Date d ...

  9. Java时间日期格式转换 转自:http://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html

    Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...

随机推荐

  1. Nhibernate 一对一关系映射(主键映射)

    参考:点击这里 妈的,搞了一天了,终于可以了,现在总结下,以防下次再出现这样痛苦的问题了,有两个表:user(用户)和Blog(设置表),它们之间的关系正如我所说的是一对一的关系.现在我们来映射这两个 ...

  2. 1028: [JSOI2007]麻将 - BZOJ

    Description 麻将是中国传统的娱乐工具之一.麻将牌的牌可以分为字牌(共有东.南.西.北.中.发.白七种)和序数牌(分为条子.饼子.万子三种花色,每种花色各有一到九的九种牌),每种牌各四张.在 ...

  3. js实现网页图片延时加载的原理和代码 提高网站打开速度

    有时我们看到一些大型网站,页面如果有很多图片的时候,当你滚动到相应的行时,当前行的图片才即时加载的,这样子的话页面在打开只加可视区域的图片,而其它隐藏的图片则不加载,一定程序上加快了页面加载的速度,对 ...

  4. javascript中跨源资源共享

    来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(十) 通过XHR实现Ajax通信的一个主要限制,来源于跨域安全策略.默认情况下,XHR对 ...

  5. [转载]AOP面向方面编程

    1.引言 软件开发的目标是要对世界的部分元素或者信息流建立模型,实现软件系统的工程需要将系统分解成可以创建和管理的模块.于是出现了以系统模块化特性的面向对象程序设计技术.模块化的面向对象编程极度极地提 ...

  6. C#跳出循环的几种方法的区别

    break是循环结束执行,执行循环体后面的代码. continue是跳过本次循环未执行的代码,继续执行下一次循环. goto是跳到指定的指令去,你指哪,他跳到哪. return是函数返回,如果循环在M ...

  7. 以守护进程方式启动firefly

    原地址:http://www.9miao.com/question-15-53966.html 最近看源码,查了半天,没找到已守护进程方式启动firefly的方法,自己改了改写了一个,废话不多说直接上 ...

  8. 定长内存池之BOOST::pool

    内存池可有效降低动态申请内存的次数,减少与内核态的交互,提升系统性能,减少内存碎片,增加内存空间使用率,避免内存泄漏的可能性,这么多的优点,没有理由不在系统中使用该技术. 内存池分类: 1.      ...

  9. POJ3258River Hopscotch(二分)

    http://poj.org/problem?id=3258 题意:有一条很长很直的河距离为L,里边有n块石头,不包括起点和终点的那两块石头,奶牛们会从一个石头跳到另外一个,但因为有的石头隔得太近了, ...

  10. Linux 阿里云挂载新分区

    阿里云服务器可以自己购买数据盘并挂载使用,虽然官方也提供了挂载的教程,但是还是有些朋友不清楚其中的细节,为此,我在这里来给大家分享一下详细的挂载办法. 工具/原料 已经购买开通阿里云服务器,并且在开通 ...