`

日期比较的两种方法

 
阅读更多

1、用java.util.Date.compareTo(Date anotherDate) 但是Date这个类很多方法对时区等支持不够,所以不少都是已过时。现在日期推荐用大家这个方法比较calnedar。不过Date还是有可取之处,相对来说它更轻量级。在比较文本日期的时候比较简单,比如:
DateFormat df = new SimpleDateFormat();
(df.parse("2007-05-01")).compareTo(df.parse("2007-07-01"))
算是一个比较严谨又简单的比较文本日期的方法了。


2、用calnedar的实现类GregorianCalendar ,这个构造就多了。实际能进行比较的方法就是public boolean before(Object when),public boolean after(Object when),public int compareTo(Calendar anotherCalendar),其实本质都是用的compareTo

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics