DateFormatter没有以给定格式打印值 [英] DateFormatter is not printing value in the given format

查看:92
本文介绍了DateFormatter没有以给定格式打印值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期时间格式字符串是: yyyy-MM-dd'T'HH:mm:ss.SSSZ
我正在使用Joda Time的 DateTimeFormatter 以上述格式打印日期.

My date-time format string is : yyyy-MM-dd'T'HH:mm:ss.SSSZ
I am using DateTimeFormatter from Joda Time to print my date in the above mentioned format.

现在,将日期视为

2016/04/01 23:00:00

2016/04/01 23:00:00

那么它应该已经打印了

2016-04-01T23:00:00.000Z

2016-04-01T23:00:00.000Z

但是,它会打印

2016-04-01T23:00:00.000 + 0200

2016-04-01T23:00:00.000+0200

请帮助我获取以字符串格式指定的相同格式打印日期.

Please help me in getting the date printed in the same format as specified in the string format.

推荐答案

根据

According https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html Z has special meaning:

Z       zone-offset

如果要转义Z,请在Z处加上':

If you want to escape Z quote Z with ':

yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

例如:

java.time.LocalDateTime date = java.time.LocalDateTime.now();
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
                .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
java.lang.String text = date.format(formatter);
System.out.println(text);

打印

2016-06-11T18:39:41.962Z

这篇关于DateFormatter没有以给定格式打印值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆