比较Android中的两个时间值

huangapple go评论63阅读模式
英文:

Comparing two time value in Android

问题

I have two time value as string, I need to check the start time is before end time. The start and end times are 02:00 AM and 02:00 PM. I have used the following code.
But it gives me false.
I have tried with HH:mm k, HH:mm a and HH:mm aa patterns, all result in false. It should give true, since 2 am is before 2 pm, right?

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.println("time1 is after time2");
            return false;
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return false;
}
英文:

I have two time value as string, I need to check the start time is before end time. The start and end times are 02:00 AM and 02:00 PM. I have used the following code.
But it gives me false.
I have tried with HH:mm k, HH:mm a and HH:mm aa patterns, all result in false. It should give true, since 2 am is before 2pm, right?

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;
    }

答案1

得分: 0

根据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;
}
英文:

According to the Java API, the AM/PM format marker should be the lowercase letter a, not k, if this is the format of the arguments you're passing to your method. This code should work for you:

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(&quot;Hello world!&quot;);

        checkTimings(&quot;02:00 PM&quot;, &quot;02:00 PM&quot;);
    }

    public static boolean checkTimings(String startTime, String endTime) {
        String pattern = &quot;hh:mm a&quot;;
        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(&quot;time1 is before time2&quot;);
                return true;
            } else {
                System.out.println(&quot;time1 is after time2&quot;);
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}

Additionally, you could polish the conditional logic to check if the values passed as arguments are coetaneous:

public static boolean checkTimings(String startTime, String endTime) {
    String pattern = &quot;hh:mm a&quot;;
    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 &lt; 0) {
            System.out.println(&quot;time1 is before time2&quot;);
            return true;
        } else if (compareResult == 0) {
            System.out.println(&quot;time1 is equal to time2&quot;);
            return true;
        } else {
            System.out.println(&quot;time1 is after time2&quot;);
            return false;
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return false;
}

答案2

得分: 0

不要使用 SimpleDateFormat,因为它已经过时。
请使用 java.time 类来格式化数据。

// 使用 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"))
// 使用 isAfter 或 isBefore 进行时间比较
val isLate = time1.isAfter(time2)
英文:

Don't use SimpleDateFormat as it's outdated
Use java.time classes to format the data.

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

huangapple
  • 本文由 发表于 2023年5月11日 17:10:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定