Android date deserialization?

Status
Not open for further replies.

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
I've got a couple of Android apps that generate XML files that I need to consume elsewhere. Everything is readable except the date strings, which are in the XML serialized as 13 digit numbers.

Examples with known values: (All times are Central Time Zone)
1339773579367 June 14, 2012 10:05pm
1339729545703 June 14, 2012 4:42pm
1339710122523 June 14, 2012 4:39pm
1338071026430 May 27, 2012 3:57pm

How do I convert these strings into their date equivalent? (C# if it matters)
 

Dracorat

Ars Tribunus Militum
2,798
The dates you posted are off by a little bit.

Here's how you do it...

Create a new class file and add this code:
Code:
public static class JavaConversionExtensionMethods
{
	public static DateTime ToDateFromJavaTime(this long javaTime)
	{
		// In Java, dates start from Jan 1, 1970
		DateTime baseDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
		
		// Now convert the time input to a TimeSpan
		TimeSpan offset = TimeSpan.FromMilliseconds(javaTime);
		
		// Combine them
		return baseDate.Add(offset).ToLocalTime();
	}
}

To use it:

Code:
void Main()
{
	long javaDate = 1339773579367;
	Console.WriteLine(javaDate.ToDateFromJavaTime());
}
 
Status
Not open for further replies.