Results 1 to 9 of 9
  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default Set and add date

    Hi,

    How can I use a static date attribute to store a specific date for example,
    1/1/2008 using GregorianCalender class? Also, I need to add this date to the user's date when they enter day, month and year in integar. After all of this,
    I need to show the final date in the format day/month/year. Any help will be much appreciated.

    Thanks in advance.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Did you try anything at all?
    Open the API specs for GrgorianCalendar, read it, try some code and post if you have problems with your code.

  3. #3
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Ok,I have written my codes but I don't know what exactly I should do next to connect them by adding the set date to the user's input date.
    here is my source code:

    public class Car
    { public static void main(String[] args)
    { private static GregorianCalendar date = new GregorianCalendar();

    /*set the date*/
    public static void set()
    { date.set(GregorianCalendar.YEAR, 2008);
    date.set(GregorianCalendar.MONTH, 1);
    date.set(GregorianCalendar.DATE, 1); }

    /*read the user's date*/
    private String getDate()
    { System.out.println("Days: ");
    day = In.nextInt();
    System.out.println("Month: ");
    month = In.nextInt();
    System.out.println("Year: ");
    year = In.nextInt(); }
    }

    thanks.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Did you even try to compile that code?
    Do you know how to call a method from the main method?
    Also if you have to post code, format it properly and post it wrapped in code tags.

  5. #5
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    I am not really sure how to use tags for my codes.
    This program needs another method that could add the user's input to the Set date and I don't know how to do that. The user enters an integar number for day, month and year and these will be added to the set date which is in this case 1/1/2008. And this will be displayed in the format
    day/month/year using another method. I wasn't able to find much information regarding this.

    Thanks.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Well this is a duplicate post, Moderator make note -- but here is my post from the other thread because I have not seen any activity on it.

    Well, you could store the date in long format, then use the setTime method inherited from Calendar. For example:
    Java Code:
    import java.util.Date;
    import java.util.GregorianCalendar;
    
    public class PointInTime {
        static long time = 1252247661203L;
        static GregorianCalendar cal;
    
        static{
            cal = new GregorianCalendar();
        }
    
        public static String getTime(){
            cal.setTime(new Date(time));
            return cal.get(cal.HOUR_OF_DAY)+":"+cal.get(cal.MINUTE)+":"+cal.get(cal.SECOND);
        }
    
        public static void main(String[] args) {
            System.out.println(getTime());
        }
    }
    I included a main method for testing -- Is this along the lines you were thinking of? Any time the getTime method is called, it will always return the same point in time, stored in the long value. You can use the Date class to convert a traditional date/time into a long value, or you can just store a Date object.

  7. #7
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    I still have no clues how to go about making my program do what i metioned in my previous post. The program needs another method that could add the user's input to the Set date and I don't know how to do that. The user enters an integar number for day, month and year and these will be added to the set date which is in this case 1/1/2008. And this will be displayed in the format
    day/month/year using another method.
    Thanks.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I don't like to spoon feed, but you're not grasping what I was hinting at. You really should have just read the API for gregorianCalendar, as it contains everything you need. I learned how to do this for the first time and wrote the code in less than 10 min with the api. Anyway, good luck, I hope it helps :)
    Java Code:
    import java.util.GregorianCalendar;
    
    public class PointInTime {
        static long startTime = 1252247661203L;
        static GregorianCalendar cal;
    
        static{
            cal = new GregorianCalendar();
        }
    
        public static String getTime(int year, int month, int date){
            cal.setTimeInMillis(startTime);
            cal.add(cal.YEAR, year);
            cal.add(cal.MONTH, month);
            cal.add(cal.DATE, date);
            return cal.get(cal.DAY_OF_MONTH)+"/"+cal.get(cal.MONTH)+"/"+cal.get(cal.YEAR);
        }
    
        public static void main(String[] args) {
            //Add 1 year, 1 month and 1 day to starting time
            System.out.println(getTime(1,1,1)); 
        }
    }

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    PS you'll need to change the startTime to suit your needs -- mine was System.currentTimeMillis() from earlier today - you can get the time in ms for a specific date with the gregorianCal object, simply set the date first with ints, then print the current time in ms using the "getTimeInMillis()" method.

Similar Threads

  1. java.util.Date vs java.sql.Date
    By Jack in forum New To Java
    Replies: 5
    Last Post: 10-28-2010, 03:59 PM
  2. Date
    By dj kourampies in forum New To Java
    Replies: 3
    Last Post: 02-22-2009, 12:53 PM
  3. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 09:10 AM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 09:32 PM
  5. Difference between current date and anothe date
    By vijay balusamy in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 05:15 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
  •