zoukankan      html  css  js  c++  java
  • java 实现万年历

    package com.direct.str;
    
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.Scanner;
    
    public class CalendarOne {
    
    	/**
    	 * @param args
    	 */
    	public static void showCalendar(int year,int month){
    		String[]  weekString = {"日","	","一","	","二","	","三","	","四","	","五","	","六"};
    		for (int i = 0; i <weekString.length ; i++) {
    			System.out.print(weekString[i]);
    		}
    		System.out.println();
    		Calendar c = new GregorianCalendar();
    		c.set(Calendar.YEAR, year);
    		c.set(Calendar.MONTH, month-1);
    		c.set(Calendar.DATE,1);
    		int count = c.getActualMaximum(c.DAY_OF_MONTH);//得到当月的天数
    		int day_week = c.get(Calendar.DAY_OF_WEEK)-1;
    		for(int i=0;i<day_week;i++){
    			System.out.print("	");
    		}
    		for(int j=1;j<=count;j++,day_week++){
    			if((day_week)%7==0){
    				System.out.println();
    			}
    			System.out.print(j+"	");
    		}
    		
    	}
    	
    	public static void main(String[] args) {
    		System.out.println("<<<<<<<<万年历>>>>>>>>");
    		Scanner sc = new Scanner(System.in);
    		System.out.println("请输入年份:");
    		int year = sc.nextInt();
    		System.out.println("请输入月份:");
    		int month = sc.nextInt();
    		System.out.println("-------------"+year+"年"+month+"月------------");
    		CalendarOne.showCalendar(year,month);
    
    	}
    
    }
    

      

    package com.direct.str;
    
    public class CalendarTwo {
    
    	public static void main(String[] args) {
    		
    		int year = 2017;
    		int month = 12;
    		int totalDay = 0;
    		//天数计算从1900开始,毫秒数型1970年开始
    		for (int i = 1900; i < year; i++) {
    			if(i%4==0 && i%100!=0 || i%400==0){
    				totalDay+=366;
    			}else{
    				totalDay+=365;
    			}
    		}
    		int dayM = 0;
    		for (int i = 1; i <= month; i++) {
    			switch (i) {
    			case 1:
    			case 3:
    			case 5:
    			case 7:
    			case 8:
    			case 10:
    			case 12:
    				dayM = 31;
    				totalDay+=dayM;
    				break;
    			case 4:
    			case 6:
    			case 9:
    			case 11:
    				dayM = 30;
    				break;
    			case 2:
    				if(year%4==0 && year%100!=0 || year%400==0){
    					dayM = 29;
    				}else{
    					dayM = 28;
    				}
    				break;
    			default:
    				System.out.println("月份有错");
    				break;
    			}
    			if(i<month){
    				totalDay+=dayM;
    			}
    			
    		}
    		int temp = totalDay%7;
    		int firstDayofMonth = 0;
    		if(temp==7){
    			firstDayofMonth = 0;
    		}else {
    			firstDayofMonth = temp;
    		}
    		System.out.println("日	一	二	三	四	五	六	");
    		for(int i=0;i<firstDayofMonth;i++){
    			System.out.print("	");
    		}
    		for(int i=1;i<= dayM;i++){
    			System.out.print(i+"	");
    			if((totalDay+i)%7==0){
    				System.out.println();
    			}
    		}
    
    		
    		
    	}
    }
    

      

  • 相关阅读:
    Java基础面试题总结-编程题总结
    Linux下mysql的安装与卸载并且连接navicat详解(亲测可用)
    linux 下安装redis
    linux 下安装tomcat
    Linux系统下安装jdk及环境配置(两种方法)
    Servlet概述
    多线程之volatile关键字
    多线程之ThreadLocal
    多线程之synchronized实现原理
    线程池2
  • 原文地址:https://www.cnblogs.com/nn369/p/8037897.html
Copyright © 2011-2022 走看看