首页 > 编程语言 >Java 比较两个日期的方法

Java 比较两个日期的方法

时间:2023-07-28 17:32:01浏览次数:33  
标签:02 01 Java df 日期 Date newDate 方法 2012


三种方法分别是:

1) by Using classic CompareTo method of Date class.
2) by using equals(), before() and after method of Date class.
3) by using equals(), before() and after method of Calendar class in Java.



标签: <无>



代码片段(2)



[代码]

01
import java.text.DateFormat;
 
     
02
import java.text.ParseException;
 
     
03
import java.text.SimpleDateFormat;
 
     
04
import java.util.Calendar;
 
     
05
import java.util.Date;
 
     
06
 
 
     
07
public class HashtableDemo {
 
     
08
 
 
     
09
 public static void main(String args[]) throws AssertionError, ParseException {
 
     
10
 
 
     
11
 DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
 
     
12
 
 
     
13
 //comparing date using compareTo method in Java
 
     
14
 System.out.println("Comparing two Date in Java using CompareTo method");
 
     
15
 compareDatesByCompareTo(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
 
     
16
 compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
 
     
17
 compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("01-02-2012"));
 
     
18
 
 
     
19
 //comparing dates in java using Date.before, Date.after and Date.equals
 
     
20
 System.out.println("Comparing two Date in Java using Date's before, after and equals method");
 
     
21
 compareDatesByDateMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
 
     
22
 compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
 
     
23
 compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));
 
     
24
 
 
     
25
 //comparing dates in java using Calendar.before(), Calendar.after and Calendar.equals()
 
     
26
 System.out.println("Comparing two Date in Java using Calendar's before, after and equals method");
 
     
27
 compareDatesByCalendarMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
 
     
28
 compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
 
     
29
 compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));
 
     
30
 
 
     
31
 }
 
     
32
 
 
     
33
 public static void compareDatesByCompareTo(DateFormat df, Date oldDate, Date newDate) {
 
     
34
 //how to check if date1 is equal to date2
 
     
35
 if (oldDate.compareTo(newDate) == 0) {
 
     
36
 System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
 
     
37
 }
 
     
38
 
 
     
39
 //checking if date1 is less than date 2
 
     
40
 if (oldDate.compareTo(newDate) < 0) {
 
     
41
 System.out.println(df.format(oldDate) + " is less than " + df.format(newDate));
 
     
42
 }
 
     
43
 
 
     
44
 //how to check if date1 is greater than date2 in java
 
     
45
 if (oldDate.compareTo(newDate) > 0) {
 
     
46
 System.out.println(df.format(oldDate) + " is greater than " + df.format(newDate));
 
     
47
 }
 
     
48
 }
 
     
49
 
 
     
50
 public static void compareDatesByDateMethods(DateFormat df, Date oldDate, Date newDate) {
 
     
51
 //how to check if two dates are equals in java
 
     
52
 if (oldDate.equals(newDate)) {
 
     
53
 System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
 
     
54
 }
 
     
55
 
 
     
56
 //checking if date1 comes before date2
 
     
57
 if (oldDate.before(newDate)) {
 
     
58
 System.out.println(df.format(oldDate) + " comes before " + df.format(newDate));
 
     
59
 }
 
     
60
 
 
     
61
 //checking if date1 comes after date2
 
     
62
 if (oldDate.after(newDate)) {
 
     
63
 System.out.println(df.format(oldDate) + " comes after " + df.format(newDate));
 
     
64
 }
 
     
65
 }
 
     
66
 
 
     
67
 public static void compareDatesByCalendarMethods(DateFormat df, Date oldDate, Date newDate) {
 
     
68
 
 
     
69
 //creating calendar instances for date comparision
 
     
70
 Calendar oldCal = Calendar.getInstance();
 
     
71
 Calendar newCal = Calendar.getInstance();
 
     
72
 
 
     
73
 oldCal.setTime(oldDate);
 
     
74
 newCal.setTime(newDate);
 
     
75
 
 
     
76
 //how to check if two dates are equals in java using Calendar
 
     
77
 if (oldCal.equals(newCal)) {
 
     
78
 System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
 
     
79
 }
 
     
80
 
 
     
81
 //how to check if one date comes before another using Calendar
 
     
82
 if (oldCal.before(newCal)) {
 
     
83
 System.out.println(df.format(oldDate) + " comes before " + df.format(newDate));
 
     
84
 }
 
     
85
 
 
     
86
 //how to check if one date comes after another using Calendar
 
     
87
 if (oldCal.after(newCal)) {
 
     
88
 System.out.println(df.format(oldDate) + " comes after " + df.format(newDate));
 
     
89
 }
 
     
90
 }
 
     
91
}


[代码]

01
Comparing two Date in Java using CompareTo method
 
     
02
 01-01-2012 and 01-01-2012 are equal to each other
 
     
03
 02-03-2012 is less than 04-05-2012
 
     
04
 02-03-2012 is greater than 01-02-2012
 
     
05
Comparing two Date in Java using Date's before, after and equals method
 
     
06
 01-01-2012 and 01-01-2012 are equal to each other
 
     
07
 02-03-2012 comes before 04-05-2012
 
     
08
 02-03-2012 comes after 01-02-2012
 
     
09
Comparing two Date in Java using Calendar's before, after and equals method
 
     
10
 01-01-2012 and 01-01-2012 are equal to each other
 
     
11
 02-03-2012 comes before 04-05-2012
 
     
12
 02-03-2012 comes after 01-02-2012

标签:02,01,Java,df,日期,Date,newDate,方法,2012
From: https://blog.51cto.com/u_548275/6885716

相关文章

  • Java 获取对象的属性及属性值
    获取属性名称:/***获取属性名数组**@paramo对象*@return属性名称*/privateString[]getFiledName(Objecto){Field[]fields=o.getClass().getSuperclass().getDeclaredFields();String[]fieldNames=newString[fields.length];for(inti=0;i<......
  • EAS_在controllerBean中调用其他方法,发生异常后,事务没有回滚
    首先列出例子如下:在一个方法中,执行了多个逻辑,第一部分是调用退票逻辑,第二部分是执行其他业务,这里我们遇到问题,退票逻辑执行成功,但是后面的代码异常,这时我们需要的是退回所有执行,这时我们就需要认清facade中的事务属性: 就是EJB规范的6种事务属性:Required:要求有事务:如果已......
  • java——虚拟机、线程
    java虚拟机:是一台想象中的机器,有自己想象的硬件(处理器、堆栈、寄存器等)以及相应的指令系统。生命周期:当执行java程序时,虚拟机开始运行,程序结束虚拟机停止。同一台计算机每多运行一个程序,就会多运行一个虚拟机。虚拟机开始于main()方法。java虚拟机的体系结构:......
  • java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
    环境信息:JDK17、Tomcat10问题原因我这里编译,打包都是好的,但是一运行发现就报错。错误的原因是Tomcat10里也自带了一些lib包,可以打开Tomcat下lib文件夹,这又涉及到了包的加载顺序了一打开发现Tomcat里的是 JakartaEE,我Maven里引用的还是JavaEE导致的。JavaEE的servlet......
  • Robot Framework 自动化测试部署常见问题及处理方法(一)
    1.在Python>>Scripts中运行pythonride.py时报错现象:1Traceback(mostrecentcalllast):2File“E:\Python27\lib\site-packages\robotide\application\application.py”,line56,inOnInit3self.frame=RideFrame(self,self._controller)4File“E:\P......
  • main方法
    importunittestfromBeautifulReportimportBeautifulReportfromtestcases.queryimporttestdemodefallTests():suite=unittest.TestSuite()loder=unittest.TestLoader()#alltests=loder.discover(start_dir="testcases")#suite.addTest(test......
  • 学习Java的第8天
    数据拓展publicclassDemo03{publicstaticvoidmain(String[]args){//*整数拓展进制二进制0b**十进制八进制**0**十六进制**0x**inti=10;inti2=010;//**八进制0**inti3=0x10;//**十六进制0x0~9A~F16**System.out.println(i);System.out......
  • element-ui 日期选择器报错 Prop being mutated: "placement"
    报错信息解决方法,添加placement="bottom-start"<el-date-pickerv-model="queryParams.startTime"type="date"placeholder="开始时间"value-format="yyyy-MM-ddHH:mm:ss"placement="bottom-start">......
  • element-ui中的change事件传递多个参数的方法
    相关小例子(比如,我想要目前的选中值和index)<el-selectv-model="value"placeholder="请选择"><el-optionv-for="(item,index)inoptions":key="item.value":label="item.label":value="i......
  • Java学习1
    java学习01简单写写学习期间不知道or感兴趣的一些东西计算机相关前置知识计算机结构系统软件DOS(DiskOperatingSystem)WindowsLinuxUnix,Mac,Android,IOS...windows快捷键关闭窗口 ALT+F4永久删除文件Shift+Delete快捷指令win+R打开我的电......