public class Test { public static void main(String[] args)throws Exception { //天天上班時間 8:30 String workDateStr="2018-07-01 8:30:00"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date workDate = dateFormat.parse(workDateStr); //獲取當前時間 String nowDateStr="2018-07-01 12:01:00"; Date nowDate = dateFormat.parse(nowDateStr); long time = nowDate.getTime()- workDate.getTime(); System.out.println("time="+time); int hour = (int) (time/1000/60/60); System.out.println(hour); } }
輸出:java
time=-30540000 -8
發現不對呀!!!時間怎麼是負數???spa
一萬個草泥馬奔騰而來!!!code
後來發現orm
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");get
hh:表明12小時制io
HH:表明24小時制class