java中的日期比较大小

方法一:
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
String dateBegin=fmt.format(carrierCommand.getDateBegin());
String dateEnd=fmt.format(carrierCommand.getDateEnd());
//如果获得的日期格式不是'2008-05-22',就必须要格式化一下日期
String dateBegin = request.getParameter("dateBegin");
String dateEnd = request.getParameter("dateEnd");
if(java.sql.Date.valueOf(dateBegin).after(java.sql.Date.valueOf(dateEnd))){
//起始日期大于结束日期
errors.rejectValue("dateEnd", null, "起始日期必须小于结束日期!");
}
StringBuffer timeBegin = new StringBuffer();
StringBuffer timeEnd = new StringBuffer();
timeBegin.append(request.getParameter("timeBegin"));
timeEnd.append(request.getParameter("timeEnd"));
timeBegin.append(":00");
timeEnd.append(":00");
if(java.sql.Date.valueOf(dateBegin).equals(java.sql.Date.valueOf(dateEnd)))
{ //起始日期等于结束日期
if(java.sql.Time.valueOf(timeBegin.toString()).equals(java.sql.Time.valueOf
(timeEnd.toString())))//时间相同
{
errors.rejectValue("timeEnd", null, "起始与结束日期相同,起始时间必须小于结束时间!");
}
if(java.sql.Time.valueOf(timeBegin.toString()).after(java.sql.Time.valueOf
(timeEnd.toString())))//时间相同
{
errors.rejectValue("timeEnd", null, "起始与结束日期相同,起始时间必须小于结束时间!");
}
}
方法二:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author hp
*/
public class test {
public static void main(String args[]) {
int i= compare_date("1995-11-12 15:21", "1999-12-11 09:59");
System.out.println("i=="+i);
}
public static int compare_date(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() > dt2.getTime()) {
System.out.println("dt1 在dt2前");
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
System.out.println("dt1在dt2后");
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
}
方法三:
比如:现在是2004-03-26 13:31:40
过去是:2004-01-02 11:30:24
我现在要获得两个日期差,差的形式为:XX天XX小时XX分XX秒
java计算时间差及比较时间大小
比如:现在是2004-03-26 13:31:40
过去是:2004-01-02 11:30:24
我现在要获得两个日期差,差的形式为:XX天XX小时XX分XX秒
一:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
Date d1 = df.parse("2004-03-26 13:31:40");
Date d2 = df.parse("2004-01-02 11:30:24");
long diff = d1.getTime() - d2.getTime();
long days = diff / (1000 * 60 * 60 * 24);
}
catch (Exception e)
{
}
二:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date now = df.parse("2004-03-26 13:31:40");
java.util.Date date=df.parse("2004-01-02 11:30:24");
long l=now.getTime()-date.getTime();
long day=l/(24*60*60*1000);
long hour=(l/(60*60*1000)-day*24);
long min=((l/(60*1000))-day*24*60-hour*60);
long s=(l/1000-day*24*60*60-hour*60*60-min*60);
System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
三:
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date begin=dfs.parse("2004-01-02 11:30:24");
java.util.Date end = dfs.parse("2004-03-26 13:31:40");
long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
long day1=between/(24*3600);
long hour1=between%(24*3600)/3600;
long minute1=between%3600/60;
long second1=between%60/60;
System.out.println(""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒");
====================================================
java 比较时间大小
String s1="2008-01-25 09:12:09";
String s2="2008-01-29 09:12:11";
java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Calendar c1=java.util.Calendar.getInstance();
java.util.Calendar c2=java.util.Calendar.getInstance();
try
{
c1.setTime(df.parse(s1));
c2.setTime(df.parse(s2));
}catch(java.text.ParseException e){
System.err.println("格式不正确");
}
int result=c1.compareTo(c2);
if(result==0)
System.out.println("c1相等c2");
else if(result<0)
System.out.println("c1小于c2");
else
System.out.println("c1大于c2");

你可能感兴趣的:(java)