我们如何在 Java 中将 yyyy-MM-dd-HH.mm.ss.SSSSSS 转换为 yyyy-MM-dd'T'HH:mm:ss.SSSz?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34865522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How we can convert yyyy-MM-dd-HH.mm.ss.SSSSSS to yyyy-MM-dd'T'HH:mm:ss.SSSz in Java?
提问by learner
Can anybody let me know,how we can convert date in yyyy-MM-dd-HH.mm.ss.SSSSSS
format to date in yyyy-MM-dd'T'HH:mm:ss.SSSz
in Java,where in both input and output dates should be Strings.
任何人都可以让我知道,我们如何将yyyy-MM-dd-HH.mm.ss.SSSSSS
格式中的日期转换为yyyy-MM-dd'T'HH:mm:ss.SSSz
Java 中的日期 ,其中输入和输出日期都应该是字符串。
I have used apache DateFormatUtils
but that does not give milliseconds in the output.
我使用过 apache,DateFormatUtils
但它没有在输出中给出毫秒数。
采纳答案by MadProgrammer
Java 8+
Java 8+
You could also use the newer Time API in Java 8, something like...
您还可以在 Java 8 中使用较新的Time API,例如...
String formatIn = "yyyy-MM-dd-HH.mm.ss.SSSSSS";
String formatOut = "yyyy-MM-dd'T'HH:mm:ss.SSSz";
String valueIn = "2016-01-19-09.55.00.000000";
LocalDateTime ldt = LocalDateTime.parse(valueIn, DateTimeFormatter.ofPattern(formatIn));
System.out.println("< " + ldt);
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
String out = DateTimeFormatter.ofPattern(formatOut).format(zdt);
System.out.println("> " + out);
Which outputs...
哪个输出...
< 2016-01-19T09:55
> 2016-01-19T09:55:00.000AEDT
This makes you far more responsible for managing the time zones which might be a better solution generally
这使您更有责任管理时区,这通常可能是更好的解决方案
And because converting between time zones in the Java 8 API gives me a headache (lack of experience :P)
而且因为在 Java 8 API 中的时区之间转换让我很头疼(缺乏经验:P)
LocalDateTime ldt = LocalDateTime.parse(valueIn, DateTimeFormatter.ofPattern(formatIn));
System.out.println("< " + ldt);
ZonedDateTime here = ldt.atZone(ZoneId.systemDefault());
System.out.println("here " + here);
ZonedDateTime there = here.withZoneSameInstant(ZoneId.of("GMT"));
System.out.println("there " + there);
String out = DateTimeFormatter.ofPattern(formatOut).format(there);
System.out.println("> " + out);
Which outputs...
哪个输出...
< 2016-01-19T09:55
here 2016-01-19T09:55+11:00[Australia/Sydney]
there 2016-01-18T22:55Z[GMT]
> 2016-01-18T22:55:00.000GMT
FYI: I think your input is using nano/micro seconds and not milliseconds (there's only 1000 milliseconds in a second). SimpleDateFormat
does not support nano/micro seconds, but DateTimeFormatter
does, you'd have to use the n
pattern, yyyy-MM-dd-HH.mm.ss.nnnnnn
for example
仅供参考:我认为您的输入使用的是纳秒/微秒而不是毫秒(每秒只有 1000 毫秒)。 SimpleDateFormat
不支持纳/微秒,但是,例如DateTimeFormatter
,您必须使用该n
模式yyyy-MM-dd-HH.mm.ss.nnnnnn
Java 7 and below
Java 7 及以下
The basic answer is, use a SimpleDateFormat
....
基本答案是,使用SimpleDateFormat
......
String formatIn = "yyyy-MM-dd-HH.mm.ss.SSSSSS";
String formatOut = "yyyy-MM-dd'T'HH:mm:ss.SSSz";
String valueIn = "2016-01-19-09.55.00.000000";
SimpleDateFormat in = new SimpleDateFormat(formatIn);
SimpleDateFormat out = new SimpleDateFormat(formatOut);
Date dateIn = in.parse(valueIn);
System.out.println("< " + dateIn);
String valueOut = out.format(dateIn);
System.out.println("> " + valueOut);
Which outputs...
哪个输出...
< Tue Jan 19 09:55:00 AEDT 2016
> 2016-01-19T09:55:00.000AEDT
The problem here is, you could be converting across different time zones, which case, you could use something like...
这里的问题是,您可以跨不同时区进行转换,在这种情况下,您可以使用类似...
in.setTimeZone(TimeZone.getTimeZone("GMT"));
dateIn = in.parse(valueIn);
System.out.println("< " + dateIn);
out.setTimeZone(TimeZone.getTimeZone("GMT"));
valueOut = out.format(dateIn);
System.out.println("> " + valueOut);
which outputs
哪个输出
< Tue Jan 19 20:55:00 AEDT 2016
> 2016-01-19T09:55:00.000GMT
or a combination of, if you want to covert to a different time zone.
或两者的组合,如果您想转换到不同的时区。
But, personally, I'd use Joda-Time, but that's me
但是,就我个人而言,我会使用Joda-Time,但这就是我
回答by Meinkraft
SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS");
SimpleDateFormat s2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
Date d= s1.parse( "2015-11-11-02.01.11.000001" );
String str= s2.format( d);
System.out.println(str);
This parse the string date to a Date project using the SimpleDateFormat s1. Then it parse it to the desired form using a second SimpleDateFormat.
这使用 SimpleDateFormat s1 将字符串日期解析为日期项目。然后它使用第二个 SimpleDateFormat 将其解析为所需的形式。