Quantcast
Channel: Web and Android Development Tips
Viewing all articles
Browse latest Browse all 23

Parsing a XML Datetime in Android (Java)

$
0
0
The Java Date class is a complete mystery to me. Why does it have to be so complicated? 

I needed to parse an XML DateTime string (like this: "2011-02-10T13:40:26.33") in Java.

In C#, it just works.

DateTime.Parse("2010/01/01") and DateTime.Parse("2010-01-01") ? sure, no problem. It'll figure it out.

But in Java? Date.Parse(string) returns a long.... what? Why?

Now I'm no expert in Java, I haven't done it in a while, but I don't want to spend 30 minutes to an hour reading on why. 

So if you want a quick solution, here's one:

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateAdapter {
 public static Date parseDate(String s) 
 throws java.text.ParseException{
            SimpleDateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
   return dfm.parse(s);
 }
}

Viewing all articles
Browse latest Browse all 23

Trending Articles