<small>
1豆混、促銷日代碼
用戶輸入商品生產(chǎn)日期和保質(zhì)期篓像,通過程序計(jì)算促銷日期。計(jì)算規(guī)則為:到保質(zhì)期前14天所在周的周三為促銷日皿伺≡北纾控制臺(tái)交互情況如圖-1所示。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class Deline {
public static void main(String[] args) throws ParseException{
Calendar cal=Calendar.getInstance();
System.out.println("請(qǐng)輸入生產(chǎn)日期(yyyy-MM-dd):");
Scanner sc=new Scanner(System.in);
String str= sc.nextLine();
System.out.println("請(qǐng)輸入保質(zhì)期:");
int day=sc.nextInt();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date bothDate=sdf.parse(str);
cal.setTime(bothDate);
cal.add(Calendar.DATE,day-14);
cal.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);
System.out.println("促銷日為:"+sdf.format(cal.getTime()));
}
}