java.util.Calendar not setting correct date using set(field,value)

32 views
Skip to first unread message

Mark Bolduc

unread,
Mar 20, 2020, 7:37:21 PM3/20/20
to CodenameOne Discussions
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA Netbeand 11
Desktop OS Windows 10 Pro
Simulator Latest
Device Simulator, possibly devices

I have a method that passed date and time integers to Calendar to sed a Date object.

Curious, I diod not see this before unless it just broke.
            int year = 2020;
            int month = 3;
            int dayinmonth = 20;
            int hour = 1700;
            int min = 20;
            int sec = 0;
            java.util.Date dte1 = getDateObjectFromYYYY_MM_DD_HH_MM_SS(year, month, dayinmonth, hour, min, sec);
            System.out.println("dte1 = [" + dte1 + "]");

           Printed =  (dte1 = [Sat May 30 08:20:00 EDT 2020])

    public java.util.Date getDateObjectFromYYYY_MM_DD_HH_MM_SS(int year, int month, int day, int hour, int min, int sec) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month-1);
            cal.set(Calendar.DAY_OF_MONTH, day);
            cal.set(Calendar.HOUR, hour);
            cal.set(Calendar.MINUTE, min);
            cal.set(Calendar.SECOND, sec);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (cal.getTime()); // Returns a java.util.Date object
    }

Thoughts?

Regards




Shai Almog

unread,
Mar 21, 2020, 1:30:01 AM3/21/20
to CodenameOne Discussions
You set the hour to 1700 instead of 17.

Mark Bolduc

unread,
Mar 21, 2020, 6:50:10 AM3/21/20
to CodenameOne Discussions
Sorry, I can't believe I did that.

Have a great week end.
Reply all
Reply to author
Forward
0 new messages