Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: GregorianCalendar

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default GregorianCalendar

    converting GregorianCaledanr value to String is easy by
    using SimpleDateFormat class

    but how to convert it into int



    ------------------------------------
    String representation
        public static void main(String[] args)throws IOException
     
        {
            int year,
                month,
                day;
     
            GregorianCalendar cal;
            SimpleDateFormat sdf;
     
            System.out.print("Enter Year: ");
            year = Integer.parseInt(br.readLine( ));
     
            System.out.print("Enter Month:");
            month = Integer.parseInt(br.readLine( ));
     
            System.out.print("Enter Day: ");
            day = Integer.parseInt(br.readLine( ));
     
            cal = new GregorianCalendar(year, month-1, day);
            sdf = new SimpleDateFormat("EEEE");
     
            System.out.println("Day Of Week: " + sdf.format(cal.getTime( )));
        }
     
    }
    --------------------------------------------------------------------
    the problem is asking to display it as int

    heres the code i hope you understand this

    //sets the expiration date
    public void setExpDate(GregorianCalendar date)
    {
       ........<statement> here
    }
     
    //returns the epiration year
    public int getExpYear(  )
    {
      ......<statement> here
    }
     
    //returns the expriration month
    public int getExpMonth( )
    {
    .....<statement>Here
    }
     
    //returns the expiration day
    public int getExpDay( )
    {
     .....<statement> here
    }

    setting the date is easy , displaying the date by String representaion is easy
    but how to display it into an INteger value?


    the program is asking me to extend a predefined class (LibraryCard class) by adding such methods
    for an expiring date of a card

    but to display it as int ...

    Oh my....


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: GregorianCalendar

    Time is represented as a long usually and here we go.

    calendar.getTimeInMillis();

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (August 29th, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: GregorianCalendar

    ahh so i have to use the method of GregorianCalendar class that can return a value of int

    oh my.. the book doesnt give a full detail...... hayzz...

  5. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: GregorianCalendar

    Oh, no you can get the time in milliseconds without the GregorianCalendar, but I thought you wanted to know how to get it from the Calendar.

    System.currentTimeMillis();

    That will get you the time as a long.

    // Json