Results 1 to 5 of 5
  1. #1
    VeasMKII's Avatar
    VeasMKII is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default [SOLVED] Setting Calendar

    When i use the code below i expect the date to be set to 01/01/01.

    Java Code:
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
            int day = 1;
            int month = 1;
            int year = 1;
            System.out.println("Composite Date:"+day+"/"+month+"/"+year);
            System.out.println("Before set:"+dateFormat.format(cal.getTime()));
            cal.set(year, month, day);
            System.out.println("After set:"+dateFormat.format(cal.getTime()));
    The output i get shows i don't actually get this:

    Java Code:
    Composite Date:1/1/1
    Before set:22/03/09
    After set:01/02/01
    Is this a bug or am i missing something obvious?
    Last edited by VeasMKII; 03-22-2009 at 05:19 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Is this a bug or am i missing something obvious?
    You're missing something obvious. Avoid magic numbers if possible and use Calendar constants. Run this code to see what Calendar uses to represent January. You'll go "aha!", trust me.

    Java Code:
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    public class Fubar2
    {
      public static void main(String[] args)
      {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
        int day = 1;
        int month = Calendar.JANUARY;
        int year = 1;
        System.out.println("Composite Date:"+day+"/"+month+"/"+year);
        System.out.println("Before set:"+dateFormat.format(cal.getTime()));
        cal.set(year, month, day);
        System.out.println("After set:"+dateFormat.format(cal.getTime()));
        
        System.out.println("Calendar.JANUARY = " + Calendar.JANUARY);
      }
    }

  3. #3
    VeasMKII's Avatar
    VeasMKII is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    Ah, i see. The months include 0 like arrays ;) thanks

    I've just thought of an alternative way of getting around it inbetween the reply

    Java Code:
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
            int day = 1;
            int month = 1;
            int year = 1;
            System.out.println("Date:"+day+"/"+month+"/"+year);
            System.out.println("Before:"+dateFormat.format(cal.getTime()));
           [B] Date date = null;
            try {
                date = dateFormat.parse(day + "/" + month + "/" + year);
            } catch (ParseException ex) {
                //
            }[/B]
            System.out.println(date);
    [B]        cal.setTime(date);[/B]
            System.out.println("After:"+dateFormat.format(cal.getTime()));
    Its kind of botched, but it formats the date to dd/MM/yy and then uses the setTime method instead from a Date variable
    Last edited by VeasMKII; 03-22-2009 at 05:32 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Those in the know tell me here that Java date is farked up, that Sun knows this and there may be a fix in the future. One possible solution (that Java itself may choose to use) is to use Joda-Time in place of Java Date and Calendar class. You can read more about this here:

    Joda Time - Java date and time API - Home

  5. #5
    VeasMKII's Avatar
    VeasMKII is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    Thanks for the info

    I've always thought the use of irregular calendars (Feb 28) something a bit strange to understand anyway ;) i can imagine the hell when designing those API's

Similar Threads

  1. Calendar bug?
    By Stigvig in forum Advanced Java
    Replies: 14
    Last Post: 02-08-2009, 09:56 AM
  2. Calendar Pop-up issue
    By jeeva in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-29-2009, 01:09 AM
  3. calendar
    By John in forum SWT / JFace
    Replies: 12
    Last Post: 08-07-2008, 10:54 PM
  4. How to use Calendar class
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-04-2008, 02:33 PM
  5. Web calendar
    By Daniel in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-27-2007, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •