在Android中比较两个时间值

lo8azlld  于 2023-05-12  发布在  Android
关注(0)|答案(2)|浏览(129)

我有两个时间值作为字符串,我需要检查开始时间是在结束时间之前。开始和结束时间分别为02:00 AM和02:00 PM。我使用了以下代码。但它给了我假的。我尝试了HH:mm k、HH:mm a和HH:mm aa模式,结果都是false。它应该给予真的,因为2am是在2pm之前,对吗?

public static boolean checkTimings(String startTime, String endTime) {
        String pattern = "HH:mm k";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern,Locale.US);
        try {
            Date date1 = sdf.parse(startTime);
            Date date2 = sdf.parse(endTime);
            Log.e("date 1 ", date1.toString());
             Log.e("date 2 ", date2.toString());
            if (date1.before(date2)) {
                System.out.println("time1 is before time2");
                return true;
            }  else {
                System.out.println("time1 is after time2");
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
jvlzgdj9

jvlzgdj91#

根据Java API,AM/PM格式标记应该是小写字母a,而不是k,如果这是您传递给方法的参数的格式。这段代码应该对你有用:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");

        checkTimings("02:00 PM", "02:00 PM");
    }

    public static boolean checkTimings(String startTime, String endTime) {
        String pattern = "hh:mm a";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
        try {
            Date date1 = sdf.parse(startTime);
            Date date2 = sdf.parse(endTime);
            if (date1.before(date2)) {
                System.out.println("time1 is before time2");
                return true;
            } else {
                System.out.println("time1 is after time2");
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}

此外,你可以优化条件逻辑,检查作为参数传递的值是否是同时期的:

public static boolean checkTimings(String startTime, String endTime) {
    String pattern = "hh:mm a";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
    try {
        Date date1 = sdf.parse(startTime);
        Date date2 = sdf.parse(endTime);
        int compareResult = date1.compareTo(date2);
        if (compareResult < 0) {
            System.out.println("time1 is before time2");
            return true;
        } else if (compareResult == 0) {
            System.out.println("time1 is equal to time2");
            return true;
        } else {
            System.out.println("time1 is after time2");
            return false;
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return false;
}
nfs0ujit

nfs0ujit2#

不要使用SimpleDateFormat,因为它是outdated
使用java.time类来格式化数据。

// parse you date using DateTimeFormatter
val time1 = LocalTime.parse("02:30 AM", DateTimeFormatter.ofPattern("hh:mm a"))
val time2 = LocalTime.parse("02:30 PM", DateTimeFormatter.ofPattern("hh:mm a"))
// compare time using isAfter or isBefore
val isLate = time1.isAfter(time2)

相关问题