0

I would like to calculate the time difference between two days (e.g. Friday and Saturday) of the same week. This sort of calculation is required for validating a time restriction of my project. To understand more about the restriction see the below examples,

Example 1

            {
                "id": "3",
                "from_day": "Fri",
                "from_time": "16:00:00",
                "to_day": "Sat",
                "to_time": "06:00:00"
            }

Example 2

           {
                "id": "4",
                "from_day": "Mon",
                "from_time": "04:00:00",
                "to_day": "Mon",
                "to_time": "09:00:00"
            }

From the above example I've to verify if the running application passes between the exact date and time of the same week.

What I've done so far?

I've created this simple function which takes the "day of week" e.g Mon, "from time" e.g 04:00:00 and "to time" e.g 09:00:00 as parameter and returns if it's within the range.

public boolean getValidity(String day, String dateStart, String dateStop) {

        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        String current_day = new SimpleDateFormat("EE", Locale.ENGLISH)
                .format(date.getTime());

        if (current_day.matches(day)) {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
            format.setTimeZone(TimeZone.getTimeZone("GMT+8"));

            Date today = Calendar.getInstance().getTime();
            String datePresent = format.format(today);

            Date d1 = null;
            Date d2 = null;
            Date d3 = null;

            try {
                d1 = format.parse(dateStart);
                d2 = format.parse(dateStop);
                d3 = format.parse(datePresent);

            } catch (Exception e) {
                e.printStackTrace();
            }

            long current_time = d3.getTime();
            long start_time = d1.getTime();
            long stop_time = d2.getTime();

            if (current_time >= start_time && current_time <= stop_time) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
    // this function is used for converting the time into GMT +8 before passing as a parameter in the getValidity() function
    public String toGMT(String time){

        //first convert the received string to date
        Date date = null;

        //creating DateFormat for converting time from local timezone to GMT
        DateFormat format = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //getting GMT timezone, you can get any timezone e.g. UTC
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        return format.format(date).toString();
    }

But the above code doesn't works for the first example where the dates are different. It would be extremely helpful if anyone can give some idea of solving the issue.

Community
  • 1
  • 1
Purple Lotus
  • 129
  • 15
  • possible duplicate..read this http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances – Michele Lacorte Sep 04 '15 at 08:46

1 Answers1

1

You can turn a date object into a long (milliseconds since Jan 1, 1970), and then use TimeUnit to get the number of seconds:

long diffInMs = endDate.getTime() - startDate.getTime();

long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs);

end date and start date as date object for your days which you can do self.

Androider
  • 3,833
  • 2
  • 14
  • 24
  • What will happen if I compare Saturday and Sunday? Since after Saturday night it's going to start a new week but how it's going to understand if the Sunday is from the current week or from the next week? Also the time is in HH:mm:ss format but with no dates. – Purple Lotus Sep 04 '15 at 09:58
  • @PurpleLotus The Answer suggests you use the first week of 1970 on UTC (the [epoch](https://en.m.wikipedia.org/wiki/Epoch_(reference_date))) as **fake dates** for the purpose of your calculations. – Basil Bourque Sep 04 '15 at 15:55