下面的程序主要介绍了在JAVA里进行时间比较,获得时间差。例如如果要实现一个功能,30分钟内可以提交,超过30分钟后就不能提交了。则:
/** */
/** 计算时间差 **/
SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
String systemTime = sdf .format( new Date()).toString();
/** */ /** 将截取到的时间字符串转化为时间格式的字符串 **/
Date begin =sdf .parse( 2008 - 03 - 28 11 : 55 : 30 );
Date end = sdf.parse(systemTime);
long between = (end.getTime() - begin.getTime()) / 1000 ; // 除以1000是为了转换成秒
long day = between / ( 24 * 3600 );
long hour = between % ( 24 * 3600 ) / 3600 ;
long minute = between % 3600 / 60 ;
long second = between % 60 / 60 ;
SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
String systemTime = sdf .format( new Date()).toString();
/** */ /** 将截取到的时间字符串转化为时间格式的字符串 **/
Date begin =sdf .parse( 2008 - 03 - 28 11 : 55 : 30 );
Date end = sdf.parse(systemTime);
long between = (end.getTime() - begin.getTime()) / 1000 ; // 除以1000是为了转换成秒
long day = between / ( 24 * 3600 );
long hour = between % ( 24 * 3600 ) / 3600 ;
long minute = between % 3600 / 60 ;
long second = between % 60 / 60 ;
if((hour==0)&&(day==0)&&(minute<=30)) { /****/}