简体   繁体   中英

Calculate the time difference between two days of current week on Android

I would like to calculate the time difference between two days (eg 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" eg Mon , "from time" eg 04:00:00 and "to time" eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM