欢迎光临
我们一直在努力

将日期字符串(包含T,Z)转化为Date类型

在Java中,如何把日期字符串(包含T,Z)转化为Date类型呢?这就是这篇文章要说明的内容。首先需要明确的是,包含T,Z字符的日期字符串是和时区有关系的,具体日期字符串中包含的各字母的含义见下表:

Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M/L Month in year Month July; Jul; 07
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 in week Text Tuesday; Tue
u(小写) Day number of week 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 Time; PST; GMT-08:00
Z(大写) Time zone RFC 822 time zone -0800
X(大写) Time zone ISO 8601 time zone -08; -0800; -08:00

下面是一些输入的字符串类型,及应该对应的SimpleDateFormat表达式:

字符串 表达式
2016-04-10T08:38:04.000Z yyyy-MM-dd’T’HH:mm:ss.SSS’Z’

如下,是Java代码的一个演示示例:

public static void main(String[] args) throws  Exception{
        String dateString = "2016-04-10T08:38:04.000Z";
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.CHINA);
        format.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = format.parse(dateString);
        Long l = date.getTime();
        Date d1 = new Date(l+1000l);
        String s = format.format(d1);
        System.out.println(s);

}

如有错误,敬请更正!

参考文章如下(略有更改):

  1. http://stackoverflow.com/questions/4216745/java-string-to-date-conversion;
  2. https://segmentfault.com/a/1190000000646877;
赞(0)
未经允许不得转载:Ddmit » 将日期字符串(包含T,Z)转化为Date类型

评论 抢沙发

登录

找回密码

注册