英文:
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("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;
}
}
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 = "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;
}
答案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("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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论