zoukankan      html  css  js  c++  java
  • 应用LocalDate类显示当月日历

    应用LocalDate类显示当月日历

    输出格式

    日历格式

    程序清单

    import java.time.*;
    public class CalendarTest {
        public static void main(String[] args) {
            LocalDate date = LocalDate.now();//获取一个日历对象,使用当前的时间初始化2019.7.8
            int month =  date.getMonthValue();//7
            int today = date.getDayOfMonth();//8
    
            date = date.minusDays(today-1);//将当前日期设置为该月第一天,2019年7月1日
            DayOfWeek weekday = date.getDayOfWeek();//得到当前日期是星期几,“星期一”
            int value = weekday.getValue();//得到星期几的数值“1”
    
            System.out.println("Mon Tue Wed Thu Fri Sat Sun");//首行输出
            for(int i = 1; i < value; i++){
                System.out.print(" ");//输出首行缩进
            }
            while (date.getMonthValue() == month){
                System.out.printf("%3d",date.getDayOfMonth());//输出“日-->1”
                /**
                 * 标记"今天"是在日历中的哪一天
                 */
                if (date.getDayOfMonth() == today){
                    System.out.print("*");
                }else {
                    System.out.print(" ");
                }
                date = date.plusDays(1);//日期往后+1
                if(date.getDayOfWeek().getValue() == 1)System.out.println();//如果为“星期一”则换行
            }
            if (date.getDayOfWeek().getValue() != 1)System.out.println();
        }
    }
    
    

    API Java.time.localDate 8

    • static LocalTime now()
      构造一个表示当前日期的对象
    • static LocalTime of(int year, int month int day)
      构造一个表示给定日期的对象
    • int getYear()
    • int getMonthValue()
    • int getDayOfMonth()
      得到当前日期的年月日。
    • DayOfWeek getDayOfWeek
      得到当前日期是星期几,返回一个DayOfWeek的实例。调用getValue()得到1~7之间的一个数分别对应星期一到星期日
    • LocalDate plusDays(int n)
      生成当前日期之后的几天日期
    • LocalDate 生成当前日期之前的几天日期
  • 相关阅读:
    多Web服务器之间共享Session的解决方案
    在WinForm中使用CacheDependency来监视文件
    使用WCF的一些问题
    IIS6.0配置注意
    匿名委托注册事件的触发
    关于datawindow does not have update capability
    EF自关联建模详解
    NHiberante3.2版注意
    EF做数据绑定时一些神奇问题
    EF 中不同会话上下文的对象,不能互设为对方的导航属性值
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13860170.html
Copyright © 2011-2022 走看看