怎么用JAVA编个日历输入指定年月后显示出来
答案:2 悬赏:70
解决时间 2021-04-21 11:05
- 提问者网友:冥界祭月
- 2021-04-20 18:43
怎么用JAVA编个日历输入指定年月后显示出来
最佳答案
- 二级知识专家网友:桃花别处起长歌
- 2021-04-20 19:37
public static void main(String[] args) {
//日历类Calendar
//获取Calendar日历对象
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR); //获取指定的日历字段的值
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
System.out.print(year + "年");
System.out.print(month + "月");
System.out.print( date + "日");
//日历类Calendar
//获取Calendar日历对象
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR); //获取指定的日历字段的值
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
System.out.print(year + "年");
System.out.print(month + "月");
System.out.print( date + "日");
全部回答
- 1楼网友:气场征服一切
- 2021-04-20 20:44
public class calendar {
public static boolean isbissextile(int year) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
return true;
}
return false;
}
public static int daysofmonthinyear(int month, int year) {
int months[] = {31,29,31,30,31,30,31,31,30,31,30,31};
//判断是否是闰年,闰年2月有29 天
if (isbissextile(year)) {
months[1] = 29;
} else {
months[1] = 28;
}
return months[month - 1];
}
public static int daysfromnovecento(int day, int month, int year) {
//接收天数差值
int dayssum = 0;
//将距离1900年的整年天数相加
for (int i = 1900; i < year; i++) {
//是闰年则为366天不是为365天
dayssum += isbissextile(i) ? 366 : 365;
}
//计算当年距离1月的整月天数
for (int j = 1; j < month; j++){
dayssum += daysofmonthinyear(j, year);
}
//加上当月天数
dayssum += day;
return dayssum;
}
public static int whichweek(int day, int month, int year){
//1900.1.1是星期一,取余得1234560分别代表星期一到星期天
return daysfromnovecento(day, month, year) % 7;
}
public static void printmonthofyear(int month, int year){
string []weeks = {"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
//打印最上面的星期标记
for (int i = 0; i < weeks.length; i++) {
system.out.print(weeks[i] + "\t");
}
system.out.println();
//计数器
int count = 0;
//打印当月日期前的水平制表 判断当月1号是星期几就打印几个(0表示星期天)
for (int j = 0; j < whichweek(1, month, year); j++) {
system.out.print("\t");
count++;
}
//循环次数为当月天数
for (int i = 1; i <= daysofmonthinyear(month, year); i++) {
system.out.print(i + "\t");
count++;
//每打印7个打印换行
if (count % 7 == 0) {
system.out.println();
}
}
//当所有日期打印完毕若最后一个打印后没有换行打印换行美化显示
if (count % 7 !=0) {
system.out.println();
}
system.out.println();
}
public static void printyear(int year){
system.out.println("\t\t\t" + year + "年");
//循环12个月 每次调用打印月份方法
for (int i = 1; i <= 12; i++) {
system.out.println("\t\t\t" + i + "月");
printmonthofyear(i, year);
}
}
//main方法 --- 测试
public static void main(string []args) {
system.out.println(daysfromnovecento(30, 10, 2015));
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯