用Java转换日期时间格式 [英] Converting Date time format in Java

查看:92
本文介绍了用Java转换日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一行C#代码,我正在尝试用Java复制。代码如下。

I have a line of C# code that I am trying to replicate in Java. The code looks as follows.

n.InnerText = DateTime.Parse(n.InnerText).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");

目的是将xml上下文中已有的DateTime替换为代表通用时间的DateTime。

The intent is to replace the DateTime already in the xml context with one representing universal time.

我尝试使用

node.setTextContent = Date.parse(node.getTextContent())

但由于Date.parse( )已弃用。我通读了Eclipse中的注释,并按建议尝试使用DateFormat,但DateFormat没有解析方法。

but I am unable to continue due to the Date.parse() being deprecated. I read through the note in Eclipse and tried DateFormat as suggested but DateFormat does not have a parse method.

有人可以建议不使用任何第三方库的问题解决方案吗?

Can someone suggest a solution to my problem that does not use any third party libraries?

推荐答案

您可以使用:

DateFormat format = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ssZ");
Date date = format.parse(myString);

请务必检查语言环境是否合适,并检查字段是否相同(如我不知道您打算解析什么,我只是复制了它们。)

Be sure to check the locale if it's appropriate, and to check that the fields are the same (as I don't know what you intended to parse, I just copied them).

(例如, T不存在。)

(For example "T" does not exist.)

这篇关于用Java转换日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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