Results 1 to 5 of 5
  1. #1
    iainnitro is offline Member
    Join Date
    Nov 2012
    Location
    Denver, CO
    Posts
    2
    Rep Power
    0

    Default Help with Date calculations

    This may seem like a silly question, but I am knocking my head against the wall trying to get this seemingly simple part of my program to run.

    In a nutshell, I take a date in a format 2012-11-15 06:15:50 GMT and my program then converts it to local time from GMT. I end up with a calendar object after this.

    I make a new calendar object called now. So, I have two calendar objects and need to get the difference (duration) between them. I would like to know seconds, minutes, hours, days... but the scope of my application is only 2 hours from the GMT input time and the goal of the program is to output whether or not we have missed the 2 hour window or to report how much time we have left to complete the window.

    I would also point out that the actual program is in Groovy, but the Java stuff should work fine from everyone's heresay.

    This is the code at this point:

    Java Code:
    String input = "2012-11-15 06:15:50"
    inDate = input + " GMT"
    println("The Date input was: " + inDate)
    
    //make date input into calendar object
    def date = Date.parse("yyyy-M-dd H:m:s z",inDate).toCalendar()
    println("The Date converted is: " + date.format("yyyy-M-dd H:m:s z"))
    
    //add 2 hours to date originally entered
    etime = date
    etime.add(Calendar.HOUR_OF_DAY, +2)
    println("Estimated time is: " + etime.format("yyyy-M-dd H:m:s z"))
    
    //manipulate dates
    def now = Calendar.getInstance()
    println("The Current Time is: " + now.format("yyyy-M-dd H:m:s z"))
    //code to get difference between now and date here
    Suggestions on how to improve this welcome, but right now just need to get it working. The input will be from console or Swing later, but right now using a static timestamp as I test the program.

    Thanks,

    Mike
    Last edited by iainnitro; 11-16-2012 at 01:07 PM. Reason: forgot some info in code

  2. #2
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Help with Date calculations

    I think I must be missing something since this is trivial and does not need Calendar. As far as I can see all you need to do is parse the date string to a java.util.Date() using class SimpleDateFormat with an appropriate timezone and extract the number of milliseconds from the date. Then create a new java.util.Date object using the default constructor and extract it's number milliseconds. Subtract the two and you have the number of milliseconds you need!

  3. #3
    iainnitro is offline Member
    Join Date
    Nov 2012
    Location
    Denver, CO
    Posts
    2
    Rep Power
    0

    Default Re: Help with Date calculations

    Hi Sabre150. Could you provide example code? this would be very helpful. Thanks.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Help with Date calculations

    This is a forum, not a code factory. Sabre150 has spelled out the few simple steps to achieve your goal; write code that followd that outline and if you still have a problem, post that code, in the form of an SSCCE (Short, Self Contained, Correct (Compilable), Example).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Help with Date calculations

    Moved from Advanced Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Hypotenuse Calculations
    By Nightcrawler in forum New To Java
    Replies: 21
    Last Post: 03-17-2012, 08:23 PM
  2. OO for calculations?
    By StateMachine in forum New To Java
    Replies: 1
    Last Post: 12-31-2011, 09:17 AM
  3. Time calculations
    By Jason in forum New To Java
    Replies: 6
    Last Post: 09-29-2011, 10:42 PM
  4. Rounding calculations
    By lynxbci in forum New To Java
    Replies: 9
    Last Post: 08-20-2011, 04:49 PM
  5. Thread calculations, and another thread to use the calculations
    By phil128 in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-12-2010, 05:32 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
  •