Skip to content
Advertisement

Debug simple java code related to Calendar Date GMT

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

public class Test {

    public static void main(String[] args) throws ParseException {

        Calendar dateFromNet = strToCal("11-MAR-2004", "dd-MMM-yyyy");
        Calendar IEndTime = strToCal("20-05-2004", "dd-mm-yyyy");

        if (dateFromNet.after(IEndTime) ) {

            System.out.println(dateFromNet);
            System.out.println(IEndTime);

            System.out.println("not true: 11-MAR-2004(11-3-2004) > 20-05-2004 ");
        }


    }

    private static Calendar strToCal(String date, String format) throws ParseException {
        SimpleDateFormat input = new SimpleDateFormat(format);
        input.setTimeZone(TimeZone.getTimeZone("GMT"));

        Date d = (Date) input.parse(date);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c;
    }
}

This test shows

dateFromNet.after(IEndTime) == true

i.e. 11-03-2004 is after 20-05-2004

What have I done wrong?

Advertisement

Answer

Calendar IEndTime = strToCal("20-05-2004", "dd-mm-yyyy");

mm is for milliseconds; make those capitol M, like this:

Calendar IEndTime = strToCal("20-05-2004", "dd-MM-yyyy");
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement