2018-12-17
1.输入日期格式字符串作为商品的生产日期,输入保质期(天);计算截止到
今天,该商品还有多少天会过保质期
1、控制台输入字符串转为日期格式
2、将输入日期增加保质期天数
3、取得当前系统日期,比较两个日期,如果过期输出“该商品已经过期
Scanner imput = new Scanner(System.in);
System.out.println(\"请按所示格式输入日期(YYYY-MM-DD):\");
String str = imput.nextLine();
System.out.println(\"请输入保质期:\");
int i = imput.nextInt();
SimpleDateFormat f = new SimpleDateFormat(\"yyyy-MM-dd\");
Calendar c = Calendar.getInstance();
c.setTime(f.parse(str));
c.add(Calendar.DAY_OF_MONTH, i);
Date date = new Date();
Calendar b = Calendar.getInstance();
System.out.println(\"今日日期为\" + f.format(date));
if (date.after(c.getTime()) == true) {
System.out.println(\"该产品已过期\");
} else {
System.out.println(\"该产品在保质期内\");
int x = c.get(Calendar.DAY_OF_MONTH) - b.get(Calendar.DAY_OF_MONTH);
System.out.println(\"保质期还有\" + x + \"天\");
}
System.out.println(\"保质期至\" + f.format(c.getTime()));
继续阅读与本文标签相同的文章
-
数字技术让“诗和远方”融为一体
2026-05-18栏目: 教程
-
“成人网站”免费让人上钩,其背后有什么不为人知的猫腻,网友:不敢惹
2026-05-18栏目: 教程
-
网络互连技术之路由协议
2026-05-18栏目: 教程
-
信息技术助力物流行业转型升级
2026-05-18栏目: 教程
-
登榜!风采依旧表现“佳”
2026-05-18栏目: 教程
