输入年份,然后打印出该年的万年历,以及标识出当天日期。类似于linux下的cal -y结果。
2015-07-14 15:42
555 查看
public class Permanent {
public static boolean isLeapYear(int year){//能被4整除但不能被100整除,或者能被400整除
boolean leapYear = false;
if((year % 100 == 0 && year % 400 == 0)||(year % 100 != 0 && year % 4 == 0)){
leapYear = true;
}
return leapYear;
}
public static int countDays(int year){//选个基准2015年1月1日,星期四
int countDays = 0;
int beginYear =year > 2015 ? 2015 : year;
int endYear = year > 2015 ? year : 2015;
for(int i = beginYear;i < endYear;i++ ){
if(isLeapYear(i)){
countDays += 366;
}else{
countDays += 365;
}
}
return countDays;
}
public static void showCaledar(int year){
int days = countDays(year);
int weekDay = days % 7;
if(year > 2015){
weekDay = (weekDay + 4) % 7;
}else{
weekDay = (11 -weekDay) % 7;
}
String[] monthLabels = new String[]{"January","February","March","April","May","June","July","August","September","October","November","December"};
String[] weekLabels = new String[]{"Sun","Mon","Tur","Wen","Thr","Fra","Sat"};
int[] monthDay = {31,28,31,30,31,30,31,31,30,31,30,31};
for(int i = 0;i < 12;i++){
System.out.println("\n"+monthLabels[i]);
for(String weekLabel : weekLabels){ //每一周的标签
System.out.print(weekLabel + " ");
}
System.out.println(); //下一行
for(int j = 0; j < weekDay; j++){//找到第一个日期
System.out.print(" ");
}
if(isLeapYear(year)){
monthDay[1] = 29;
}else{
monthDay[1] = 28;
}
for(int k = 1;k <= monthDay[i];k++){
if((k + weekDay - 1) % 7 == 0){
System.out.println();
}
if(k < 10){ //这里是对齐的问题
System.out.print(k + " ");
}else{
System.out.print(k + " ");
}
}
weekDay = (weekDay + monthDay[i]) % 7;
}
}
public static void main(String[] args) {
showCaledar(2014);
}
}
public static boolean isLeapYear(int year){//能被4整除但不能被100整除,或者能被400整除
boolean leapYear = false;
if((year % 100 == 0 && year % 400 == 0)||(year % 100 != 0 && year % 4 == 0)){
leapYear = true;
}
return leapYear;
}
public static int countDays(int year){//选个基准2015年1月1日,星期四
int countDays = 0;
int beginYear =year > 2015 ? 2015 : year;
int endYear = year > 2015 ? year : 2015;
for(int i = beginYear;i < endYear;i++ ){
if(isLeapYear(i)){
countDays += 366;
}else{
countDays += 365;
}
}
return countDays;
}
public static void showCaledar(int year){
int days = countDays(year);
int weekDay = days % 7;
if(year > 2015){
weekDay = (weekDay + 4) % 7;
}else{
weekDay = (11 -weekDay) % 7;
}
String[] monthLabels = new String[]{"January","February","March","April","May","June","July","August","September","October","November","December"};
String[] weekLabels = new String[]{"Sun","Mon","Tur","Wen","Thr","Fra","Sat"};
int[] monthDay = {31,28,31,30,31,30,31,31,30,31,30,31};
for(int i = 0;i < 12;i++){
System.out.println("\n"+monthLabels[i]);
for(String weekLabel : weekLabels){ //每一周的标签
System.out.print(weekLabel + " ");
}
System.out.println(); //下一行
for(int j = 0; j < weekDay; j++){//找到第一个日期
System.out.print(" ");
}
if(isLeapYear(year)){
monthDay[1] = 29;
}else{
monthDay[1] = 28;
}
for(int k = 1;k <= monthDay[i];k++){
if((k + weekDay - 1) % 7 == 0){
System.out.println();
}
if(k < 10){ //这里是对齐的问题
System.out.print(k + " ");
}else{
System.out.print(k + " ");
}
}
weekDay = (weekDay + monthDay[i]) % 7;
}
}
public static void main(String[] args) {
showCaledar(2014);
}
}
相关文章推荐
- Linux系统脚本的三种执行方式
- Linux系统挂载U盘的方法
- Linux上的常用软件
- linux的stat函数
- Linux系统iNode耗尽硬盘无法写入文件怎么办?
- 树莓派linux内核编译与固件升级
- Linux下ln链接命令详解
- CentOS修改ulimit(最大进程数和最大文件打开数)
- Linux下select函数的使用
- Linux 系统实现源地址路由
- linux命令学习DAY.3
- linux中svn命令集
- Linux测试标准------stress压力测试
- centos重置密码
- Linux下高频命令分类辑录(基本使用篇)
- Linux之变量引用与命令替换
- Linux之文件加密解密
- Linux的10个彩蛋
- Centos允许root远程登录设置
- Linux常见的命令