判断2010-05-31 16:25:00之间2010-05-31 16:20:00相差5分钟 |
|
30分 |
装换成long型的,然后相减。
得到的是相差的毫秒数。 1000 * 60 * 5 = 5分钟是多少毫秒 两个毫秒数判断是否相等,就知道是否相差5分钟了。 |
我也知道这样可以但是具体代码不知道怎么写,好像long型相减有时候的出来的都是long型数据
根本不是毫秒数 |
|
Calendar c1 = Calendar.getInstance() ; c1.set(Calendar.YEAR,2010); c1.set(Calendar.MONTH,4); c1.set(Calendar.DATE,31); c1.set(Calendar.HOUR_OF_DAY,16); c1.set(Calendar.MINUTE,25); c1.set(Calendar.SECOND,0); Calendar c2 = Calendar.getInstance() ; c2.set(Calendar.YEAR,2010); c2.set(Calendar.MONTH,4); c2.set(Calendar.DATE,31); c2.set(Calendar.HOUR_OF_DAY,16); c2.set(Calendar.MINUTE,20); c2.set(Calendar.SECOND,0); long subTime = c1.getTime().getTime() - c2.getTime().getTime(); System.out.println(subTime); if (subTime == 1000 * 60 * 5) { System.out.println("相差5分钟"); } |
|
用 Timestamp 类型更加简单一些
|
|
给LZ的列子
import java.sql.Timestamp; public static void main(String[] args) { System.out.println(Test("2010-05-31 16:25:00.0", "2010-05-31 16:21:00.0"));//精确到毫秒 } public static boolean Test(String time1, String time2){ Timestamp t1 = Timestamp.valueOf(time1); Timestamp t2 = Timestamp.valueOf(time2); if( t1.getTime() - t2.getTime() >= 5 * 60 * 1000){ return true; } return false; } } 希望对你有帮助 |
|
对不起 上面的复制时少了public class test {
import java.sql.Timestamp; public class test { public static void main(String[] args) { System.out.println(Test("2010-05-31 16:25:00.0", "2010-05-31 16:21:00.0"));//精确到毫秒 } public static boolean Test(String time1, String time2){ Timestamp t1 = Timestamp.valueOf(time1); Timestamp t2 = Timestamp.valueOf(time2); if( t1.getTime() - t2.getTime() >= 5 * 60 * 1000){ return true; } return false; } } |