Java比较两个时间大小方法记录

@GetMapping("/compare/time/{start}/{end}")
public static Integer compareTime(@PathVariable String start, @PathVariable String end) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");//这个地方时间规格可根据自己的需求修改
    long result = sdf.parse(start).getTime() - sdf.parse(end).getTime();
    return result < 0L?Integer.valueOf(-1):(result == 0L?Integer.valueOf(0):Integer.valueOf(1));
}
 
  
 
  
实验使用结果:http://localhsot:8888/api/compare/time/10:00:00/11:00:00
返回结果:
-1

实验使用结果:http://localhsot:8888/api/compare/time/10:00:00/09:00:00
返回结果:
1
 
  
 
  
实验使用结果:http://localhsot:8888/api/compare/time/10:00:00/10:00:00
返回结果:
0

 
  
 
  
 
  
 
  

你可能感兴趣的:(Java)