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