Results 1 to 4 of 4
  1. #1
    mohiit is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Unhappy How to set values with Calendar class ?

    Hi all,

    I want today's date set in Calendar Obj. I have used Calendar class methods to set zero values in Hour, Minutes and Second. But for Hour it sets "1" always. Code is shown below :

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    If I use deprecated methods of Date class (date.setHours(0)), it works fine. But with Calendar class its not working.

    Please suggest possible remedies.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    How did you instantiate the calender object? Seems to me you have done it wrong way. Can you post your code here.

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    I have used Calendar class methods to set zero values in Hour, Minutes and Second. But for Hour it sets "1" always.
    You may read the GregorianCalendar class.

    you can use getTimeInMillis() method to get the current time.
    and the set(int field, int value) method to set the hour of it, also the min and seconds.

    depends on you.... :)
    freedom exists in the world of ideas

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Try something like this,

    Java Code:
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_MONTH, 0);
            cal.set(Calendar.MONTH, 0);
            cal.set(Calendar.YEAR, 0);
    
            System.out.println(cal.getTime());

Similar Threads

  1. How to use Calendar class
    By JavaBean in forum Java Tip
    Replies: 1
    Last Post: 06-26-2008, 04:09 PM
  2. Replies: 4
    Last Post: 04-15-2008, 02:04 PM
  3. How to use Calendar class
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-04-2008, 03:33 PM
  4. Java Calendar Class
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:51 AM
  5. Java Calendar Class
    By vasu18 in forum New To Java
    Replies: 0
    Last Post: 12-06-2007, 11:45 AM

Posting Permissions

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