Java实现按年月打印日历功能【基于Calendar】

 更新时间:2018年03月13日 15:06:19   作者:lizonghuan  
这篇文章主要介绍了Java实现按年月打印日历功能,涉及java基于Calendar进行日期运算的相关操作技巧,需要的朋友可以参考下

本文实例讲述了Java实现按年月打印日历功能。分享给大家供大家参考,具体如下:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalendarBook {
  public static void main(String[] args) throws ParseException {
    CalendarBook cb = new CalendarBook();
    cb.printWeekTitle();
    cb.printCalendar(2018, 3);
  }
  public void printCalendar(int year, int month) throws ParseException {
    String monthStr; // 格式化月份,因为要转成yyyyMMdd格式的
    if (month < 10) {
      monthStr = "0" + month;
    } else {
      monthStr = month + ""; // 数字跟字符串拼接转成字符串格式
    }
    String yearMonthStr = year + monthStr;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Calendar calendarEnd = Calendar.getInstance();
    Calendar calendarStart = Calendar.getInstance();
    // 根据年份和月份得到输入月份有多少天
    int monthDays = getMonthLastDay(year, month);
    // 月初的date字符串
    String dateStartStr = yearMonthStr + "01";
    // 月末的date字符串
    String dateEndStr = yearMonthStr + monthDays;
    Date startDate = sdf.parse(dateStartStr);
    Date endDate = sdf.parse(dateEndStr);
    calendarStart.setTime(startDate);
    calendarEnd.setTime(endDate);
    // 得到输入月份有多少周
    int weeks = calendarEnd.get(Calendar.WEEK_OF_MONTH);
    // 得到当月第一天是星期几,这里周日为第一天,从1开始,周一则为2
    int dayOfWeek = calendarStart.get(Calendar.DAY_OF_WEEK);
    int day = 1;
    // 当月的第一周做特殊处理,单独打印一行
    for (int i = 1; i <= 7; i++) {
      if (i >= dayOfWeek) {
        System.out.print(" " + day + " ");
        day++;
      } else {
        System.out.print("  ");
      }
    }
    System.out.println();
    // 开始打印从第二周开始的日期
    for (int week = 1; week < weeks; week++) {
      for (int i = 1; i <= 7; i++) {
        if (day > monthDays) {
          break;
        }
        if (day < 10) {
          System.out.print(" " + day + " ");
        } else {
          System.out.print(day + " ");
        }
        day++;
      }
      System.out.println();
    }
  }
  public int getMonthLastDay(int year, int month) {
    int monthDay;
    int[][] day = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
        { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
      // 闰年
      monthDay = day[1][month];
    } else {
      monthDay = day[0][month];
    }
    return monthDay;
  }
  public void printWeekTitle() {
    System.out.println("日" + "   " + "一" + "   " + "二" + "   " + "三"
        + "   " + "四" + "   " + "五" + "   " + "六");
  }
}

运行结果截图(运行效果,字体大小5号最佳):

PS:这里再为大家推荐几款关于日期与时间计算的在线工具供大家参考使用:

在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在线万年历日历:
http://tools.jb51.net/bianmin/wannianli

在线阴历/阳历转换工具:
http://tools.jb51.net/bianmin/yinli2yangli

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

更多关于java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

相关文章

  • JVM内存模型知识点总结

    JVM内存模型知识点总结

    在本篇文章里小编给大家分享了关于JVM内存模型的学习心得以及相关知识点总结,有兴趣的朋友们跟着学习下。
    2019-05-05
  • @RequestMapping对不同参数的接收方式示例详解

    @RequestMapping对不同参数的接收方式示例详解

    Spring MVC框架中,@RequestMapping注解用于映射URL到控制器方法,不同的参数类型如简单参数、实体参数、数组参数、集合参数、日期参数和JSON参数,本文给大家介绍@RequestMapping对不同参数的接收方式,感兴趣的朋友一起看看吧
    2024-10-10
  • SpringBoot @ControllerAdvice 拦截异常并统一处理

    SpringBoot @ControllerAdvice 拦截异常并统一处理

    这篇文章主要介绍了SpringBoot @ControllerAdvice 拦截异常并统一处理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • springboot如何使用vue打包过的页面资源

    springboot如何使用vue打包过的页面资源

    这篇文章主要介绍了springboot如何使用vue打包过的页面资源,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • Spring Boot条件注解之@ConditionalOnProperty完全解析

    Spring Boot条件注解之@ConditionalOnProperty完全解析

    这篇文章主要介绍了SpringBoot中的@ConditionalOnProperty注解,通过配置文件属性值控制Bean或配置类的加载,实现功能开关和环境配置,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-02-02
  • SpringBoot bean依赖属性配置详细介绍

    SpringBoot bean依赖属性配置详细介绍

    Spring容器是Spring的核心,一切SpringBean都存储在Spring容器内。可以说bean是spring核心中的核心。Bean配置信息定义了Bean的实现及依赖关系,这篇文章主要介绍了SpringBoot bean依赖属性配置
    2022-09-09
  • 史上最全面的Spring Boot配置文件深入讲解

    史上最全面的Spring Boot配置文件深入讲解

    Springboot极大的简化了Spring框架的使用配置流程,在核心配置文件里,几乎可以完成所有的配置工作,下面这篇文章主要给大家介绍了关于Spring Boot配置文件的相关资料,文中介绍的非常全面,需要的朋友可以参考下
    2018-12-12
  • 使用SpringSecurity+defaultSuccessUrl不跳转指定页面的问题解决方法

    使用SpringSecurity+defaultSuccessUrl不跳转指定页面的问题解决方法

    本人是用springsecurity的新手,今天遇到defaultSuccessUrl不跳转指定页面的问题,真是头疼死了,网上找遍了解决方法都解决不了,今天给大家分享使用SpringSecurity+defaultSuccessUrl不跳转指定页面的问题解决方法,感兴趣的朋友一起看看吧
    2023-12-12
  • Java中命令行参数--与-D的区别

    Java中命令行参数--与-D的区别

    本文主要介绍了Java中命令行参数--与-D的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Spring-cloud Config Server的3种配置方式

    Spring-cloud Config Server的3种配置方式

    这篇文章主要介绍了Spring-cloud Config Server的3种配置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09

最新评论