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("請(qǐng)輸入生產(chǎn)日期");
String sc = s.nextLine();
System.out.println("請(qǐng)輸入保質(zhì)期(天)");
int bz = s.nextInt();
//字符串轉(zhuǎn)化日期格式
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
Date d = sim.parse(sc);
//獲取Calendar對(duì)象實(shí)例
Calendar c = Calendar.getInstance();
//設(shè)置Calendar對(duì)象c的時(shí)間
c.setTime(d);
//Calendar對(duì)象c的時(shí)間加上保質(zhì)期
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("您購(gòu)買的商品已過(guò)期");
} 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("您購(gòu)買的商品還有"+(k + 1)+"天過(guò)期");
}
}
}
}