我们如何将yyyy-MM-dd-HH.mm.ss.SSSSSS转换为yyyy-MM-dd'T'HH:mm:ss.SSSz在Java中? [英] How we can convert yyyy-MM-dd-HH.mm.ss.SSSSSS to yyyy-MM-dd'T'HH:mm:ss.SSSz in Java?

查看:2830
本文介绍了我们如何将yyyy-MM-dd-HH.mm.ss.SSSSSS转换为yyyy-MM-dd'T'HH:mm:ss.SSSz在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我们,我们如何将 yyyy-MM-dd-HH.mm.ss.SSSSSS 格式的日期转换为 yyyy-MM-dd'T'HH:mm:ss.SSSz 在Java中,输入和输出日期应该是字符串。

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.

我使用了apache DateFormatUtils ,但输出中没有给出毫秒数。

I have used apache DateFormatUtils but that does not give milliseconds in the output.

推荐答案

Java 8 +



你也可以使用更新的 Java 8中的时间API ,类似于......

Java 8+

You could also use the newer Time API in Java 8, something like...

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

因为Java 8 API中时区之间的转换让我很头疼(缺乏经验:P)

And because converting between time zones in the Java 8 API gives me a headache (lack of experience :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:我认为您的输入是使用nano / micro秒而不是毫秒(一秒钟内只有1000毫秒)。 SimpleDateFormat 不支持纳秒/微秒,但 DateTimeFormatter ,你必须使用 n pattern, yyyy-MM-dd-HH.mm.ss.nnnnnn 例如

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

基本答案是,使用 SimpleDateFormat ....

The basic answer is, use a 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);

输出

< 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.

但是,就个人而言,我会使用 Joda-Time ,但那是我

But, personally, I'd use Joda-Time, but that's me

这篇关于我们如何将yyyy-MM-dd-HH.mm.ss.SSSSSS转换为yyyy-MM-dd'T'HH:mm:ss.SSSz在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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