直接比较就可以了
BTW: 这种问题,试试不就行了
BTW: 这种问题,试试不就行了
解决方案 »
- 追问org.apache.jasper.JasperException: /test.jsp(10,0) UseBean has invalid attribu
- 上传附件,浏览器提示“undefined”
- Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
- 电子地图的实现
- 打开新的页面时传递数组参数问题(急)
- 如何打印EXT窗口中的内容
- 如有能解决此难题着,赠送170分!!!!!!!
- 请问:那里可以找到文档编排的JSP源码?
- 高分寻求apache1.3+tomcat4配置
- 请教!
- MVC编程模式是什么意思呀?
- 高分寻找用Vector做分页显示的例子!
但是Date(String s)异步推荐使用,尽管如此你还是能用的。同时,你可以用DateFormat来试试。
DateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
Date currentTime_1 = new Date();
System.out.print(currentTime_1.getDay()); 它显示为5,但今天明明是11号还有我用时间作比较时也最是不对,
Date jj = formatter.parse("2002-11-04");
Date ff = formatter.parse("2002-09-11");
System.out.println(jj.getDay()); 它显示为1
System.out.print(jj.compareTo(ff)); 它也显示为1
你是字符串存入数据库中的吧?
不管它
先把它取出来还是字符串的下面进行转换
String mytime=rs.getString("mytime");
java.sql.Date t=java.sql.Date.valueOf(mytime);//将取出的值转化为DATE类型的
long m=t.getTime();//为时间戳,也可以不用时间戳比直接将t和当前的时间相比java.util.Date now=new java.util.Date();
long n=now.getTime();然后将m和n比较就可以了,而且还可以用(m-n)/1000*60*60*3600求出二者相差的天数
你也可以求得相差的年,天,星期
Returns the day of the week represented by this date. The returned value (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday) represents the day of the week that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.
public int compareTo(Object o) Compares this Date to another Object. If the Object is a Date, this function behaves like compareTo(Date). Otherwise, it throws a ClassCastException (as Dates are comparable only to other Dates).Specified by: compareTo in interface Comparable Parameters:o - the Object to be compared.Returns:the value 0 if the argument is a Date equal to this Date; a value less than 0 if the argument is a Date after this Date; and a value greater than 0 if the argument is a Date before this Date.
Date ff = formatter.parse("2002-09-11");
System.out.println(jj.getDay()); 它显示为1,2002-11-04星期一
System.out.print(jj.compareTo(ff)); 它也显示为1,2002-9-11before 2002-11-04
Date jj = formatter.parse("2002-11-04");
Date ff = formatter.parse("2002-09-11");
现在就求这两者之间的时间差,怎么求
我用 long m=jj.getTime();
long n=ff.getTime();
(m-n)/1000*60*60*3600求出的结果怎么那么大,能详细说明一点吗?1000是毫秒吗?3600是什么
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class Time {
Date date1,date2;
Calendar calendar1, calendar2;
public Time(){
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
try{
date1 = sdf1.parse("2002-11-04");
date2 = sdf1.parse("2002-09-11");
} catch(Exception e) {
System.out.println(e);
}
}
public long getTime(){
calendar1 = Calendar.getInstance();
calendar2 = Calendar.getInstance();
calendar1.setTime(date1);
calendar2.setTime(date2);
return calendar1.getTimeInMillis()-calendar2.getTimeInMillis();
}
}
这下你该知道了吧
long n=ff.getTime();
(m-n)/1000*60*60*3600
中不对,应该是(m-n)/1000/60/60
/1000得到秒
再/60得到分
再/60得到时
以此类推
再/24得到天
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;public class MyDate {
public MyDate() {
}
public static void main(String[] args) throws java.text.ParseException{
MyDate myDate1 = new MyDate();
Date date1=new Date();
for(int j=1000000;j>0;j--){}
Date date2=new Date();
Date today=new Date();
SimpleDateFormat df1=new SimpleDateFormat("yyyy-MM-dd");
date1=df1.parse("2003-03-23");
date2=df1.parse("2003-05-01");
long k=date2.getTime()-date1.getTime();
System.out.println(date1.toString());
System.out.println(date2);
System.out.println("相差"+k+"毫秒");
k/=1000;
System.out.println("相差"+k+"秒");
k/=60;
System.out.println("相差"+k+"分");
k/=60;
System.out.println("相差"+k+"小时");
k/=24;
System.out.println("相差"+k+"天");
System.out.println(today);
System.out.println(today +"compare to"+date1+"is"+today.compareTo(date1));
System.out.println(today +"compare to"+date1+"is"+today.compareTo(date2)); }
}
Sun Mar 23 00:00:00 CST 2003Thu May 01 00:00:00 CST 2003相差3369600000毫秒相差3369600秒相差56160分相差936小时相差39天Sun Apr 13 12:41:38 CST 2003Sun Apr 13 12:41:38 CST 2003compare toSun Mar 23 00:00:00 CST 2003is1Sun Apr 13 12:41:38 CST 2003compare toSun Mar 23 00:00:00 CST 2003is-1