JAVA中时间大小比较 某个时间点 比如 2010-08-05 想用 系统当前时间 与 前面那个时间点24小时后 的时间进行比较 小弟不才 忘高手把代码贴给我 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 代码不在这台机器上,所以无法贴给你。。大致思路吧:获得系统的时间,用date.getTime(),返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。然后将前面的那个时间也转成用毫秒数表示的long型数据,加上24*3600*1000(一天)之后就可以比较了! SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); String str = "2010-08-09 "; //系统当前时间 Calendar cal = Calendar.getInstance(); //比较时间 Calendar cal1 = Calendar.getInstance(); cal1.setTime(sf.parse(str)); //减去1天 cal1.add(Calendar.DAY_OF_MONTH, -1); //比较 System.out.println(sf.parse(sf.format(cal.getTime())).compareTo(cal1.getTime())); public static int dateCompare(String date){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); try { cal.setTime(sdf.parse(date)); } catch (ParseException e) { e.printStackTrace(); } cal.roll(Calendar.DAY_OF_MONTH, 1); return cal.getTime().compareTo(new Date()); } public static int dateCompare(String date){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); try { cal.setTime(sdf.parse(date)); } catch (ParseException e) { e.printStackTrace(); } cal.roll(Calendar.DAY_OF_MONTH, 1); return cal.getTime().compareTo(new Date()); } import java.util.Date;import java.text.DateFormat;import java.text.ParseException;import java.util.Calendar;public class B { public static void main(String[]arg) { long i=0; String b="2010-08-05";//某个时间点 DateFormat df = DateFormat.getDateInstance(); //创建DateFormat对象初始化为系统当前值 Calendar r = Calendar.getInstance(); Calendar r1 = Calendar.getInstance();//创建Calendar对象初始化为系统当前值 try{ Date f=df.parse(b);//将字符串转化成Date形式 //System.out.print(f); r.setTime(f);//用获得的Date设置r r.add(Calendar.HOUR_OF_DAY, 24);//某时间点24小时后的时间的Calendar对象 Date t=r.getTime(); System.out.println(t); i=r1.getTimeInMillis()-r.getTimeInMillis();//进行比较,两者具体相差多少毫秒 System.out.println(i); i=r1.compareTo(r);//比较两个时间的先后r1是当前时间 r是你指定的时间 if(i==0)System.out.println("ri=r"); if(i<0)System.out.println("ri<r"); if(i>0)System.out.println("ri>r"); } catch (ParseException e){ System.out.print("失败"); } } }看上去很多 其实实际内容就一点点 运行过 你可以改成自己的类 (d1.getTime()+24*60*60*1000)-d2.getTime() 5楼的实现是正解;不过在实现“减去1天”的时候roll()方法有问题,应该用3楼的方式:cal.add(Calendar.DAY_OF_MONTH, 1);(不过第二个参数为1) /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String dateStr = format.format(date); double n = computeTime(dateStr, "2010-08-09 12:41:41"); System.out.println(n); } //比较两个时间的大小 public static double computeTime(String current, String target) { double sa; double h=0; String old = target; int old_yyyy = Integer.parseInt(old.substring(0, 4)); int old_mm = Integer.parseInt(old.substring(5, 7)); int old_dd = Integer.parseInt(old.substring(8, 10)); int old_hh = Integer.parseInt(old.substring(11, 13)); int old_mi = Integer.parseInt(old.substring(14, 16)); int old_ss = Integer.parseInt(old.substring(17, 19)); String now = current; int now_yyyy = Integer.parseInt(now.substring(0, 4)); int now_mm = Integer.parseInt(now.substring(5, 7)); int now_dd = Integer.parseInt(now.substring(8, 10)); int now_hh = Integer.parseInt(now.substring(11, 13)); int now_mi = Integer.parseInt(now.substring(14, 16)); int now_ss = Integer.parseInt(now.substring(17, 19)); GregorianCalendar gcOld = new GregorianCalendar( old_yyyy-1900,old_mm-1,old_dd,old_hh,old_mi,old_ss); GregorianCalendar gcNow = new GregorianCalendar( now_yyyy-1900,now_mm-1,now_dd,now_hh,now_mi,now_ss); gcOld.add(Calendar.DAY_OF_MONTH, 1); //24H 后 sa =(gcNow.getTimeInMillis() - gcOld.getTimeInMillis()); h = (sa/(60*60*1000)); return h; } 有没有用过StringUtil类的 java面试题 定义一个类名为“MyClass.java”的类,并且该类可被所有类访问,那么该类的正确声明应为 struts2文件上传的问题 我的classes文件夹下的*.java文件怎么都没有了 高分求个struts+hibernate+oracle实现登陆注销的代码 求J2EE高手! 怎样将一个jar文件设置为开机启动? 关于连接池的理解??这样有问题吗? 请问一个初级问题,我在手工编译一个EJB的文件时包错,如下 如何利用java程序实现.txt文本格式文件的自动入库 Oracle 数据库 十万火急~!字符截取,各位大侠,帮小女子下
大致思路吧:获得系统的时间,用date.getTime(),返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
然后将前面的那个时间也转成用毫秒数表示的long型数据,加上24*3600*1000(一天)
之后就可以比较了!
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
String str = "2010-08-09 ";
//系统当前时间
Calendar cal = Calendar.getInstance();
//比较时间
Calendar cal1 = Calendar.getInstance();
cal1.setTime(sf.parse(str));
//减去1天
cal1.add(Calendar.DAY_OF_MONTH, -1);
//比较
System.out.println(sf.parse(sf.format(cal.getTime())).compareTo(cal1.getTime()));
public static int dateCompare(String date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
try {
cal.setTime(sdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
cal.roll(Calendar.DAY_OF_MONTH, 1);
return cal.getTime().compareTo(new Date());
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
try {
cal.setTime(sdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
cal.roll(Calendar.DAY_OF_MONTH, 1);
return cal.getTime().compareTo(new Date());
}
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
public class B
{
public static void main(String[]arg)
{
long i=0;
String b="2010-08-05";//某个时间点
DateFormat df = DateFormat.getDateInstance(); //创建DateFormat对象初始化为系统当前值
Calendar r = Calendar.getInstance();
Calendar r1 = Calendar.getInstance();//创建Calendar对象初始化为系统当前值
try{
Date f=df.parse(b);//将字符串转化成Date形式
//System.out.print(f);
r.setTime(f);//用获得的Date设置r
r.add(Calendar.HOUR_OF_DAY, 24);//某时间点24小时后的时间的Calendar对象
Date t=r.getTime();
System.out.println(t);
i=r1.getTimeInMillis()-r.getTimeInMillis();//进行比较,两者具体相差多少毫秒
System.out.println(i);
i=r1.compareTo(r);//比较两个时间的先后r1是当前时间 r是你指定的时间
if(i==0)System.out.println("ri=r");
if(i<0)System.out.println("ri<r");
if(i>0)System.out.println("ri>r");
}
catch (ParseException e){
System.out.print("失败");
}
}
}看上去很多 其实实际内容就一点点 运行过 你可以改成自己的类
不过在实现“减去1天”的时候roll()方法有问题,应该用3楼的方式:
cal.add(Calendar.DAY_OF_MONTH, 1);(不过第二个参数为1)
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String dateStr = format.format(date);
double n = computeTime(dateStr, "2010-08-09 12:41:41");
System.out.println(n); }
//比较两个时间的大小 public static double computeTime(String current, String target) {
double sa;
double h=0;
String old = target;
int old_yyyy = Integer.parseInt(old.substring(0, 4));
int old_mm = Integer.parseInt(old.substring(5, 7));
int old_dd = Integer.parseInt(old.substring(8, 10));
int old_hh = Integer.parseInt(old.substring(11, 13));
int old_mi = Integer.parseInt(old.substring(14, 16));
int old_ss = Integer.parseInt(old.substring(17, 19));
String now = current;
int now_yyyy = Integer.parseInt(now.substring(0, 4));
int now_mm = Integer.parseInt(now.substring(5, 7));
int now_dd = Integer.parseInt(now.substring(8, 10));
int now_hh = Integer.parseInt(now.substring(11, 13));
int now_mi = Integer.parseInt(now.substring(14, 16));
int now_ss = Integer.parseInt(now.substring(17, 19));
GregorianCalendar gcOld = new GregorianCalendar(
old_yyyy-1900,old_mm-1,old_dd,old_hh,old_mi,old_ss);
GregorianCalendar gcNow = new GregorianCalendar(
now_yyyy-1900,now_mm-1,now_dd,now_hh,now_mi,now_ss);
gcOld.add(Calendar.DAY_OF_MONTH, 1); //24H 后 sa =(gcNow.getTimeInMillis() - gcOld.getTimeInMillis());
h = (sa/(60*60*1000));
return h;
}