简体   繁体   English

在Android上计算当前一周两天之间的时差

[英]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 例子1

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

Example 2 例子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. 我创建了这个简单的函数,它以“星期几”(例如星期一) ,“从时间”(例如04:00:00)和“到时间”(例如09:00:00)为参数,并返回它是否在范围内。

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: 您可以将一个日期对象变成一个很长的时间(自1970年1月1日以来的毫秒数),然后使用TimeUnit来获取秒数:

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. 结束日期和开始日期作为您可以自行设置的日期的日期对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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