Results 1 to 12 of 12
- 01-23-2011, 08:48 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 20
- Rep Power
- 0
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:
Please help the output doesn't make sense. I'll put the same date in and the output as a long is: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! I've been all over the API and can't find ANYTHING!!!----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.
-Clerek
- 01-23-2011, 08:57 AM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
very simple
cal.getTimeInMillis() returns a long, which contains the number of milli seconds from 1970 Jan 1 00:00:00 till today's current timeJava Code:Calendar cal = Calendar.getInstance(); System.out.println([B]cal.getTimeInMillis()[/B]);
- 01-23-2011, 09:18 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 20
- Rep Power
- 0
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
- 01-23-2011, 09:26 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Read the API closely.the output doesn't make sense.
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()).
- 01-23-2011, 09:29 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 01-23-2011, 09:29 AM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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
Then go ahead withJava Code:long currentTimeInMillis = cal.getTimeInMillis();
Let me know if you don't want the time to come in pictureJava Code:cal.set(yy, mm, dd); long givenTimeInMillis = cal.getTimeInMillis() if (currentTimeInMillis > givenTimeInMillis) //it's past else //it's future
- 01-23-2011, 09:53 AM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
If you just want to compare date (ignoring time) see the two variables in the below code
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)Java Code:int year = cal.get(cal.YEAR); int dayOfTheYear = cal.get(cal.DAY_OF_YEAR);
Build your logic using these two integers
- 01-23-2011, 10:02 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 20
- Rep Power
- 0
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:
then after this, should I apply the same set() with the two arguments for the other 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; }
-Clerek
- 01-23-2011, 10:05 AM #9
Member
- Join Date
- Jan 2011
- Posts
- 20
- Rep Power
- 0
Another thing I just noticed:
Should this be:Java Code:cal.set(yy, mm, dd);
Right? I am entering it as 3 20 1987 being March 20th, 1987.Java Code:cal.set(mm, dd, yy);
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 10:17 AM.
- 01-23-2011, 10:23 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 01-23-2011, 10:56 AM #11
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Hi Clerek,
This is what i have mentioned in my last post.
suppose 3rd March 2011 is the given date
Now use the .get() method as shown in my previous post (post #7)Java Code:Calendar calCurrent = Calendar.getInstance(); //Set the date as shown below Calendar calGiven = Calendar.getInstance(); calGiven.set(mm, dd, yy);
Hope that helps :(
- 01-26-2011, 05:50 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 20
- Rep Power
- 0
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
-
JAVA Programmers Wanted - Full-Time and Part-Time Positions open
By javatrek2020 in forum Jobs OfferedReplies: 3Last Post: 08-23-2011, 12:46 PM -
Code for - On click of button current date & time should be assignesd to label.
By amartya51 in forum Suggestions & FeedbackReplies: 1Last Post: 08-16-2010, 10:49 AM -
calculate time diff for particular time period
By baktha.thalapathy in forum New To JavaReplies: 2Last Post: 05-24-2010, 04:10 PM -
Current Time in millisecs
By jitman in forum New To JavaReplies: 1Last Post: 03-11-2010, 05:16 PM -
how to get the current date and time
By valery in forum New To JavaReplies: 1Last Post: 08-03-2007, 06:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks