Question

I'm parsing some date data from an XML string into a hierarchy of model objects. The dates are in a 10-digit seconds format. I use the method below to convert those seconds into Date objects

public static Date getDateFromSecondsString(String seconds){
    try{
        long millis = Long.parseLong(seconds) * 1000;
        Date date = new Date(millis);
        return date;
    }
    catch(Exception ex){
        ex.printStackTrace();
    }

    return null;
}

Here's the problem...

When I step through the parsing code at run-time (the code snippet for that is shown below), the date conversion method is returning the expected date.

Element startDateElt = eventElt.getChild("start_date");
if(startDateElt != null){
    startDateElt.setEndTextElementListener(new EndTextElementListener() {
        @Override
        public void end(String body) {
            currEvent.startDate = DateTimeUtil.getDateFromSecondsString(body);
        }
    });
}

However, once I have finished populating my model objects with the parsed data, the dates are wrong. Here are some examples:

Seconds: 1369206000, should be: 2013-05-22, unfortunately is: 2013-05-03
Seconds: 1369292400, should be: 2013-05-23, unfortunately is: 2013-05-04
Seconds: 1369551600, should be: 2013-05-26, unfortunately is: 2013-04-30
Seconds: 1369638000, should be: 2013-05-27, unfortunately is: 2013-05-01
Seconds: 1369724400, should be: 2013-05-28, unfortunately is: 2013-05-02

I have looked through my code to make sure nothing is modifying the dates between the time that the XML is parsed and the time that I display the dates. I know that the Date objects in Java/Android are a little messed up, but would they behave like this?

Any suggestions or insights would be greatly appreciated.

Was it helpful?

Solution

Turns out, the problem was that I was using Date.toDay() in some places where I should have been using Date.toDate() instead. Grrr! I dislike Java's Date implementation.

Thanks to all of you that gave your two cents, and let this be a warning for anyone who may be confusing toDay() with toDate().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top