Android - 比较日期的时间部分


1. get current time
// current time 
Calendar now = Calendar.getInstance();
                        int hour = now.get(Calendar.HOUR);
                        int minute = now.get(Calendar.MINUTE);
                        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                        Date nowTime = sdf.parse(hour + ":" + minute);
2.get time to compare
// time to compare 
private Date toTime(String name){
            try{
                String timeStr = name.split("-")[1];
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                Date time = sdf.parse(timeStr);
                return time;
            }
            catch(ParseException ex){
                return null;
            }
        }
Date time = toTime(operationName);


3. compare
// compare currentTime with the time string
if(time.after(nowTime)){
	//logic 
}









你可能感兴趣的:(android)