在 Java 中比较时间值的方法有哪些?
在本地时间类表示本地时间,即时间,而不时区。这个类提供了各种方法,例如isBefore(),isAfter()和,isEqual()来比较两次。
示例
import java.time.LocalTime;
public class Test {
public static void main(String args[]) {
LocalTime Time1 = LocalTime.of(10, 15, 45);
LocalTime Time2 = LocalTime.of(07, 25, 55);
Boolean bool1 = Time1.isAfter(Time2);
Boolean bool2 = Time1.isBefore(Time2);
if(bool1){
System.out.println(Time1+" 是在之后 "+Time2);
}else if(bool2){
System.out.println(Time1+" 是之前 "+Time2);
}else{
System.out.println(Time1+" 等于 "+Time2);
}
}
}输出结果10:15:45 是在之后 07:25:55
示例
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CreateDateTime {
public static void main(String args[])throws ParseException{
String timeStr1 = "8:27:45 AM";
String timeStr2 = "2:30:12 PM";
//实例化SimpleDateFormat类
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:SS a");
Date dateTime1 = formatter.parse(timeStr1);
Date dateTime2 = formatter.parse(timeStr2);
Boolean bool1 = dateTime1.after(dateTime2);
Boolean bool2 = dateTime1.before(dateTime2);
Boolean bool3 = dateTime1.equals(dateTime2);
if(bool1){
System.out.println(timeStr1+" 是在之后 "+timeStr2);
}else if(bool2){
System.out.println(timeStr1+" 是之前 "+timeStr2);
}else if(bool3){
System.out.println(timeStr1+" 等于 "+timeStr2);
}
}
}输出结果8:27:45 AM 是在之后 2:30:12 PM
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下