日期比较问题(急)!
日期是14位的
如:
开始时间
20070523111700
结束时间
20070531180000
如何能知道
结束时间-开始时间=差几天
谢谢各位
------解决方案--------------------给你个例子,看看就明白了
long endTime = 0;
long beginTime = 0;
Date begin_time = new Date();
Date end_time = new Date();
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss ");
try {
begin_time = sdf.parse( "2007-02-10 10:10:10 ");//m_begintime=txt_begintime.getText();开始的时间
end_time = sdf.parse( "2007-02-10 10:10:11 ");//m_endtime=txt_endtiem.getText();结束的时间
endTime = end_time.getTime();
beginTime = begin_time.getTime();
} catch (Exception e) {
}
int result = new Long(endTime - beginTime).intValue();
System.out.println(result);
------解决方案--------------------用SimpleFormatDate转成Date减,得到毫秒,然后再换算成天
------解决方案--------------------import java.util.*;
import java.text.*;
public class DateTest
{
public static void main(String[] args) throws Exception
{
String startDate = "20070523111700 ";
String endDate = "20070531180000 ";
SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss ");
Date currDate = new Date();
System.out.println(sdf.format(currDate));
Date sDate = sdf.parse(startDate);
Date eDate = sdf.parse(endDate);
System.out.println((eDate.getTime() - sDate.getTime()) / (1000 * 60 * 60 * 24));
}
}
------解决方案--------------------SimpleDateFormat smdf = new SimpleDateFormat( "yyyyMMddhhmmss ");
try {
Date start = smdf.parse( "20070523111700 ");
Date end = smdf.parse( "20070531180000 ");
long t = (end.getTime() - start.getTime()) / (3600 * 24 * 1000);
System.out.println(t);
} catch (
ParseException e) {
e.printStackTrace();
}
------解决方案--------------------String start_time = "20070523231700 ";
String end_time = "20070532180000 ";
long start = 0;
long end = 0;
SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddhhmmss ");
try {
start = sdf.parse(start_time).getTime();
end = sdf.parse(end_time).getTime();
} catch(Exception ex) {
ex.printStackTrace();
}
int subdays = (int)((end - start)/24/1000/60/60);
System.out.println( "subdays = " + subdays );
------解决方案--------------------/**
*
*/
import
java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author liaojy 下午03:12:24
*/
public class Test {
public static long dataDiff(Date a, Date b) {
return (a.getTime() - b.getTime()) / 86400000;
}
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
System.out.print(Math.abs(dataDiff(new SimpleDateFormat( "yyyyMMddmmss ").parse( "200703041232 "), new SimpleDateFormat( "yyyyMMddmmss ").parse( "200703090000 ")))+ "\n ");