A-A+

Java时间大小比较

2018年08月13日 java学习 暂无评论 阅读 次
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class Test {
 
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
	public static void main(String[] args) throws Exception {
		
		//Date1.after(Date2),当Date1大于Date2时,返回TRUE,当小于等于时,返回false; 
		//Date1.before(Date2),当Date1小于Date2时,返回TRUE,当大于等于时,返回false;
		Date startDate = sdf.parse("2016-11-01");
		Date endDate = sdf.parse("2016-11-03");
		
		Date currDate = sdf.parse("2016-11-03");
		
		
		//当前时间大于开始时间并且小于结束时间允许捐赠
		if(currDate.after(startDate) && currDate.before(endDate)){
			System.out.println("--允许捐赠---------");
		}
		
		//当前时间小于开始时间,捐赠未开始
		if(currDate.before(startDate)){
			System.out.println("--捐赠未开始---------");
		}
		
		//当前时间大于结束时间,捐赠已结束
		if(currDate.after(endDate)){
			System.out.println("--捐赠已结束---------");
		}
		
		
		System.out.println("当前时间大于开始时间true:" + currDate.after(startDate));
		System.out.println("当前时间小于结束时间true:" + currDate.before(endDate));
		
		
		Calendar calendar = Calendar.getInstance();
		Date currentDate = calendar.getTime();
		
		
		String s1 = "2016-12-01";
		String s2 = "2016-12-02";
		Calendar startTime = Calendar.getInstance();
		Calendar endTime = Calendar.getInstance();
		startTime.setTime(sdf.parse(s1));
		startTime.set(Calendar.HOUR_OF_DAY, 0);
		startTime.set(Calendar.SECOND,0);
		startTime.set(Calendar.MINUTE,0);
		System.out.println("startTime : " + sdf1.format(startTime.getTime()));
		endTime.setTime(sdf.parse(s2));
		endTime.set(Calendar.HOUR_OF_DAY, 23);
		endTime.set(Calendar.SECOND,59);
		endTime.set(Calendar.MINUTE,59);
		System.out.println("endTime : " + sdf1.format(endTime.getTime()));
		Calendar current = Calendar.getInstance();
		System.out.println("current : " + sdf1.format(current.getTime()));
//		current.setTime(sdf.parse("2016-12-02"));
		//0-等于,1-大于,-1小于
		//当前时间大于开始时间并且小于结束时间允许捐赠
		System.out.println(current.compareTo(startTime));
		System.out.println(current.compareTo(endTime));
		if((current.compareTo(startTime) == 1 || current.compareTo(startTime) == 0) && ( current.compareTo(endTime) == -1 || current.compareTo(endTime) == 0)){
			System.out.println("--允许捐赠---------");
		}
		
		//当前时间小于开始时间,捐赠未开始
		if(current.compareTo(startTime) == -1){
			System.out.println("--捐赠未开始---------");
		}
		
		//当前时间大于结束时间,捐赠已结束
		if(current.compareTo(endTime) == 1){
			System.out.println("--捐赠已结束---------");
		}
		
	}
 
 
}

 

平面模特伊伊Eva美少女装扮写真

给我留言