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();
                }
            
            }
        }
    }

  • 相关阅读:
    安卓开发环境搭建
    css3实现漂亮的按钮链接
    mouseover与mouseenter的区别
    踩了一个在body上添加多个代理事件的坑
    javascript与生活:papago行车记录仪播放器golife无卫星图修复方法
    grunt入门之windows下搭建一个最基本的grunt项目
    一个简单的跨浏览器的弹出窗口的实现
    如何查询mysql中执行效率低的sql语句
    用PHP读写音频文件的信息(支持WMA和MP3)
    PHP功能类[获取客户端IP、页面跳转]
  • 原文地址:https://www.cnblogs.com/z-jun/p/6104656.html
Copyright © 2011-2022 走看看