Results 1 to 8 of 8
- 09-17-2009, 07:24 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 2
- Rep Power
- 0
- 09-17-2009, 08:01 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Read the API specs for GregorianCalendar.
- 09-17-2009, 11:21 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 2
- Rep Power
- 0
But every method in gregoriancalender is depends on system date.
I want to find out todays date irrespective of system date.
- 09-17-2009, 11:27 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Today's date depends on which Calendar you are using and which part of the world you are in. If you don't want to rely on the settings on the computer running the program, then you are going to have to make your program access some external service over the internet to get the information.
- 09-17-2009, 11:28 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
So get the time from the server.
Edit: Too slow.
- 09-26-2009, 11:55 PM #6
{sigh}
I usually collect things like this into a re-usable utility class, with static method signature, as they are just a simple function to work to return a simple value from only its arguments.
and the sample outputJava Code:import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.junit.Test; public class DateUtil { /** * gets a date that is before, or after a given date by the specified number of days * @param in * @param daysToAdd the number of days to add to the given date. Use a negative value here to go back days * @return */ public static Date dateAdd(Date in, int daysToAdd) { if (in == null) { return null; } GregorianCalendar cal = new GregorianCalendar(); cal.setTime(in); cal.add(Calendar.DAY_OF_MONTH, daysToAdd); return cal.getTime(); } @Test public void getTwoDaysBeforeSomeDate() { // where here i just use system date for testing, but the function converts any date object. Date someDate = new Date(); Date otherDate = DateUtil.dateAdd(someDate, -2); System.out.println("Original Date : " + someDate); System.out.println("Two Days Before: " + otherDate); } }
Java Code:Original Date : Sat Sep 26 18:10:24 EDT 2009 Two Days Before: Thu Sep 24 18:10:24 EDT 2009
Last edited by travishein; 09-26-2009 at 11:57 PM.
- 09-27-2009, 12:30 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
If you notice what the post is about, it is not about subtracting time from a specific date, it is about retreiving the current date without using the System clock. The OPs misguided attempt at "protection".
- 09-27-2009, 01:45 AM #8
My bad. yes, my earlier code post is then quite note helpful.
So, in an applet, to get the time of day, without reading the local system time, I can't think of any good way without doing at least one URL query to some reference.
How about reading it from a simple URL query service to the web server where the applet originated,
(since applets can't communicate with the ntp servers (different host) directly, and that woudl be too slow anyway).
a simple servlet mapped to a /getTime.do url that just echoed' in text the current time back in the response, the applet reads that and turns it into a date using a SimpleDateFormat. It wouldn't need to be a secure url, just returns the date always. that shouldn't be too slow to do, and one query on applet startup and the applet caching the time.
which in turn could get it's value from some internet standard time source ( in case there is concern the server hosting the web application would have it's clock tinkered with)
I guess this is the real problem of how to get that. I would imagine the server on start up would want to invoke a request to pool.ntp.org using their JavaSntpClient , and then cache the time read, and maybe re-read it every day or once in a while depending on how much a priority it is to have an accurate current time, but certianly this could be completely independent of the client's machine where the applet is running and the app server.
now for time caching, if we don't want to invoke more queries, that would need to depend on the local system's time at the time the looked up time was
Date origRemoteActualTime
Date origLocalTime
and then later on,
Date currentLocalTime
so
Date currentRealTime = (currentLocalTime - origLocalTime) + origRemoteActualTime
Similar Threads
-
Been stuck for days! any help would be appriciated...
By juddy1 in forum New To JavaReplies: 8Last Post: 01-06-2011, 06:33 PM -
adding days to a date
By karen.tao in forum Java AppletsReplies: 13Last Post: 09-13-2009, 09:05 PM -
plz i need this program in 2 days .it`s tic tac toe game
By amr in forum Java 2DReplies: 7Last Post: 07-21-2009, 02:39 AM -
Default/System Date Format Pattern?
By Gajesh Tripathi in forum AWT / SwingReplies: 1Last Post: 10-05-2008, 07:34 AM -
No fo days between two dates
By Java Tip in forum Java TipReplies: 0Last Post: 01-28-2008, 09:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks