java 这个是输入生日然后算出 从生日到现在 活了多少天 求代码
答案:3 悬赏:50 手机版
解决时间 2021-04-08 10:04
- 提问者网友:末路
- 2021-04-07 11:50
java 这个是输入生日然后算出 从生日到现在 活了多少天 求代码
最佳答案
- 五星知识达人网友:荒野風
- 2021-04-07 12:55
输入年龄 输出在世界活了多少天
全部回答
- 1楼网友:蓝房子
- 2021-04-07 14:31
Scanner scanner = new Scanner(System.in);
System.out.println("输入您的生日:格式为yyyy-MM-dd");
String day = scanner.next();
String sYear = day.substring(0, 4);
int year = Integer.valueOf(sYear);
String sMonth = day.substring(5, 7);
int month = Integer.valueOf(sMonth);
String sDay = day.substring(8, 10);
int dday = Integer.valueOf(sDay);
//生日
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, dday);
//今天
Calendar calendar = Calendar.getInstance();
long time1 = cal.getTimeInMillis();
long time2 = calendar.getTimeInMillis();
//两个日期相隔的毫秒数
long between_days = (time2 - time1) / (1000 * 3600 * 24);
//两个日期相隔的天数
int days = Integer.valueOf(String.valueOf(between_days));
System.out.println("到今天,您共活了" + days + "天!");
System.out.println("输入您的生日:格式为yyyy-MM-dd");
String day = scanner.next();
String sYear = day.substring(0, 4);
int year = Integer.valueOf(sYear);
String sMonth = day.substring(5, 7);
int month = Integer.valueOf(sMonth);
String sDay = day.substring(8, 10);
int dday = Integer.valueOf(sDay);
//生日
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, dday);
//今天
Calendar calendar = Calendar.getInstance();
long time1 = cal.getTimeInMillis();
long time2 = calendar.getTimeInMillis();
//两个日期相隔的毫秒数
long between_days = (time2 - time1) / (1000 * 3600 * 24);
//两个日期相隔的天数
int days = Integer.valueOf(String.valueOf(between_days));
System.out.println("到今天,您共活了" + days + "天!");
- 2楼网友:woshuo
- 2021-04-07 13:12
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class WorkDemo {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Scanner sc = new Scanner(System.in);
System.out.print("请输入你的生日, 输入格式为:yyyy-MM-dd");
String birthday = sc.nextLine();
Date birDate = sdf.parse(birthday);
Date now = new Date();
long time = now .getTime() - birDate.getTime();
long day = time/1000/60/60/24;
System.out.println("到今天经历了:"+day+"天");
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class WorkDemo {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Scanner sc = new Scanner(System.in);
System.out.print("请输入你的生日, 输入格式为:yyyy-MM-dd");
String birthday = sc.nextLine();
Date birDate = sdf.parse(birthday);
Date now = new Date();
long time = now .getTime() - birDate.getTime();
long day = time/1000/60/60/24;
System.out.println("到今天经历了:"+day+"天");
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯