JME JME - 7 months ago 20
Java Question

Converting Date time format in 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");


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

I have attempted to use

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


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?

Answer

You can use:

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

(For example "T" does not exist.)

Comments