Monday, April 15, 2013

[android help] Android format a UTC datetime string into local format throws null error


hi I have the method below which takes the value of a UTC datetime string, format it to local display and return:



public static String convertDateStringUTCToLocal(String sourceUtcDateTimeString)
{
SimpleDateFormat simpleDataFormat = new SimpleDateFormat();
simpleDataFormat.setTimeZone(getCurrentTimeZone());
String outputUTCDateTimeString = simpleDataFormat.parse(sourceUtcDateTimeString, new ParsePosition(0)).toString();

return outputUTCDateTimeString;
}

public static TimeZone getCurrentTimeZone()
{
Calendar calendar = Calendar.getInstance();
TimeZone outputTimeZone = calendar.getTimeZone();

return outputTimeZone;
}


When debugging, the value of parameter sourceUtcDateTimeString is 'Mon Apr 15 13:54:00 GMT 2013', I found that 'simpleDataFormat.parse(sourceUtcDateTimeString, new ParsePosition(0))' gives me 'null', and 'simpleDataFormat.parse(sourceUtcDateTimeString, new ParsePosition(0)).toString()' throws error "java.lang.NullPointerException at toString()".


Looks like there is nothing at ParsePosition(0), but I am really new to Android dev, no idea why this is happening and how to get around it, could any one help out with a fix? I got stuck on this issue for hours.


thanks in advance.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...