Results 1 to 8 of 8
  1. #1
    vanitha nanda is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default how to get 10 days prior system date

    Is there any way to get 10 days prior system date irrespective of today's date using java, since the user may change the system date at any time.

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

    Default

    Read the API specs for GregorianCalendar.

  3. #3
    vanitha nanda is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    But every method in gregoriancalender is depends on system date.
    I want to find out todays date irrespective of system date.

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

    Default

    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.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    So get the time from the server.

    Edit: Too slow.

  6. #6
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default {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.

    Java 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);
      }
    }
    and the sample output
    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-27-2009 at 12:57 AM.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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".

  8. #8
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    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

  1. Replies: 8
    Last Post: 01-06-2011, 07:33 PM
  2. adding days to a date
    By karen.tao in forum Java Applets
    Replies: 13
    Last Post: 09-13-2009, 10:05 PM
  3. Replies: 7
    Last Post: 07-21-2009, 03:39 AM
  4. Default/System Date Format Pattern?
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 1
    Last Post: 10-05-2008, 08:34 AM
  5. No fo days between two dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 10:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •