平日と自然の日を計算する
Javaでカウントダウンプログラムを書きます.例えば、日付を入力します.フォーマットは20090501で、現在の日付から20090501までの平日と自然の日を計算できます.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test1 {
public static void main(String[] args) {
String time = "20090510";
int day1 = 0;//
int day2 = 0;// ,
Calendar ca = Calendar.getInstance();
Calendar now = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try {
Date date = sdf.parse(time);
ca.setTime(date);
System.out.println(sdf.format(ca.getTime()));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String ss = sdf.format(ca.getTime());
if (now.before(ca)) {
while (!sdf.format(now.getTime()).equals(ss)) {// ca.compareTo(now)!=0
if (now.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| now.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
day2++;
}
day1++;
now.add(Calendar.DAY_OF_YEAR, 1);
}
}
System.out.println(" " + time + " " + day2 + " ");
System.out.println(" " + time + " " + day1 + " ");
}
}