Java中字符串格式的日期比较详解
在实际开发中,有的小伙伴会遇到一些日期处理的问题,最比如日期的比较。有时候日期并不是Date类,而是以字符串来表示的,like this : 1994-09-11,这种日期是没办法直接比较的,这就需要我们将它先格式化了,这里就用到了DateFormat类了。
先上代码:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5563')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5563>public boolean compareDate(String startDate, String endDate) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date sDate = sdf.parse(startDate);
Date eDate = sdf.parse(endDate);
if (eDate.before(sDate)) {
return false;
} else {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
</td></tr></table>
我们这里并没有用到DateFormat类,用的是SimpleDateFormat类,它是DateFormat类的一个子类,这个先不说。
看代码,先构造一个format对象sdf,然后用用sdf将输入的两个字符串格式化成日期,其格式化的方式由你传入的字符串决定,此处是yyyy-MM-dd,因为我要传入的字符串就是1994-09-11这种格式的。然后用sdf的before方法来比较两个日期的大小,是不是很方便呢?
在实际中用的最多的还是SimpleDateFormat类,作为DateFormat的子类, SimpleDateFormat类显然比DateFormat拥有更多的功能,他还能将标准日期格式化成你想要的日期格式,下面是示例代码:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7464')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7464>public String formatDate(Date date){SimpleDateFormat formater = new SimpleDateFormat("yyyy年MM月dd日"); //通过构造方法设置模板
String dateString = formater.format(date);
formater.applyPattern("yyyy.MM.dd");//重新设置格式化模板
dateString = formater.format(date);
return dateString;
}
</td></tr></table>
例子
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6477')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6477>public class Timetest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1="2015-02-08 20:20:20";
String str2="2015-01-08 10:10:10";
int res=str1.compareTo(str2);
if(res>0)
System.out.println("str1>str2");
else if(res==0)
System.out.println("str1=str2");
else
System.out.println("str1<str2");
}
}
当然我们可以先尝试来检查判断一个字符串是不是一个合法的日期格式
例子,
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2605')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2605>public static boolean isValidDate(String str) {
boolean convertSuccess=true;
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
try {
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
// e.printStackTrace();
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
convertSuccess=false;
}
return convertSuccess;
}
您可能感兴趣的文章:
Java 实例
Java中字符串格式的日期比较详解
php5 字符串处理函数汇总
JSP 日期处理
DOS批处理格式化日期字符串的三种方法
php字符串转时间戳
php字符串函数有哪些
js long日期格式转为标准日期格式的代码
JavaScript Date(日期)对象
mysql中FROM_UNIXTIME()和UNIX_TIMESTAMP()函数的区别