android 如何在java中比较Date中的时间部分

fzwojiic  于 2022-12-02  发布在  Android
关注(0)|答案(3)|浏览(144)

我需要比较两个日期/时间变量中的时间部分,看看时间是否介于android中的当前时间之间。我该怎么做呢?

Boolean InBetweenTime(Date currentTime, Date StartTime, Date EndTime)
{
    Boolean result = false;
    Calendar calendar = Calendar.getInstance();
    if ((!currentTime.before(StartTime))&&(!currentTime.after(EndTime))){
        //between time
    } else {
        //not inbetween time
    }
    return result;
}
9lowa7mx

9lowa7mx1#

如果你只需要比较时间,忽略日期,那么你应该这样做,这样你的“日期”部分在所有三个变量中是相等的。

Boolean InBetweenTime(Date currentTime, Date startTime, Date endTime){
    Boolean result = false;

    //calendar for currentTime
    Calendar currentCal = Calendar.getInstance();
    currentCal.setTime(currentTime);
    //calendar for startTime
    Calendar startCal = Calendar.getInstance();
    startCal.setTime(startTime);
    //calendar for endTime
    Calendar endCal = Calendar.getInstance();
    endCal.setTime(endTime);
    //set corresponding date fields of startTime and endTime to be equal t ocurrentTime, so you compare only time fields
    startCal.set(currentCal.get(Calendar.YEAR), currentCal.get(Calendar.MONTH), currentCal.get(Calendar.DAY_OF_MONTH));
    endCal.set(currentCal.get(Calendar.YEAR), currentCal.get(Calendar.MONTH), currentCal.get(Calendar.DAY_OF_MONTH));
    //return true if between, false otherwise
    return (!currentTime.before(startCal.getTime())) && (!currentTime.after(endCal.getTime()));
}
llmtgqce

llmtgqce2#

要获取Date的时间部分(即日期/时间值),请使用以下代码:

Date date = ...
Instant instant = d.toInstant();
Instant midnight = instant.truncatedTo(ChronoUnit.DAYS);
Duration timeSinceMidnight = Duration.between(midnight, instant);
System.out.println(timeSinceMidnight);
khbbv19g

khbbv19g3#

使用java.time API的解决方案(Java 8+)

使用modern date-time API时,溶液将洁净透明。

演示

import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {
        // Tests
        System.out.println(isBetween(Instant.parse("2022-11-20T20:30:00Z"), Instant.parse("2022-11-15T23:45:00Z")));
        System.out.println(isBetween(Instant.parse("2022-11-20T10:20:30Z"), Instant.parse("2022-11-15T20:10:05Z")));
        System.out.println(isBetween(Instant.parse("2022-11-20T10:20:30Z"), Instant.parse("2022-11-15T10:20:30Z")));
    }

    static boolean isBetween(Instant startInstant, Instant endInstant) {
        LocalTime currentTime = getTime(Instant.now());
        LocalTime startTime = getTime(startInstant);
        LocalTime endTime = getTime(endInstant);
        return currentTime.isAfter(startTime) && currentTime.isBefore(endTime);
    }

    static LocalTime getTime(Instant instant) {
        return instant.atZone(ZoneId.systemDefault()).toLocalTime();
    }
}

在~ 2022-11- 30 T23:32:00 Z运行时的输出

true
false
false

从**Trail: Date Time**了解有关现代日期-时间API的更多信息。

使用Java版本〈8的解决方案

如果您的项目允许使用第三方的日期-时间库,我建议您选择How to use ThreeTenABP in Android Project。ThreeTenABP库可以将java.time API向后移植到Java 6和7。
如果你想坚持使用Java版本〈8的标准库,这里有一个解决方案:
诀窍是将今天的年、月、日设置为startDate和endDate,然后你可以像往常一样比较它们。如果你在Date的所有三个示例中都有相同的年、月、日,基本上你只是比较它们的时间。

演示

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws ParseException {
        // Tests
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        System.out.println(isBetween(sdf.parse("2022-11-20T20:30:00Z"), sdf.parse("2022-11-15T23:45:00Z")));
        System.out.println(isBetween(sdf.parse("2022-11-20T10:20:30Z"), sdf.parse("2022-11-15T20:10:05Z")));
        System.out.println(isBetween(sdf.parse("2022-11-20T10:20:30Z"), sdf.parse("2022-11-15T10:20:30Z")));
    }

    static boolean isBetween(Date startTime, Date endTime) {
        Date currentTime = new Date();
        // System.out.println(currentTime);
        Calendar currentCal = Calendar.getInstance();
        currentCal.setTime(currentTime);
        int year = currentCal.get(Calendar.YEAR);
        int month = currentCal.get(Calendar.MONTH);
        int day = currentCal.get(Calendar.DAY_OF_MONTH);

        // Set today's year, month and day into the startDate
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(startTime);
        startCal.set(year, month, day);
        startTime = startCal.getTime();
        // System.out.println(startTime);

        // Set today's year, month and day into the endDate
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endTime);
        endCal.set(year, month, day);
        endTime = endCal.getTime();
        // System.out.println(endTime);

        return currentTime.after(startTime) && currentTime.before(endTime);
    }
}

在~ 2022-11- 30 T23:33:00 Z运行时的输出

true
false
false

相关问题