Results 1 to 12 of 12
  1. #1
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default need help with getting current time in Millis

    I am doing an assignment and I'm stuck. I'm supposed to ask the user for a date change it to millis, get the current date and change it to millis and then compare. Everytime I try this it comes up with VERY random output...here's both the methods:

    Java Code:
    	public static long readUserDate()
    	{
    		Scanner kb = new Scanner(System.in);
    		Calendar cal = Calendar.getInstance();
    		
    		System.out.print("Please enter a date: ");
    		int mm = kb.nextInt();
    		int dd = kb.nextInt();
    		int yy = kb.nextInt();
    	
    		cal.set(yy, mm, dd);
    		long uDate = cal.getTimeInMillis();
    		return uDate; 
    		
    	}
    	
    	public static long currentTimeMillis()
    	{
    		long sDate = findComputerDate();
    		return sDate;
    	}
    			
    	public static long findComputerDate()
    	{
    		long sDate = System.currentTimeMillis();
    		return sDate;
    	}
    Please help the output doesn't make sense. I'll put the same date in and the output as a long is:

    ----jGRASP exec: java CSCD210Lab4

    Please enter a date: 1 23 2011
    Your date is in the future.
    1298450536249
    1295772138992

    ----jGRASP: operation complete.

    ----jGRASP exec: java CSCD210Lab4

    Please enter a date: 1 23 2011
    Your date is in the future.
    1298450095127
    1295771698204

    ----jGRASP: operation complete.

    ----jGRASP exec: java CSCD210Lab4

    Please enter a date: 1 23 2011
    Your date is in the future.
    1298450113083
    1295771715735

    ----jGRASP: operation complete.
    Please help! I've been all over the API and can't find ANYTHING!!!

    -Clerek

  2. #2
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default very simple

    Java Code:
    Calendar cal = Calendar.getInstance();
    System.out.println([B]cal.getTimeInMillis()[/B]);
    cal.getTimeInMillis() returns a long, which contains the number of milli seconds from 1970 Jan 1 00:00:00 till today's current time

  3. #3
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    So how could I change the first method to have a more consistent answer? I still need it in Milliseconds to make the comparison between the two longs. How can I strip the time off? Like say I make it at midnight for both dates? That would increase consistency and make it easier to compare the two...???

    Thanks,
    -Clerek

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

    Default

    the output doesn't make sense.
    Read the API closely.

    First, from the set() documentation we read "Parameters:
    year - the value used to set the YEAR calendar field.
    month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.
    date - the value used to set the DAY_OF_MONTH calendar field."

    You are entering a month value of 1, ie February, which explains why "Your date is in the future."

    Secondly as to why the numbers keep changing: we would expect the current time to change because more milliseconds have elapsed from one run to the next. Slightly more trickily we would also expect the millisecond time for the user entered time to keep changing. That's because as the getInstance() documentation states: "The Calendar returned is based on the current time..." and the current time changes from one run to the next as noted before. (Of course you change the year, month and day but the hour-of-day, minutes, seconds and milliseconds are left as they are returned by getInstance()).

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

    Default

    Sorry for the slow post.

    Like say I make it at midnight for both dates? That would increase consistency and make it easier to compare the two...?

    That is exactly the right approach. Use the 2 argument form of set() to zero the unwanted fields.

  6. #6
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Could you be more specific.
    Is the future/past thing you want to compare is based only one date, or is it based both on time and date?

    If it's the second one, the solution I gave above is enough

    Java Code:
    long currentTimeInMillis = cal.getTimeInMillis();
    Then go ahead with
    Java Code:
    cal.set(yy, mm, dd);
    long givenTimeInMillis = cal.getTimeInMillis()
    if (currentTimeInMillis > givenTimeInMillis)
    //it's past
    else
    //it's future
    Let me know if you don't want the time to come in picture

  7. #7
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    If you just want to compare date (ignoring time) see the two variables in the below code

    Java Code:
    	int year = cal.get(cal.YEAR);
    	int dayOfTheYear = cal.get(cal.DAY_OF_YEAR);
    You can get the integer value of year and the integer value of day of the year (Dec 31 will be 365/366 in this case)

    Build your logic using these two integers

  8. #8
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    Exactly, I don't want the time to come into the picture or at least just make them both come at midnight...Here's what i did for the first method:

    Java Code:
    	public static long readUserDate()
    	{
    		Scanner kb = new Scanner(System.in);
    		Calendar cal = Calendar.getInstance();
    		
    		System.out.print("Please enter a date: ");
    		int mm = kb.nextInt();
    		int dd = kb.nextInt();
    		int yy = kb.nextInt();
    		cal.set(yy, mm, dd);
    		cal.set(Calendar.HOUR, 12);//constant time
    		cal.set(Calendar.MINUTE, 00);//constant time
    		cal.set(Calendar.SECOND, 00);//constant time
    		long uDate = cal.getTimeInMillis();
    		return uDate; 
    	}
    then after this, should I apply the same set() with the two arguments for the other method?

    -Clerek

  9. #9
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    Another thing I just noticed:

    Java Code:
    cal.set(yy, mm, dd);
    Should this be:

    Java Code:
    cal.set(mm, dd, yy);
    Right? I am entering it as 3 20 1987 being March 20th, 1987.

    Subith: Part of the assignment is to use a date and then compare using System.currentTimeinMillis. I think there is a lesson somewhere in this but as far as I can tell it's just a Pain...I wanted to make the first method compare to the second in one of three ways: future, past, or same. I need to have three different kinds of output so I was wanting to do one of each...
    Last edited by Clerek; 01-23-2011 at 11:17 AM.

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

    Default

    The three argument form of set() requires the arguments in the order year, month, day-of-month as described in the API docs I linked to originally.

    So if you enter 3 20 1987 in that order your code will set mm to 3, dd to 20, and yy to 1987. Consequenty cal1.set(yy,mm,dd) will set cal1 to 20 April, 1987.

  11. #11
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Hi Clerek,

    This is what i have mentioned in my last post.
    suppose 3rd March 2011 is the given date

    Java Code:
    Calendar calCurrent = Calendar.getInstance();
    //Set the date as shown below
    Calendar calGiven = Calendar.getInstance();
    calGiven.set(mm, dd, yy);
    Now use the .get() method as shown in my previous post (post #7)

    Hope that helps :(

  12. #12
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    thanks! I figured it all out and turned it in for a grade. Thanks all for the help. Here's my code if anyone else needs help. I ended up setting the hour on both and changing the user date to be more user friendly (accept 1 instead of 0 for January). Extra credit was to tell the difference between the two dates. I couldn't really get that to work but it was an attempt that counted...

    Code:

    Java Code:
    /**Ricardo Flores
    *
    *	This is a simple java program that asks for a date and
    *		then pulls the date from the system and compares it 
    *		to the date that was asked for.
    *
    *	Extra credit has been attempted in calcDateDifference.
    */
    import java.lang.Math;
    import java.util.Scanner;
    import java.util.Calendar;
    
    public class CSCD210Lab4
    {
    	public static void main(String [] args)
    	{
    		
    		Calendar uDate = readUserDate();
    		Calendar sDate = currentTimeMillis();
    		calcDateDifference(uDate, sDate);	
    		//System.out.println(uDate.getTime());
    		//System.out.println(sDate.getTime());
    	}//end main
    	
    	public static Calendar readUserDate()
    	{
    		Scanner kb = new Scanner(System.in);
    		Calendar cal = Calendar.getInstance();
    		cal.clear();
    		
    		System.out.print("Please enter a date: ");
    		int mm = kb.nextInt();
    		int dd = kb.nextInt();
    		int yy = kb.nextInt();
    		cal.set(Calendar.MONTH, mm-1);
    		cal.set(Calendar.DAY_OF_MONTH, dd);
    		cal.set(Calendar.YEAR, yy);
    		cal.set(Calendar.HOUR, 12);
    		cal.set(Calendar.MINUTE, 00);
    		cal.set(Calendar.SECOND, 00);
    		cal.set(Calendar.MILLISECOND, 00);
    		return cal;
    	}
    	
    	public static Calendar currentTimeMillis()
    	{
    		int mm, yy, dd;
    		long sDate = findComputerDate();
    		Calendar cal1 = Calendar.getInstance();
    		cal1.setTimeInMillis(sDate);
    		cal1.set(Calendar.HOUR, 00);
    		cal1.set(Calendar.MINUTE, 00);
    		cal1.set(Calendar.SECOND, 00);
    		cal1.set(Calendar.MILLISECOND, 00);
    		return cal1;
    	}
    			
    	public static long findComputerDate()
    	{
    		return System.currentTimeMillis();
    	}
    	
    	public static void calcDateDifference(Calendar uDate, Calendar sDate)
    	{
    		int yy = uDate.get(Calendar.YEAR);
    		int yy1 = sDate.get(Calendar.YEAR);
    		int mm = uDate.get(Calendar.MONTH);
    		int mm1 = sDate.get(Calendar.MONTH);
    		int dd = uDate.get(Calendar.DAY_OF_MONTH);
    		int dd1 = sDate.get(Calendar.DAY_OF_MONTH);
    		int yy2 = yy - yy1;
    		int mm2 = mm - mm1;
    		int dd2 = dd - dd1;
    		int futyy, futmm, futdd;
    		
    		if(uDate.after(sDate))
    		{
    			System.out.println("Your date is " + dd2 + " day " + mm2 + " month and " + yy2 + " year in the future.");
    		}
    		else if(uDate.before(sDate))
    		{
    			System.out.println("Your date is " + dd2 + " day " + mm2 + " month and " + yy2 + " year in the past.");
    		}
    		else
    		{
    			System.out.println("Both your dates are the same.");
    		}
    	}
    		
    
    
    }//end class

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 01:46 PM
  2. Replies: 1
    Last Post: 08-16-2010, 11:49 AM
  3. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 05:10 PM
  4. Current Time in millisecs
    By jitman in forum New To Java
    Replies: 1
    Last Post: 03-11-2010, 06:16 PM
  5. how to get the current date and time
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 07:05 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
  •