simpledateformat 和 Date 去计算活了多少天

代码:

package cn.com.yin;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public class Test {
public static void main(String[] args) throws ParseException {
function();
}
public static void function() throws ParseException{
Scanner s= new Scanner(System.in);
String x = s.next();
SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = s1.parse(x);
Date date=new Date();
long time = date.getTime();
long time2 = date2.getTime();
long days=(time-time2)/1000/3600/24;
System.out.println("你一共活了"+days+"天");
}
}

猜你喜欢

转载自www.cnblogs.com/yinziqiang0909/p/10385567.html