Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jersey已经将我的mysql时间戳写为2011-09-28221:48:25Z我如何用Java格式化它?_Java_Mysql_Xml_Date_Jersey - Fatal编程技术网

Jersey已经将我的mysql时间戳写为2011-09-28221:48:25Z我如何用Java格式化它?

Jersey已经将我的mysql时间戳写为2011-09-28221:48:25Z我如何用Java格式化它?,java,mysql,xml,date,jersey,Java,Mysql,Xml,Date,Jersey,我有一个mysql后端,它有一个timestamp字段,可以像这样自动设置为currenttimestamp(date\u timetimestamp NOT NULL默认的CURRENT\u timestamp)。将值读取为Date后,发送一个xml,其中时间戳显示为 <timestamp>2011-09-28T21:48:25Z</timestamp> 2011-09-28221:48:25Z 请不要过分渲染背后的故事:我不能改变我从泽西岛得到的东西。现在我的问题

我有一个mysql后端,它有一个timestamp字段,可以像这样自动设置为currenttimestamp(
date\u time
timestamp NOT NULL默认的CURRENT\u timestamp)。将值读取为Date后,发送一个xml,其中时间戳显示为

<timestamp>2011-09-28T21:48:25Z</timestamp>
2011-09-28221:48:25Z
请不要过分渲染背后的故事:我不能改变我从泽西岛得到的东西。现在我的问题是:如何将xml中的2011-09-28T21:48:25Z解析为Java能够理解的日期

谢谢。

首先

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") ;
df.setTimeZone(TimeZone.getTimeZone("Zulu")) ;
Date dd = df.parse("2011-09-28T21:48:25Z") ;
…然后,可以选择

Timestamp ts = new Timestamp(dd.getTime()) ;
tl;博士 ISO 8601 您的输入字符串是标准格式的

与此无关,只是它的设计者明智地选择使用ISO8601格式将日期时间值序列化为字符串

java.time java中内置的java.time类在解析/生成字符串时默认使用ISO 8601格式。因此,无需指定格式化模式

UTC 末尾的
Z
Zulu
的缩写,表示GMT

Instant
该类表示时间线上的一个时刻,分辨率为

时区 将
Instant
调整到您想要的任何时区

指定一个。切勿使用3-4个字母的缩写,如
EST
IST
,因为它们不是真正的时区,也不是标准化的,甚至不是唯一的(!)

串 不要将日期时间值与表示此类值的字符串合并

您可以要求java.time对象生成任意格式的字符串。但通常最好让
DateTimeFormatter
为您自动本地化

Locale l = Locale.CANADA_FRENCH;  // Or Locale.US, Locale.ITALY, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( l );
String output = zdt.format( f );
Instant instant = Instant.parse( "2011-09-28T21:48:25Z" );
ZoneId z = ZoneId.of( "America/Montreal" );  // Or "Asia/Kolkata" etc.
ZonedDateTime zdt = instant.atZone( z );
Locale l = Locale.CANADA_FRENCH;  // Or Locale.US, Locale.ITALY, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( l );
String output = zdt.format( f );