Using GregorianCalendar with SimpleDateFormat

RejectionHurts picture RejectionHurts · May 31, 2012 · Viewed 101.2k times · Source

So, I've been racking my brain over this (should-be) simple exercise to make the program turn a date string into a GregorianCalendar object, format it, and return it again as a string when it's done.

This is the last little bit of a program that takes in a chuck of text from a file, breaks it down into individual records, then breaks the records into individual pieces of data and assigns them to a person object.

I've checked the code in multiple places and the code does exactly what it's supposed to be doing up until I call the format function, which throws an IllegalArgumentException. The GergorianCalendar object is assigned the values it is supposed to be assigned (though printing it is, again, a whole other story as seen below…), yet the format will not accept the object for formatting.

Unfortunately, the instructor wasn't too sure of how to use the GregorianCalendar and SimpleDateFormat (yet assigned us to work with them) and said: "Just Google it…" I tried, and nothing I've found has helped.

The code I have so far is:

public class DateUtil {

    public static GregorianCalendar convertFromDMY(String dd_mm_yy) throws ParseException{

        // this actually works, got rid of the original code idea
        String[] splitDate = dd_mm_yy.split("-");
        int days = Integer.parseInt(splitDate[0]);
        int month = Integer.parseInt(splitDate[1]);
        int year = Integer.parseInt(splitDate[2]);

        // Dates are going in right, checked in print statement,
        // but the object is not getting formatted…
        GregorianCalendar dateConverted = new GregorianCalendar(year, month, days);
        format(dateConverted);
        return dateConverted;
    }

    public static String format(GregorianCalendar date){

        SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
        String dateFormatted = fmt.format(date);
        return dateFormatted;
    }
}

The error I get is:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object >as a Date

    at java.text.DateFormat.format(DateFormat.java:281)
    at java.text.Format.format(Format.java:140)
    at lab2.DateUtil.format(DateUtil.java:26) 
    at lab2.DateUtil.convertFromDMY(DateUtil.java:19)
    at lab2.Lab2.createStudent(Lab2.java:75)
    at lab2.Lab2.main(Lab2.java:34)

And another thing, am I even using the GregorianCalendar right?? When I print out that object's value (should be getting a date, right?) I get this:

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Vancouver",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=189,lastRule=java.util.SimpleTimeZone[id=America/Vancouver,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=?,YEAR=1985,MONTH=4,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=22,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?]

The year, month and day_of_month values are all correct as they are the numbers I passed into the creation of it.

Thoughts, suggestions, am I even close?

Edit

Original issues cleared up (thank you assylias!), but I still can't print properly because the two functions aren't linked and the requirements are to have a GregorianCalendar date value printed out from the person object (as birthdate is a GregorianCalendar).

Updated code:

public class DateUtil {

    static SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");

    public static GregorianCalendar convertFromDMY(String dd_mm_yy) throws ParseException{

        // this actually works, got rid of the original code idea
        String[] splitDate = dd_mm_yy.split("-");
        int days = Integer.parseInt(splitDate[0]);
        int month = (Integer.parseInt(splitDate[1]) - 1);
        int year = Integer.parseInt(splitDate[2]);

        // dates go in properly
        GregorianCalendar dateConverted = new GregorianCalendar(year, month, days);
        String finalDate = format(dateConverted);
        return ;
    }

    public static String format(GregorianCalendar date) throws ParseException{

       fmt.setCalendar(date);
        String dateFormatted = fmt.format(date.getTime());
        System.out.println(dateFormatted);
        return dateFormatted;
    }

}

Last edit

OK, so it seems that I'm an idiot and DIDN'T need to link the two DateUtil functions together, but use them in tandem. First, convert the birthdate to a GregorianCalendar and store it in the person object. Then, in the print statement, just simply tell the program to format that date as it's being printed. Problem was solved. All works according to specifications now, and I feel that much stupider because I was flailing like a fish out of water for the last day or so with the DateUtil class, trying to get them to work at the same time.

Thanks for the help all on getting the date go in properly!

Answer

assylias picture assylias · May 31, 2012

SimpleDateFormat.format() method takes a Date as a parameter. You can get a Date from a Calendar by calling its getTime() method:

public static String format(GregorianCalendar calendar) {
    SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
    fmt.setCalendar(calendar);
    String dateFormatted = fmt.format(calendar.getTime());

    return dateFormatted;
}

Also note that the months start at 0, so you probably meant:

int month = Integer.parseInt(splitDate[1]) - 1;