Results 1 to 10 of 10
  1. #1
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default Calendar object? - Getting date from user

    I'm going to go ahead and edit my OP for the last part of my task.
    I need to compare a user-prompted date, with the system's date using currentTimeMillis. If the prompted date is before the system date I will print X and if it is after I will print Y.

    Java Code:
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Scanner;
    
    public class DateComparison
    {
    	public static void main (String [] args)
    	{
    	       userInput();
                   systemDate();
                   differenceInDate();
            }
    	
    	public static void userInput()
    	{
    		Scanner scan = new Scanner(System.in);
    		Calendar cal1 = Calendar.getInstance();
    		int month, day, year;
    		
    		System.out.println("Please enter a month MM: ");
    		month = scan.nextInt();
    		System.out.println("Please enter a day DD: ");
    		day = scan.nextInt();
    		System.out.println("Please enter a year YYYY: ");
    		year = scan.nextInt();
    		System.out.println("You chose: " + month + " " + day + " " + year);
    		cal1.set(year, month, day);
    		long userTime = cal1.getTimeInMillis();
    		System.out.println(userTime); //just to test to see if it will print the long value
    				
    	}//end userInput
    	
    	public static void systemDate()
    	{
    		long compTime = System.currentTimeMillis();
    		Calendar compDate = Calendar.getInstance();
    		compDate.setTimeInMillis(compTime);
    		Date date = compDate.getTime();
    		System.out.println("Today's date: " + date);
    		System.out.println(compTime); //just to test to see if it will print the long value
    		
    	}//end systemDate
    	
    	public static void differenceInDate()
    	{
    		// what I want to do is something like:
                    // if (userTime > compTime) 
                    // { S.O.P("Your date is later"); }
                    // else 
                    // { S.O.P("Your date is earlier"); }
    		
    	}//end differenceInDate
    	
    }//end class
    But, how can I get my third method (differenceInDate) to recognize my userTime and compTime values? I thought just including userInput(); and systemDate(); in my method was enough but I get errors for userTime and compTime.
    Last edited by hiei_yasha; 01-23-2011 at 12:36 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I don't understand how to use the calendar class for user input

    You can use the set() method of the Calendar class.

    Java Code:
    cal1.set(year, month, day);

    (Note that month has to be zero based, so that January is zero etc. If that upsets your users you can subtract one before using the set() method.)

  3. #3
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    You can use the set() method of the Calendar class.

    Java Code:
    cal1.set(year, month, day);

    (Note that month has to be zero based, so that January is zero etc. If that upsets your users you can subtract one before using the set() method.)
    so that sets the inputted date as a calendar object called cal1, correct?
    also, are there repercussions or benefits for setting it as (month, day, year) instead of (year, month, day)?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Did you read the API documentation linked to?

    so that sets the inputted date as a calendar object called cal1, correct?

    It sets what the Calendar class calls "fields" (There are lots of these: day of month, day of week, month, year, am/pm etc).


    are there repercussions or benefits for setting it as (month, day, year) instead of (year, month, day)?

    As with any method you are obliged to use the arguments in the order that the method expects. In this case it must be year, month, day of month because that is what the documentation says the method requires. This does not affect the order in which you get these integer values from the user. Your existing code is fine and the user will never see the order of the arguments are they are used with set().

  5. #5
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Did you read the API documentation linked to?




    It sets what the Calendar class calls "fields" (There are lots of these: day of month, day of week, month, year, am/pm etc).





    As with any method you are obliged to use the arguments in the order that the method expects. In this case it must be year, month, day of month because that is what the documentation says the method requires. This does not affect the order in which you get these integer values from the user. Your existing code is fine and the user will never see the order of the arguments are they are used with set().
    Yes I've read through the api, that's always the first place I go to. I was just curious because it compiled regardless of what order I had set the last statement. Unfortunately the API is still such a confusing-ass document to me - I wish there was a newbie version of the API :P I guess it just takes practice and repetition.

    So now i'm onto the last part of my problem.. I have my system's date using currentTimeMillis in a separate method, and will set the user input date in a different method (the stuff done in the previous posts). If I want to compare the dates, will the user input date have to be converted into something different than what it is now?
    Basically what I want to do is if the user input date is before the system date i'll print X, and Y if it is after the system date.
    Or, can I just use a for statement as they are now?
    Last edited by hiei_yasha; 01-22-2011 at 05:07 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I guess you are using System.getTimeMillis(). That means you have the current instant-of-time (reported as milliseconds since some arbitrary instant-of-time).

    You can't compare this directly with the calendar instance you have constructed. A Calendar represents the sort of information you get from a clock (month, day, year etc). The two are not the same thing because at any instant of time clocks around the world are reporting different times.

    One way of proceeding is to look through the Calendar API (*) for a method that will give you the instant-of-time corresponding to cal1. It has a name similar to that used to find the system instant-of-time. Then use an if statement to decide which number is bigger.

    (This process of conversion only works because the timezone - the place where the clock is - is assumed to be your default timezone both for System.currentTimeMillis() and the similar Calendar method. As described in the docs! Also note that calendar-times and instants-of-time are both millisecond values so if your user enters day, moth, year you will end up with midnight on that day: this may or may not be a problem for whatever use you are putting this to.)


    (*) Sorry for being a bastard, but you're right the API documentation takes some getting use to and, you're right again, it takes practice and repetition.

  7. #7
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    Need to bump this thread. I've figured out how to get both values in long form, I just want to compare those values in a separate method. If this was all in main I think it would work, however I need to have 3 separate methods.
    How do I get stuff from one method into the other? I thought all you had to do was have methodName(); in the other method?
    I've spent over 15 hours on this assignment (yea, i suck) and have other classes/homework to attend to :S *frustrated face*

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    How do I get stuff from one method into the other?

    That's what method arguments are for. You are said to pass the argument from the caller to the method called. (The method called gets a copy of the value of the argument.)

    An example may help:

    Java Code:
    public class Example {
        public static void main(String[] args) {
            int a = makeA();
            int b = makeB();
    
            System.out.println("Is factor: " + isFactor(a, b));
        }
    
        private static int makeA() {
            return 42;
        }
    
        private static int makeB() {
            return 666;
        }
    
            /** Returns true iff a given int is a factor of another given int. */
        private static boolean isFactor(int a, int b) {
            return (b % a) == 0;
        }
    }

    The makeA() and makeB() methods produce a couple of int values and the isFactor() does the work of comparing them. Everything nicely split up into three methods with results and arguments being passed around between them.

    (It seems common to for people to try and make isFactor() also do the work of reporting the result with the System.out.println() statement. That's possible, but, as a rule, a method should do one thing.)

  9. #9
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    Sweet, almost done! I have it working for earlier date and later date conditions, however the same date is a problem- for January I tried each combination of 0, 00, 01, 1, and everything is either earlier or later. I think this might be what you were talking about when you mentioned the midnight value and running into a potential problem? Dunno.

    Java Code:
    public class DateDifference
    {
    	public static void main (String [] args)
    	{
    		long userTime = userInput();
    		long compTime = systemDate();
    		differenceInDate(userTime, compTime);
    	}
    	
    	public static long userInput()
    	{
    		Scanner scan = new Scanner(System.in);
    		Calendar cal1 = Calendar.getInstance();
    		int month, day, year;
    		
    		System.out.println("Please enter a month MM: ");
    		month = scan.nextInt();
    		System.out.println("Please enter a day DD: ");
    		day = scan.nextInt();
    		System.out.println("Please enter a year YYYY: ");
    		year = scan.nextInt();
    		System.out.println("You chose: " + month + " " + day + " " + year);
    		cal1.set(year, month, day);
    		long userTime = cal1.getTimeInMillis();
    		return userTime;
    	}
    	
    	public static long systemDate()
    	{
    		long compTime = System.currentTimeMillis();
    		Calendar compDate = Calendar.getInstance();
    		compDate.setTimeInMillis(compTime);
    		Date date = compDate.getTime();
    		System.out.println("Today's date: " + date);
    		return compTime;	
    	}
    	
    	public static boolean dfferenceInDate(long userTime, long compTime)
    	{
    		if (userTime > compTime)
    		{
    			System.out.println("Your date is later");
    		}
    		else if (userTime < compTime)
    		{
    			System.out.println("Your date is earlier");
    		}
    		else 
    		{
    			System.out.println("Your date is the same");
    		}
    		return (userTime > compTime) == true;
    		
    	}
    	
    }

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Yes, it may be the midnight problem.

    Correcting the typo in how you declare the difference method, I get

    Java Code:
    Please enter a month MM: 
    0
    Please enter a day DD: 
    23
    Please enter a year YYYY: 
    2011
    You chose: 0 23 2011
    Today's date: Sun Jan 23 16:23:57 NZDT 2011
    Your date is earlier

    This is what I expect because midnight this morning is, indeed, earlier than now.

    (1) Since you are only interested in days and not millisecond precision time the thing to do in your systemDate() method is to reset compDate to midnight so you are only comparing midnights with midnights. This involves using the set() method to zero the hour-of-day/minute/second and millisecond fields.

    Of course you can't return compTime, you have to return compDate.getTimeInMillis() since you have "normalised" compDate and, thereby, changed the instant-of-time it refers to.

    (2) I was wrong before when I said that userInput() will leave you at midnight on the date entered. You can include the line

    Java Code:
    System.out.println("You entered: " + cal1.getTime());

    to see the actual time represented by cal1.

    Basically you have to "normalise" cal1 to midnight as well. (Or clear() cal1 after it is returned by Calendar.getInstance()).

    A millisecond could elapse between one method being called and the other so I think all this normalising to midnight is necessary.

    (3) Note that Calendar implements the Comparable interface. Ie there is a compareTo() method that basically does what you want. If userInput() and systemDate() returned their Calendar instances rather than long values you could use compareTo() in the differenceInDate() method.

    (4) If you're thinking Java's time/date management is a bit of a dog you may be right.
    Last edited by pbrockway2; 01-23-2011 at 05:03 AM.

Similar Threads

  1. Calendar date problem
    By groobelar in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 04:47 AM
  2. how select date form calendar to textFeild
    By the swan in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2009, 01:16 PM
  3. 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
  4. java Date and Calendar
    By valoyivd in forum New To Java
    Replies: 1
    Last Post: 03-30-2008, 07:49 PM
  5. Calendar.DATE
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-04-2008, 08:51 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
  •