sample53.java
import java.io.*;
public class sample53{
public static void main(String[] args) throws Exception{
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("Year?: ");
String str1 = in.readLine();
System.out.print("Month?: ");
String str2 = in.readLine();
int year = Integer.parseInt(str1);
int month = Integer.parseInt(str2);
int dow = dayofweek(year, month);
int dim = daysinmonth(year, month);
showcal(dow, dim);
}
// カレンダーを表示する
public static void showcal(int dow, int days) throws Exception{
System.out.println("Su Mo Tu We Th Fr Sa");
int d = 1;
int i;
// 最初の週の表示
for(i = 0; i < dow; i++){
System.out.print(" ");
}
for(; i < 7; i++){
System.out.print(" " + d + " ");
d++;
}
System.out.println();
// 二週目以降の表示
for(int j = 0; d <= days; j++){
for(i = 0; i < 7 && d <= days; i++){
if(d < 10)
System.out.print(" " + d + " ");
else
System.out.print(d + " ");
d++;
}
System.out.println();
}
}
// 月の日数をかえす
public static int daysinmonth(int year, int month) throws Exception{
int dim = 31;
if(month == 4 || month == 6 || month == 9 || month == 11)
dim = 30;
if(month == 2){
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
dim = 29; // うるう年
else
dim = 28;
}
return dim;
}
// 年の日数をかえす
public static int daysinyear(int year) throws Exception{
int diy;
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
diy = 366; // うるう年
else
diy = 365;
return diy;
}
// 月の初日の曜日をかえす
public static int dayofweek(int year, int month) throws Exception{
int days = 0;
int y = 0, m = 0;
int dow = 0;
// 2000年以降
if(year >= 2000){
for(y = 2000; y < year; y++){
days = days + daysinyear(y);
}
for(m = 1; m < month; m++){
days = days + daysinmonth(year, m);
}
dow = (days + 6) % 7;
}
// 1999年以前
else{
for(m = month; m <= 12; m++){
days = days + daysinmonth(year, m);
}
for(y = year + 1; y < 2000; y++){
days = days + daysinyear(y);
}
dow = 6 - days % 7;
}
return dow;
}
}
day_of_week.java
import java.util.Date;
import java.util.Calendar;
class day_of_week{
String yo(int b_yy, int b_mm , int b_dd){
Date today = new Date();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, b_yy);
cal.set(Calendar.MONTH, b_mm-1);
cal.set(Calendar.DATE,b_dd);
int h = cal.get(Calendar.DAY_OF_WEEK);
String yo ="";
switch(h){
case 1: yo = "日"; break;
case 2: yo = "月"; break;
case 3: yo = "火"; break;
case 4: yo = "水"; break;
case 5: yo = "木"; break;
case 6: yo = "金"; break;
case 7: yo = "土"; break;
}
return yo;
}
}
leap_year.java
class leap_year{
int dim;
int uruudosi(int yy);
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
dim = 29; // うるう年
else
dim = 28;
}
return dim;
}
cmdYear?: 2015 Month?: 2 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
よろしくお願いいたします。
daysinmonth
が流用できるから必要無いような気がします。