1、把年月日时分秒转化为毫秒,计算两个时间段有多少毫秒
long time1 = 0;
long time2 = 0;
long time=0;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(“时间段1”);
time1 = cal.getTimeInMillis();
cal.setTime(“时间段2”);
time2 = cal.getTimeInMillis(); //以毫秒计算
time=time2-time1;
2、将毫秒转化为时分秒
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//初始化Formatter的转换格式。
formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
String hms = formatter.format(time); //time为转化的毫秒数,long类型的