zoukankan      html  css  js  c++  java
  • 运用Date日期来做日历

    import java.util.*;
    import java.text.*;

    class Two
    {
        public static void main(String[] args)
        {
            System.out.println("*****请输入日期(格式:2016-01)*****");
            Scanner sc = new Scanner(System.in);
            String scStr = sc.next();

    //****** 提取星期 年  月
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date date = new Date();
            try{
                date = sdf.parse(scStr); //输入的字符串转换成日期
            }catch(ParseException e){
            }
            SimpleDateFormat sdfE = new SimpleDateFormat("E");
            String dE = sdfE.format(date); //输入的月份1号 是星期几
            SimpleDateFormat sdfY = new SimpleDateFormat("yyyy");
            String dY = sdfY.format(date);  //提取年
            SimpleDateFormat sdfM = new SimpleDateFormat("MM");
            String dM = sdfM.format(date);  //提取月

    //******  将 年 月转换为数字
            int month = 0;    //接收字符串转数字
            int year = 0;
            try{  //捕捉异常处理
                year = Integer.parseInt(dY);
                month = Integer.parseInt(dM);
            }catch(NumberFormatException e){}

            System.out.println("星期日 星期一 星期二 星期三 星期四 星期五 星期六");

            switch(month){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                    calendar(dE,31);
                    break;
                    // 30天
                case 4:
                case 6:
                case 11:
                    calendar(dE,30);
                    break;
                    // 28 或 29天的
                case 2:
                    if(year%4==0 && year%100!=0){
                        calendar(dE,29);
                    }else {
                        calendar(dE,28);
                    }
                break;
                default:
                    break;
                
            }

        }

        public static void calendar(String e,int n){
            int a = 0;
            if(e.equals("星期日")){
                a = 0;
            } else if(e.equals("星期一")){
                a = 1;
            }else if(e.equals("星期二")){
                a = 2;
            }else if(e.equals("星期三")){
                a = 3;
            }else if(e.equals("星期四")){
                a = 4;
            }else if(e.equals("星期五")){
                a = 5;
            }else if(e.equals("星期六")){
                a = 6;
            }


            for(int k = 1; k <= a+n; k++){
                if(k<=a){
                    System.out.print("* ");
                }else {
                    System.out.print(k-a+" ");
                }
                if(k%7==0){
                    System.out.println();
                }
            
            }
        }
    }

  • 相关阅读:
    [kuangbin带你飞]专题十二 基础DP1
    bits/stdc++.h
    第七届 山东省ACM Execution of Paladin(水题)
    poj 1523 SPF【点双连通求去掉割点后bcc个数】
    hdoj 5112 A Curious Matt
    【转】我,一个写代码的
    poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
    数据结构第二次上机实验【链表实现多项式的加法和乘法】
    hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
    hdoj 3849 By Recognizing These Guys, We Find Social Networks Useful【双连通分量求桥&&输出桥&&字符串处理】
  • 原文地址:https://www.cnblogs.com/z-jun/p/6104656.html
Copyright © 2011-2022 走看看