public class test2 {
public static void main(String[] args)throws ParseException {
// TODO Auto-generated method stub
method1();
}
public static void method1() throws ParseException{
Scanner s = new Scanner(System.in);
System.out.println("请输入生产日期");
String sc = s.nextLine();
System.out.println("请输入保质期(天)");
int bz = s.nextInt();
//字符串转化日期格式
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
Date d = sim.parse(sc);
//获取Calendar对象实例
Calendar c = Calendar.getInstance();
//设置Calendar对象c的时间
c.setTime(d);
//Calendar对象c的时间加上保质期
c.add(Calendar.DAY_OF_MONTH, bz);
String h = sim.format(c.getTime());
Date o = sim.parse(h);
Date to = new Date();
String T = sim.format(to);
System.out.println();
c.add(Calendar.YEAR, -1900);
System.out.println("今天日期为" + T);
boolean b = o.before(to);
if (b == true) {
System.out.println("您购买的商品已过期");
} else {
Date d2 = new Date();
c.setTime(o);
c.add(Calendar.DAY_OF_MONTH, -d2.getDate());
c.getTime();
long k = (o.getTime() - d2.getTime()) / 86400000;
System.out.println("您购买的商品还有"+(k + 1)+"天过期");
}
}
}
}