找软件
软件工具大全

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

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

LetterDate or Time ComponentPresentationExamples
GEra designatorTextAD
yYearYear1996; 96
YWeek yearYear2009; 09
M/LMonth in yearMonthJuly; Jul; 07
w(小写)Week in yearNumber27
WWeek in monthNumber2
DDay in yearNumber189
dDay in monthNumber10
FDay of week in monthNumber2
EDay in weekTextTuesday; Tue
u(小写)Day number of weekNumber1
aAm/pm markerTextPM
HHour in day (0-23)Number0
k(小写)Hour in day (1-24)Number24
K(大写)Hour in am/pm (0-11)Number0
hHour in am/pm (1-12)Number12
mMinute in hourNumber30
s(小写)Second in minuteNumber55
S(大写)MillisecondNumber978
z(小写)Time zoneGeneral time zonePacific Standard Time; PST; GMT-08:00
Z(大写)Time zoneRFC 822 time zone-0800
X(大写)Time zoneISO 8601 time zone-08; -0800; -08:00

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

字符串表达式
2016-04-10T08:38:04.000Zyyyy-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)
未经允许禁止转载:优米格 » 将日期字符串(包含T,Z)转化为Date类型

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系