Get time difference Within 2 gregorian Calendars, 1 obtained via SimpleDate

Hi guys
I'm having a problem with an application, i've posted something before about it and with two GregorianCalendars.
This one is receiving a GregorianCalendar value from the default constructor, and the second gregorian calendar from a String, using SimpleDateFormat class
The problem is that whenever i start the application it get stucked at: "Deze datum ligt: 56 in het verleden" meaning from dutch "this datum is 56 days in the past"
the issue is thus at the method "getVerschil()" which means "get difference" in Dutch.
Even if you try to modify the date fields from the class with the main method, inserting new dates it keeps on telling me 56.
I was wondering if you see anything strange in the code and then a little question.
Does the JDK sees a GregorianCalendar made with the default constructor, and a GregorianCalendar made with a SimpleDateFormat string patter "dd-mm-yyyy", in the same way?
I mean, can java make a comparison within the 2 Calendars recognising which are days and which are the months?
Thank you guys:
Below the codes, the first one is the interface and below you'll see the main method class which inserts the date with SimpleDateFormat
public class Kalender {
      * attributes
     private final String patroon;
     private String datum= null;
     private String userDagText= null;
     private int userDag;
     private int userMaand;
     private int userJaar;
     long DAGMILLIS=  24*60*60*1000;
     //deze kalender wordt gemaakt op het moment dat de toepassingen aangezet wordt
     GregorianCalendar nu= new GregorianCalendar();
     //deze kalender word gemaakt door de user-ingevoerde datum
     GregorianCalendar userCalendar= new GregorianCalendar(userDag, userMaand, userJaar);
     //datum formatter
     SimpleDateFormat sdf;
      * Constructor
     public Kalender(String datum, String patroon){
          this.patroon= patroon;
          this.datum=datum;
          sdf= new SimpleDateFormat(patroon);
     }//EOF
     public void setUserDate(){
          Date userDatum= sdf.parse(datum, new ParsePosition(0));
          userCalendar.setTime(userDatum);
          userDag= userCalendar.get(Calendar.DAY_OF_WEEK);
          userMaand= userCalendar.get(Calendar.MONTH);
          userJaar= userCalendar.get(Calendar.YEAR);
     }//EOF
      * deze methode returns de hele date
     public String geefDatum(){
          return sdf.format(userCalendar.getTime());
     }//EOF
      * deze methode bepaald of de ingevoerde datum een schrikkeljaar is
      * deze methode vindt het tijdverschil tussen twee datums
     public String getVerschil(){
          long userMillis= userCalendar.getTimeInMillis();
          long nuMillis= nu.getTimeInMillis();
          if(nuMillis==userMillis){
               return "deze datum zijn gelijk";
          } else if(nuMillis>userMillis){//userdatum in het verleden
               long millisVerschil= nuMillis-userMillis;
               long dagenVerschil= millisVerschil / (DAGMILLIS);
               return "Deze datum ligt: "+dagenVerschil+" in het verleden";
          } else  if(nuMillis<userMillis){//userdatum in het toekomst
               long millisVerschil= userMillis- nuMillis;
               long dagenVerschil= millisVerschil / (DAGMILLIS);
               return "Deze datum ligt: "+dagenVerschil+" in het toekomst";
          return " ";
}The tester (main) class which creates the second GregorianCalendar is the following
public class KalenderTest {
     public static void main(String[] args){
          //constructor werkt
          Kalender kal= new Kalender("01-28-2010", "dd-mm-yyyy");
          //setUserDate werkt
          kal.setUserDate();
          //testen van getVerschil
          System.out.println(""+kal.getVerschil());
     }Edited by: classboy on Apr 26, 2010 9:22 PM
Edited by: classboy on Apr 26, 2010 9:23 PM

classboy wrote:
Does the JDK sees a GregorianCalendar made with the default constructor, and a GregorianCalendar made with a SimpleDateFormat string patter "dd-mm-yyyy", in the same way?Of course not.
And is there anything wrong with your code? Plenty. You passed the fields to the GregorianCalendar constructor in the wrong order. You got the day of the week and then used it as if it were the day of the month. That's just from a quick look, there's probably more.

Similar Messages

  • Time subtraction within a Calendar instance

    Hello all, quick question about time subtraction within a Calendar instance.
    Calendar calendar = Calendar.getInstance();
    System.out.println(calendar.getTime()); // Tue Apr 07 11:14:24 EDT 2009
    calendar.set(Calendar.MINUTE, -10);
    System.out.println(calendar.getTime()); // Tue Apr 07 10:50:24 EDT 2009Shouldn't the latter time be "Tue Apr 07 11:04:24 EDT 2009" instead?
    Thanks,
    wf

    Read the docs for set() more closely. It doesn't say it adds time. Also look for another method that might add time, since that is what you are doing.

  • Error While Getting Time Difference

    Hi
    I want to get the time difference in hh and mm.
    I have written the following program
    it should give the time diff = 01:38
    but can't understand why its giving 07:08
    import java.text.*;
    public class TimeDiff{
    public static void main(String args[]) throws Exception
    SimpleDateFormat format = new SimpleDateFormat("HH:mm");
    String time1 = "12:23";
    String time2 = "10:45";
    Date date1 = format.parse(time1);
    Date date2 = format.parse(time2);
    long diff = date1.getTime() - date2.getTime();
    System.out.println( "Time diff = " + format.format(new Date(diff)));
    Thanks

    The answer is in the java.util.date class.
    A Date object describes a date starting from Jan 1,1970 00:00:00
    plus how many milliseconds have passed since that time in Long form.
    So when you take the date jan 1,1970 12:23:00 pm (or 24780000 milliseconds) and subtract it from jan 1,1970 10:45:00 am (or 18900000 milliseconds)you get jan 1 1970, 2:38 am ( or 5880000 milliseconds).
    You would only get that time if your system clock was set to
    Greenwich mean time. In your case I am assuming that either your system
    clock is set wrong or you live in India or somewhere close to there.
    In Bombay,Calcutta,Madras or New Delhi for instance your program should
    output "Time diff = 07:38".
    Hope this helps:)

  • Reg : subtracting weekoff day from the time difference

    Hi ,
    I have query where I am getting time difference first of all in terms of hours as:
    DECLARE @L_TIME_DIFF   INT
    SET @L_TIME_DIFF= (DATEDIFF(hh, '05/09/2014', '05/12/2014'))
    i.e. subtracting 12th may - 9th may and getting diff in hours ..in this case it will be 3*24 = 72 hours
    All fine till here. Now next step I need to subtract from this 72 hours if falling any week off day. It can be Friday, it can be Saturday, or Sunday, or Half day Saturday, or Half Day Sunday, or Full 2 days Saturday/Sunday  .
    The above scenario is decided based on one table [CALENDER_WORKINGDAYS] which looks like below :
    CAL_WORKING_DAY 
    CAL_AM
    CAL_PM
    CAL_SEQ_NO
    Monday
    1
    1
    1
    Tuesday
    1
    1
    2
    Wednesday
    1
    1
    3
    Thursday
    1
    1
    4
    Friday
    0
    0
    5
    Saturday
    1
    1
    6
    Sunday
    1
    1
    7
    So in this case, if you look at friday (CAL_AM=0 and CAL_PM=0) this measn friday full day holiday for some country.
    Hence I need to : 72 - 24 = 48 Hours should be my output.
    In another scenario, let s say some country work half day Saturday and Sunday full day off.
    So my output should be : 72 - 36 (because 24 hours for sunday off, and 12 hours for saturday half day off) =
    36 should be my output
    and so on..
    Can you help to build such query based on the above table given ?
    Thanks

    Hi Jose Diz,
    I am trying the below logic. Seems it should be working:
    DECLARE
    @L_START_TIME    DATETIME,          
    @L_END_TIME    DATETIME,
    @L_TIME_DIFF   INT, -- Time Difference considering week offs          
    @W_TIME_DIFF   INT ,-- Whole Time Difference,
    @PUBLICHOLS   INT,
    @WEEK_OFF INT
    set @L_START_TIME    = '5/9/2014';
    set @L_END_TIME    = '5/12/2014';
    /* TO GET THE TIMDIFFERENCE */          
      SET @L_TIME_DIFF=(DATEDIFF(hh,@L_START_TIME,@L_END_TIME))          
      SET @W_TIME_DIFF = @L_TIME_DIFF          
    -- Subtracting day off in the week(i.e. sat/sun or in some case friday) if it is in between start and end
      set @WEEK_OFF= 0;
        SELECT @WEEK_OFF= @WEEK_OFF + 12 * (abs(CAL_AM -1) + abs(CAL_PM -1))
        from CALENDER_WORKINGDAYS
     IF DATENAME(WK,@L_END_TIME) > DATENAME(WK,@L_START_TIME)           
     BEGIN          
      SET @L_TIME_DIFF= @L_TIME_DIFF - @WEEK_OFF          
     END          
    Thanks to confirm

  • Calendar time difference

    Hi, I have a problem, when I sync my Nokia 6300 with my Outlook calendar the appointment time gets changed on my phone to an hour earlier somehow. It has only recently started doing this, it was fine before. I would appreciate any suggestions. Many thanks

    check to see that both your computer and your phone are set in your correct time zone. If they are not the same the phone could be trying to make up for the time difference by altering the alarm times by an hour.
    You do not say where you are so I cannot give you your exact time zone.
    Have you removed your battery from your phone recently and had to reset the time perhaps? This might have caused the problem
    iPhone 5 32GB
    MacBook Pro Retina 15" Mac OS X Mountain Lion 10.8.4

  • How to write a query to get the time difference of two varchar type time columns

    Hi,
    I want to get the time difference between the two varchar type columns.please see the attached image for more details:
    My requirement is like:
    timestarted
    timeended
    timediff
    9:00:00
    10:00:00
    1:00
    9:15
    9:30:00
    0:15

    Storing time alone as VARCHAR2 value is a incorrect design. Always store it as DATE or TIMESTAMP.
    If you already have a messed up design and cant change it, then you need to convert your VARCHAR2 time into a DATE or TIMESTAMP and find the difference. I have converted it to TIMESTAMP and obtained the difference as INTERVAL.
    SQL> with t
      2  as
      3  (
      4  select '09:00:00' timestarted, '10:00:00' timeended from dual
      5  union all
      6  select '09:15:00' timestarted, '09:30:00' timeended from dual
      7  )
      8  select timestarted
      9       , timeended
    10       , (timeended - timestarted) day to second diff
    11    from (
    12          select to_timestamp('01011900' || timeended, 'ddmmyyyyhh24:mi:ss') timeended
    13               , to_timestamp('01011900' || timestarted, 'ddmmyyyyhh24:mi:ss') timestarted
    14            from t
    15         );
    TIMESTARTED                                        TIMEENDED                                          DIFF
    01-JAN-00 09.00.00.000000000 AM                    01-JAN-00 10.00.00.000000000 AM                    +00 01:00:00.000000
    01-JAN-00 09.15.00.000000000 AM                    01-JAN-00 09.30.00.000000000 AM                    +00 00:15:00.000000
    SQL>

  • To get time differnce based on factory calendar

    Dear All,
    We have Creation date, time, End date and time. We need to find out the time difference  based on factory calendar ..ie need to exculde weekends, holidays.
    Is there any FM to get this? Please adivse procedure how to get.
    Thanks
    Konda Reddy

    Try Something like following
    PARAMETERS : P_BEGDA TYPE BEGDA,
                 P_BTIME TYPE SYUZEIT,
                 P_ENDDA TYPE BEGDA,
                 P_ETIME TYPE SYUZEIT.
    DATA LV_DURATION TYPE F.
    CALL FUNCTION 'DURATION_DETERMINE'
      EXPORTING
        UNIT                       = 'H' "Hours
        FACTORY_CALENDAR           = 'GR'
      IMPORTING
        DURATION                   = LV_DURATION
      CHANGING
        START_DATE                 = P_BEGDA
        START_TIME                 = P_BTIME
        END_DATE                   = P_ENDDA
        END_TIME                   = P_ETIME
      EXCEPTIONS
        FACTORY_CALENDAR_NOT_FOUND = 1
        DATE_OUT_OF_CALENDAR_RANGE = 2
        DATE_NOT_VALID             = 3
        UNIT_CONVERSION_ERROR      = 4
        SI_UNIT_MISSING            = 5
        PARAMETERS_NOT_VALID       = 6
        OTHERS                     = 7.
    IF SY-SUBRC <> 0.
    * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    WRITE LV_DURATION.

  • How to get exact date and time difference?

    Hi,
    When i am using the below sql statement:
    SELECT (TO_DATE('7/27/2006 05:00:15 PM','MM/DD/YYYY HH:MI:SS PM') - to_Date('7/27/2006 8:30:13 AM','MM/DD/YYYY HH:MI:SS AM')) * 24 hours FROM DUAL;
    the output is:
    Hours
    8.50055555555556
    But Actually it is 8.30
    So how can i get exact days and time difference between two dates in Oracle?
    Thanks....
    Regards,
    Suman Sakinala
    Edited by: SSN on Sep 19, 2008 1:27 AM

    Or more clearly (this time I have my date_from and date_to the right way around hehe!)
    SQL> ed
    Wrote file afiedt.buf
      1  WITH t as (select TO_DATE('7/27/2006 05:00:15 PM','MM/DD/YYYY HH:MI:SS PM') as dt_to,
      2                    TO_DATE('7/27/2006 08:30:13 AM','MM/DD/YYYY HH:MI:SS AM') as dt_from FROM DUAL)
      3      ,d as (select (dt_to-dt_from)*24 as diff_hrs from t)
      4  -- end of dates
      5  SELECT TRUNC(diff_hrs) as hours
      6        ,TRUNC(((diff_hrs) - TRUNC(diff_hrs))*60) as mins
      7        ,TRUNC(((((diff_hrs) - TRUNC(diff_hrs))*60) - TRUNC(((diff_hrs) - TRUNC(diff_hrs))*60))*60) as secs
      8* FROM d
    SQL> /
         HOURS       MINS       SECS
             8         30          2
    SQL>

  • I have connected my time capsule to motorola SG6580 modem via ethernet.  I have a wireless network on the motorola and a wireless network from the time capsule.  I get duplicate IPs error messages sometimes. How do I fix?

    I have connected my time capsule to motorola SG6580 modem via ethernet.  I have a wireless network on the motorola and a wireless network from the time capsule.  I get duplicate IPs error messages (no connection) sometimes when trying to connect devices to the wireless network. How do I fix? Sometimes devices cannot connect to the network because of conflicting IPs.  I look on the Motorola and every device has a unique IP assigned but occasionally a device has taken the IP of another device.  I have been writing down all the devices and the IPs they have been using.  It happens more with PCs than Macs, ipads or iPhones.

    If the Time Capsule is set up correctly in bridge mode, then it is the responsibility of the Motorola modem/router to provide the correct IP address assignments for all devices on the network.
    Check to insure that the Time Capsule is correctly set up to operate in bridge mode as follows:
    On your Mac, open AirPort Utility
    Finder > Applications > Utilities > AirPort Utility
    Click on the Time Capsule icon, then click Edit in the smaller window that appears
    Click the Network tab at the top of the next window
    Insure that the setting for Router Mode is configured to read "Off (Bridge Mode)"
    Once you have confirmed that the Time Capsule is configured correctly, everything else is the responsibility of the Motorola modem/router as far as network routing and IP address assignments for devices.
    If you continue to have IP address issues, then you should contact the Internet Service Provider (ISP) that provided the Motorola device to you and ask them to fix the issue.

  • Function Module to get the difference between two times.

    Hi All,
    I want to know if there is any function module that gives me the difference between two specified times.
    For Ex: Time 1: 12/01/2007 00:01 A.M
                Time 2: Time 1 - 180 Seconds. This changes the time, also may even change the Date. (As in above situation, the Time 2 will be 11/30/2007 11:58 P.M)
    Could some one please suggest if there is any function module for this case.
    Thanks in Advance.

    Hi, You can use the following function module to calculate the diffrence between to dates in the unit that you want.
    COPF_DETERMINE_DURATION calculates the difference between two dates and time in minutes and hours.
    Parameters:
    EXPORTING
         I_START_DATE "The start date of the time interval
         I_START_TIME "The start time of the time interval
         I_END_DATE   "The end date of the time interval
         I_END_TIME   "The end time of the time interval
         I_UNIT_OF_DURATION "Time unit of the duration if not to be calculated in days.
         Value     =====     Meaning
         ' '     =====     Day (default)
         D     =====     Days
         H     =====     Hours
         MIN     =====     Minutes
         MON     =====     Months
         S     =====     Seconds
         WK     =====     Weeks
         YR     =====     Years
         I_FACTORY_CALENDER     "A factory calender if not using the Gregorian calender
    IMPORTING
         E_DURATION      "Time difference in unit specified.
    Message was edited by:
            Rajesh Soman

  • How to get the time difference (in seconds) between two timestamp fields?

    Hi All,
    I'm exploring validation expressions in SAP MDM Data Manager version 7.1.07.245
    I have two timestamp fields, suppose Update Date and Update-GDS.
    What I'm trying to do is prompt a warning when:
    (Update Date minus Update-GDS) is greater than or equal to zero seconds
    AND
    (Update Date minus Update-GDS) is less than or equal to 900 seconds
    the bottomline is, Update Date must be equal or greater than to Update-GDS but Update Date must not exceed 900 seconds difference.
    my initial try was:
    (Update Date - Update-GDS)>=0 s AND (Update Date - Update-GDS)<=900 s
    But it always returns true even the time difference between update date and update-gds is beyond the condition.
    Can you please guide me through this?
    Thank you very much in advance.
    Regards,
    ~erwin

    Hi Abhishek,
    I tried your code but it always returns true.
    It seems subtraction of timestamp fields do not actually get the time difference... (in seconds)
    Is this possible anyway? Or is there any other better approach on this?
    Thanks,
    ~erwin
    Edited by: erwin.j.rivera on Dec 14, 2011 8:40 AM

  • My Seagate external 250GB hard drive ejects improperly without command.  I am trying to use Time Machine to backup my macbook via usb cable...  I keep getting the message that the disked ejected improperly...  Any solutions...

    My Seagate external 250GB hard drive ejects improperly without command.  I am trying to use Time Machine to backup my macbook via usb cable...  I keep getting the message that the disked ejected improperly...  Any solutions...

    You are not alone. Thousands have this problem, on various models of Macs. Short answer, it has something to do with the disk going to sleep (possibly?). You can't do anything about it and Apple won't address the problem. Maybe you should buy a time capsule. Just keep giving them money and use their proprietary equipment.
    For a more complete answer, see this thread (all 65+ pages of it with NO SOLUTION IN SIGHT... except for some 3rd party solutions that sometimes work, sometimes don't): Disk Drive ejecting itself

  • I am having multiple issues with syncing my iphone calendar and outlook There are often 1 hour time differences in the entries on the 2 devices and sometimes events that have been amended are not updated on iphone I mostly enter items on my laptop

    I am having multiple issues with syncing my iphone calendar and outlook calendar on my laptop
    I mostly enter the original items on my laptop
    There are often 1 hour time differences in the entries on the 2 devices and sometimes events that have been amended are not updated on iphone

    This sounds like an error in Time Zone support. The time zone needs to be the same in Outlook as well as the phone.

  • Can't get time capsule to work via ethernet direct to mac

    Hi All
    I am having trouble setting up my time capsule direct to my mac via ethernet it works fine through my router backs up a treat via ethernet.
    Can anyone help me through the process have looked online but they all set them up through their routers.
    Cheers

    If you reset the Time Capsule by holding in the reset button for 10 seconds and then releasing and allowing it to restart, then you need to connect an Ethernet cable from one of the LAN <-> ports on your wireless router to the WAN "O" port on the Time Capsule.
    Then configure the Time Capsule to create a new wireless network. You don't have to use this new network....you can use your existing network if you want.
    Then connect an Ethernet cable from your Mac to one of the LAN <-> ports on the Time Capsule.

  • Getting the difference between 2 dates

    Hi,
    I am creating a Date object (using today;s day, month and year. hours and minutes come from a config file) and getting the difference between this date and the current date and time.
    My code is as follows:
    I am passing 07:30 into the method as the parameter aSstart_time.
    Calendar newCalendar = Calendar.getInstance(Locale.UK);
        SimpleDateFormat dtformat = new SimpleDateFormat("dd-MM-yy HH:mm");
        int lIYear = newCalendar.get(newCalendar.YEAR);
        int lIMonth = newCalendar.get(newCalendar.MONTH);
        int lIDay = newCalendar.get(newCalendar.DAY_OF_MONTH);
        String lSstartHrs = aSstart_time.substring(0,aSstart_time.indexOf(":"));
        //System.out.println(">>>>>>>>> THE HOURS IS: " + lSstartHrs);
        String lSstartMins = aSstart_time.substring(aSstart_time.indexOf(":")+1,aSstart_time.length());
        //System.out.println(">>>>>>>>> THE MINS IS: " + lSstartMins);
        java.util.Date ldStartTime = new java.util.Date(lIYear,lIMonth,lIDay, Integer.parseInt(lSstartHrs),Integer.parseInt(lSstartMins));
        System.out.println("Year: " + lIYear);
        System.out.println("Month: " + lIMonth);
        System.out.println("Day: " + lIDay);
        System.out.println("Hour: " + Integer.parseInt(lSstartHrs));
        System.out.println("Min: " + Integer.parseInt(lSstartMins));
        System.out.println("The number of milliseconds for current time is: " + newCalendar.getTime().getTime());
        System.out.println("The number of milliseconds for 07:30 is: " + ldStartTime.getTime());
        System.out.println("calendar time: " + dtformat.format(newCalendar.getTime()).toString());
        System.out.println("created time: " + dtformat.format(ldStartTime).toString());
        if(newCalendar.getTime().getTime() < ldStartTime.getTime()){
          System.out.println("The current time is less than the config time (07:30)");
        else{
          System.out.println("The time is greater than 07:30");
        }When I run this program at 10:06am I get the following output:
    Year: 2006
    Month: 1
    Day: 15
    Hour: 7
    Min: 30
    The number of milliseconds for current time is: 1139997962321
    The number of milliseconds for 07:30 is: 61098132600000
    calendar time: 15-02-06 10:06
    created time: 15-02-06 07:30
    The current time is less than the config time (07:30)
    The Date.gettime() method should measure the number of milliseconds after 01-01-1970 so the number of milliseconds for the current time (10:06am) should be greater than the number of milliseconds for 07:30. A you can see in the above output this is not the case.
    Anybody have any idea why this is happening. Am I using the .gettime() method for the correct purpose.
    Any ideas or suggestions would be greatly appreciated.
    Thanks

    It is beacause Calendar use fully defined year as parameter, so Calendar.set(year + 1900, month, date, hrs, min) is (almost) equivalent to new Date(year, month, date, hrs, min)Example:    int year = 85; // old way of specifying a year in a Date
        int month = 3;
        int date = 12;
        int hrs = 12;
        int min = 0;
        Calendar cal = Calendar.getInstance();
        cal.set(year + 1900, month, date, hrs, min);
        Date d = new Date(year, month, date, hrs, min);
        System.out.println(d);
        System.out.println(cal.getTime());You will obtain the same date (except seconds and miliseconds in this case, as they probably won't be zero with Calendar.getInstance(); )

Maybe you are looking for

  • Grouping with A formula issue

    I am grouping on a date that was calculated using the following formula : local stringvar d:= {TAGDTL.USER_DEF_TEXT_1}; //or database field numbervar y:= tonumber ( '20' & d[5 to 6]); numbervar m:= tonumber ( d[1 to 2] ); numbervar dd:= tonumber ( d[

  • Can i search the trash?

    i am trying to search for a file in the trash and it seems to revert to the whole computer when i do this. when i try a search for documents folder it stays in documents folder. any ideas? is this simply a manual process looking for a file in the tra

  • Is it safe to delete driver zip files after driver udpate?

    I have recently updated some drivers on my computer and I would like to know if it is safe to delete these zip files once the drivers have actually been installed. There are also some that are just files in the same update folder which are not compre

  • Is the Adobe Digital Edition (e-reader from the library) compatible with Adobe Reader X

    I have Adobe Reader X on my computer, and recently downloaded the Adobe Digital Edition so that I can read books from the library. However, when I attempt to download a book, the Adobe Reader X opens, and I get the message.... XXXX book could not be

  • Is there a predifined column which shows the number of support messages

    Hi, In test management work list (STWB_WORK), is there a predifined column which shows the number of support messages raised against a message (Yellow spark symbol), there is no ALV layout defined for this scenario, please advice is there any configu