基本算法思维——时间差

问题:求未来某一天距离2015-10-18的具体天数

解决:转换为毫秒数进行计算

code:

 1 public class Main{
 2     public static void main(String args[]) throws ParseException {
 3         Scanner s = new Scanner(System.in);
 4         //一天 = 24小时*60分*60秒*1000(毫秒)
 5         long day_time = 86400000L;
 6         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 7         Date cur_date = format.parse("2015-10-18");
       //获得2015-10-18所对应的毫秒数
8 long start_time = cur_date.getTime(); 9 String str = s.nextLine(); 10 Date future_date = format.parse(str); 11 long end_time = future_date.getTime(); 12 long day = (end_time-start_time)/day_time; 13 System.out.println(day); 14 } 15 }
原文地址:https://www.cnblogs.com/dream-flying/p/12780598.html