[英]Date in a format
I have this code 我有这个代码
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'-'hh:mm")
DateTime dateTime1 = new DateTime()
println("dateTime1 : " + dateTime1)
DateTime formattedDate = fmt.parseDateTime(dateTime1.toString());
println("formattedDate : " + formattedDate)
DateTimeFormatter finalFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm'-'hh:mm");
System.out.println("formatted date : " + finalFormat.print(formattedDate));
It prints out something like this : 它打印出这样的内容:
dateTime1 : 2014-08-20T15:34:17.256-04:00
formattedDate : 2014-08-20T16:00:17.256-04:00
formatted date : 2014-08-20T16:00-04:00
I want a date in exactly this format(since those are the requirements) 我想要的日期正是这种格式(因为这是要求)
2014-08-20T15:34-04:00
The problem with 问题所在
formattedDate : 2014-08-20T16:00:17.256-04:00
formatted date : 2014-08-20T16:00-04:00
is for time difference it always prints out '16:00-04:00' 因为时差,它总是打印出“ 16:00-04:00”
How do I get that ? 我怎么得到的? The date is retrieved from the database which will be converted in to DateTime object.
从数据库中检索日期,该日期将转换为DateTime对象。
When I try this : 当我尝试这个:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
DateTime dateTime1 = new DateTime()
println("dateTime1 : " + dateTime1)
DateTime formattedDate = fmt.parseDateTime(dateTime1.toString());
println("formattedDate : " + formattedDate)
DateTimeFormatter finalFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm'-'hh:mm");
System.out.println("formatted date : " + finalFormat.print(formattedDate));
the output changes : 输出更改:
dateTime1 : 2014-08-20T15:59:34.876-04:00
formattedDate : 2014-08-20T15:59:34.876-04:00
formatted date : 2014-08-20T15:59-03:59
It should be -04.00. 它应该是-04.00。 But comes back as '-03:59', which is buggy.
但是返回为“ -03:59”,这是越野车。
Any help is highly appreciated. 非常感谢您的帮助。
Thanks 谢谢
The problem is in your pattern 问题出在你的模式上
"yyyy-MM-dd'T'HH:mm'-'hh:mm"
When you add 'hh:mm' you are reading the time again as 04:00, however since it has read that it is a PM time already, this becomes 16:00. 当您添加“ hh:mm”时,您将再次将时间读取为04:00,但是由于已读取到已经是PM时间,因此该时间变为16:00。 You should read a timezone as a timezeone with a
Z
, not as part of the time itself. 您应该将时区读为带
Z
的时区,而不是时间本身的一部分。
From the Javadoc for DateTimeFormat 从Javadoc中获取DateTimeFormat
Symbol Meaning Presentation Examples
------ ------- ------------ -------
Z time zone offset/id zone -0800; -08:00; America/Los_Angeles
and 和
Zone: 'Z' outputs offset without a colon, 'ZZ' outputs the offset with a colon, 'ZZZ' or more outputs the zone id.
I suggest you use 我建议你用
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
Note: the -
at the end is not a separator, but a minus sign. 注意:
-
末尾的不是分隔符,而是减号。 I am guessing the -04:00
timezone is the AEST
timezone. 我猜
-04:00
时区是AEST
时区。
Your time format is incorrect, more specifically the last part makes it parse the timezone as the time of day (hours and minutes). 您的时间格式不正确,更具体地说,最后一部分使其将时区解析为一天中的时间(小时和分钟)。 Try using this instead:
尝试使用此代替:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
Your offset format is incorrect ZZ
is timezone with colon, and you need to add withOffsetParsed()
- 您的偏移量格式不正确
ZZ
是带有冒号的时区,您需要添加withOffsetParsed()
-
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mmZZ");
String dateTime1 = "2014-08-20T15:34-04:00";
DateTime formattedDate = fmt.withOffsetParsed().parseDateTime(dateTime1);
System.out.println("formattedDate : " + formattedDate);
System.out.println("formatted date : " + fmt.print(formattedDate));
Output is 输出是
formattedDate : 2014-08-20T15:34:00.000-04:00
formatted date : 2014-08-20T15:34-04:00
From the DateTimeFormat
javadoc , 从
DateTimeFormat
javadoc中 ,
Zone: 'Z' outputs offset without a colon, 'ZZ' outputs the offset with a colon, 'ZZZ' or more outputs the zone id.
区域:“ Z”输出不带冒号的偏移量,“ ZZ”输出带冒号的偏移量,“ ZZZ”或更大的输出区域ID。
The other answers are correct. 其他答案是正确的。
But you could more simply use a built-in formatter to produce a format of yyyy-MM-dd'T'HH:mm
. 但是,您可以更简单地使用内置格式化程序来生成
yyyy-MM-dd'T'HH:mm
格式。
DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinute().withZone( DateTimeZone.UTC ) ;
String output = formatter.print( DateTime.now() ) + "Z";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.