2

i have writen a Method to calculate the difference between two Dates.

here is the Method:

/**
 * Method to calculate time Difference between to Times Strings
 *
 * @param time1 The Current Time Time
 * @param time2 Item Start Time
 * @param item the Item should to be played
 * @return Difference between Two Times as Seconds
 */
public static long timeDifference(String time1, String time2, Item item) {

    // get the current Calendar Instance
    Calendar now = Calendar.getInstance();


    // calculate the days Difference between the Item Start Day and  the current Day
    int daysToAdd=0;

    if(RestOperations.dayoffWeek<item.getDay()){

        daysToAdd= (RestOperations.dayoffWeek-item.getDay()) * (-1);
    }
    else{

        daysToAdd = 7-(RestOperations.dayoffWeek-item.getDay());
    }

    // parse the passed Times Strings to Date Objects
    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
    Date date1 = null;
    Date date2 = null;
    try {
        date1 = format.parse(time1);
        date2 = format.parse(time2);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    // set the Current Time to the current Calendar instance
    now.setTime(date1);

    // create Date for the Item
    Calendar itemDate = Calendar.getInstance();

    // set the Item Start Time to the Item Calendar Instance
    itemDate.setTime(date2);

    // add the Days Difference to the Item Calendar Insatnce to determinate the Start Date of the Item 
    itemDate.add(Calendar.DAY_OF_MONTH, daysToAdd);



    // get Difference between the both Calendar Instances to determinate how much Seconds must the Thread be stopped
    long difference =  itemDate.getTimeInMillis()-now.getTimeInMillis();
/*        if (date1 != null & date2 != null) {
        difference = (int) (date2.getTime() - date1.getTime());
    }*/

    // start for debugging!
    int hour = now.get(Calendar.HOUR_OF_DAY);
    int minute = now.get(Calendar.MINUTE);
    int sec = now.get(Calendar.SECOND);

    int hour2 = itemDate.get(Calendar.HOUR_OF_DAY);
    int minute2 = itemDate.get(Calendar.MINUTE);
    int sec2 = itemDate.get(Calendar.SECOND);

    String currDate = now.get(Calendar.YEAR)+"-"+now.get(Calendar.MONTH)+"-"+now.get(Calendar.DAY_OF_MONTH)+" "+hour + ":" + minute + ":" + sec;
    String itDate = itemDate.get(Calendar.YEAR)+"-"+itemDate.get(Calendar.MONTH)+"-"+itemDate.get(Calendar.DAY_OF_MONTH)+" "+hour2 + ":" + minute2 + ":" + sec2;
    Log.i("CurDate: ", currDate);
    Log.i("ItemStartDate: ", itDate);
    Log.i("difff: ", difference+"");
    // end of debugging

    return difference;

}

When i run this Method for this example:

duration = Utility.timeDifference("11:8:56","07:04:43",6);

i got the year = 1970, The Month = 0 and the Duration is negative (<0)

So for explanation the first Parameter corresponds to the current Time. The second Parameter corresponds when the Item should start to Play and the third Parameter corresponds the Day of Week when the Item should start to Play.

At the above code i try to do the following:

The first Parameter should be transformed to the current Date: 20-11-2015 11:08:56.

The Second Parameter should be transformed using the third Parameter to the current Date: 21-11-2015 07:04:43.

can someone tell me please where could be the Error?

user3232446
  • 439
  • 2
  • 14
  • Your "setting" the `time` of the `Calendar` to `date1`, which, I'm, guessing, is just a time value, so its date component is been default to the epoch. Trying checking the value of `date1` (and `date2`) and then consider using either Java 8's Time API or JodaTime instead – MadProgrammer Nov 20 '15 at 10:34
  • yes your right! but why im getting the Month as 0? 1970-0-1 11:45:11 – user3232446 Nov 20 '15 at 10:46
  • Because Java's time is broken. See http://stackoverflow.com/questions/344380/why-is-january-month-0-in-java-calendar – Firzen Nov 20 '15 at 10:52
  • 2
    Because in Java calendar months are 0 based :) – Niks Nov 20 '15 at 10:53
  • ok. but the current Month is 11 why im getting 0?! – user3232446 Nov 20 '15 at 10:55
  • What do you have in RestOperations.dayoffWeek ? – reos Nov 20 '15 at 15:25
  • Day of Week ( in wich Day must the Item be played) for example for Today(friday) Restoperations.Dayofweek will give 5 as Result – user3232446 Nov 20 '15 at 17:22
  • *"ok. but the current Month is 11 why im getting 0?!*" - For the same reason you're getting 1970, A time value of 0 is equal to `Thursday, 1 January 1970`, all you're doing is setting the time to `11:45:11` – MadProgrammer Nov 20 '15 at 20:42

0 Answers0