java简单的日历
import java.util.Scanner;
class xusong
{
//主函数入口
public static void main(String[] args)
{
System.out.println("*****请输入日期(格式如: 2011-01)*****");
Scanner sc = new Scanner(System.in);
String ym = sc.next();
//截取字符串
String[] yearMonth = ym.split("-");
//将字符串转化成整型
int year = Integer.parseInt(yearMonth [0]);
int month = Integer.parseInt(yearMonth [1]);
Year.fun(year);
Month.fun(month,year);
}
}
//获取闰年个数
class Year
{
//多少个闰年
static int leapYear = 0;
public static void fun(int year){
if(year > 1990 && year <10000){
while(year >= 1900){
if(year % 4 == 0 && year % 100 != 0 || (year % 400 ==0 )){
leapYear ++;
}
year --;
}
}else{
System.out.println("请输入查询的年份大于或等于1900年!");
}
}
}
//获取一个月多少天和总天数
class Month
{
public static void fun(int month,int year){
int monthDay = 0;
Year y = new Year();
int days = y.leapYear + (year - 1900)*365;
if(month > 0 && month <=12){
for(int i = 1;i <= month;i++){
if(i < 8 && i != 2){
days += i % 2 == 1 ? 31 :30;
monthDay = i % 2 == 1 ? 31 :30;
}
if(i > 7){
days += i % 2 == 1 ? 30 :31;
monthDay = i % 2 == 1 ? 30 :31;
}
if(i == 2){
if(year % 4 == 0 && year % 100 != 0 || (year % 400 ==0 )){
days += 28;
monthDay = 29;
}else{
days += 28;
monthDay = 28;
}
}
}
Show.mothed(days,monthDay);
}else{
System.out.println("请输入正确的月份!");
}
}
}
//显示日历
class Show
{
public static void mothed(int days, int monthDay){
//加一表示从星期一开始
int week = (days - monthDay) % 7 + 1;
if(week == 7){
week = 0;
}
System.out.println("\n周日"+"\t周一"+"\t周二"+"\t周三"+"\t周四"+"\t周五"+"\t周六");
for (int i = 1;i <= monthDay + week ;i++ )
{
if(i <= week){
System.out.print(" "+"0_0"+"\t");
}else{
if((i-week) < 10){
System.out.print(" "+(i-week)+"\t");
}else{
System.out.print((i-week)+"\t");
}
}
if(i%7 == 0){
System.out.print("\n");
}
}
}
}