编写一个程序,输出一个月的日历。
来源:互联网 发布:淘宝正品美国代购 编辑:程序博客网 时间:2024/05/17 22:25
方法一:
import java.util.Calendar;
import java.util.Scanner;
public class YueLi {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();//a表示你将要输入几个年月
sc.nextLine();
Calendar calendar = Calendar.getInstance();
for(int i = 0; i < a; i++){
String s = sc.nextLine();
String[] strs = s.split("-");
int year = Integer.parseInt(strs[0]);
int month = Integer.parseInt(strs[1]);
calendar.set(Calendar.YEAR, year); //设置年
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.DATE, 1);
//System.out.println(calendar.get(7));
for(int j =1; j < calendar.get(Calendar.DAY_OF_WEEK); j++) {
System.out.print(" ");//输出开始前面的空格
}
for(int k = 1; k <= whichMonth(year, month); k++) {
calendar.set(Calendar.DATE, k);
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
System.out.println();
if(k <= 10) { //为了输出对齐
System.out.print(k+" ");
} else {
System.out.print(k+ " ");
}
}
}
}
public static int whichMonth(int year, int month) {
int days = 0;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {//润年
days = 29;
} else {
days = 28;
}
break;
}
return days;
}
}
方法二:
import java.util.Scanner;
public class DisplayYueLi{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String input1=sc.nextLine();
String[] input2=input1.split("-");
int year=Integer.parseInt(input2[0]);
int month=Integer.parseInt(input2[1]);
int fristDay=GetfristDay(year,month);//
int mDay=GetmDay(year,month);
DisplayYueLi(fristDay,mDay);
}
static int GetfristDay(int year,int month){
int zDays=0;
for(int i=1;i<year;i++){
if(i%4==0&&i%100!=0||i%400==0){
zDays=zDays+366;
}
else{
zDays=zDays+365;
}
}
for(int j=1;j<month;j++){
switch(j){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
zDays=zDays+31;
break;
case 4:
case 6:
case 9:
case 11:
zDays=zDays+30;
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0){
zDays=zDays+29;
}
else{
zDays=zDays+28;
}
}
}
int fristDay=(int)(zDays+1)%7;
// System.out.println("fristDay=" +fristDay);
return(fristDay);
}
static int GetmDay(int year,int month){
int mDay=0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
mDay=31;
break;
case 4:
case 6:
case 9:
case 11:
mDay=30;
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0){
mDay=29;
}
else{
mDay=28;
}break;
}
return(mDay);
}
static void DisplayYueLi(int fristDay,int mDay){
String space=" ";
System.out.println(space+"日"+space+"一"+space+"二"+space+"三"+space+"四"+space+"五"+space+"六");
for(int m=0;m<fristDay;m++){
System.out.print(space+" ");
}
for(int n=1;n<=mDay;n++){
if((n+fristDay)%7==0&&n!=1){
if (n<10)
System.out.println(space+" "+n);
else
System.out.println(space+n);
}
else{
if (n<10)
System.out.print(space+" "+n);
else
System.out.print(space+n);
}
}
}
}
- 编写一个程序,输出一个月的日历。
- c语言输出一个月的日历
- 编写一个打印全年日历的程序
- 编写一个程序,输入月份号,输出该月的英文月名
- 一个显示当前月的日历的java小程序
- 编写一个程序,输出当年的月历。
- 编写一个程序,输入月份号,输出该月的中文名和英文名。
- 一个简单的日历程序
- java一个月日历
- java实现一个月的日历打印
- 用C语言编写一个输出最大值的程序
- 编写程序输出一个二进制数中1的个数
- 编写一个程序输出您的名字及地址。
- 编写一个 JAVA 程序,实现输出考试成绩的前三名
- 【practice】编写一个 JAVA 程序,实现输出考试成绩的前三名
- 输入一个时间段(开始时间,结束时间),一个参数来控制每行输出的月数,打印出日历
- java程序,编写一个方法,输出在一个字符串中,指定字符串输出的次数
- Java 实训4 编写一个窗体程序显示日历
- 使用脚本方便批量改名的应用
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解
- 色谱图,颜色值表
- c语言中逗号运算符和逗号表达式
- hdu4619
- 编写一个程序,输出一个月的日历。
- iPhone开发资源汇总(各种使用Demo)
- OpenCV 2 学习笔记(4): 加载,显示和保存图像,Mat类与IplImage类
- iOS 错误处理 (一)
- 内联函数
- 与客户沟通注意的问题
- 静态代码块和ArrayList的威力
- struts下载
- iframe submit小技巧