tags:

views:

19

answers:

1

this problem is killing me, here is my code

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

public class Converter
{
    // 8/12/2010 12:20:34 PM
    static String DATE_FORMAT = "MM/dd/yyyy h:MM:ss aa";

    // from object to xml
    public static String serializeNCCDate(Date d)
    {
        try
        {
            System.out.println("serializeNCCDate");
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            System.out.println("in from object: " + d.toLocaleString());
            String s = sdf.format(d);
            System.out.println("out to object: " + s);
            return s;
        } catch (Exception ex)
        {
            return null;
        }
    }

    // from xml to object
    public static Date deSerializeNCCDate(String s)
    {
        try
        {
            System.out.println("deSerializeNCCDate");
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            System.out.println("in from xml: " + s);
            Date d = sdf.parse(s);
            System.out.println("out to object: " + d.toLocaleString());
            return d;
        } catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

then running my tests im putting an xml document into my xml engine (jibx) to convert to an object and then putting the object back into jibx to convert it back to a string. when i try to serialize the date "8/12/2010 12:20:34 PM" from my xml, this is the result i get

deSerializeNCCDate
in from xml: 8/12/2010 12:20:34 PM
out to object: Aug 12, 2011 12:00:34 PM
serializeNCCDate
in from object: Aug 12, 2011 12:00:34 PM
out to object: 08/12/2011 12:08:34 PM

why are my years and minutes not what they should be? seems like every time the parse function is called the output that it should be changes. this is pretty simple stuff, why is it not working? i could understand the hours being wrong with time zone differences, but the years and minutes?

+1  A: 

You need m (lowercase) for minutes, not M. See here for more details.

Brian Agnew
GAHHHH, thanks the chart i was looking at on another site was wrong. gee only wasted an hour on a simple typeo, thanks
scphantm