JAVA中LocalDate类日历表的建立(eclipse 实例)

该程序使用Java的LocalDate和DayOfWeek类,根据当前日期生成并打印当前月份的日历。它首先获取当前年份、月份和日期,然后回溯到月份的第一天,并标记当天的日期。日历以周为单位打印,星期日开始,每个日期前用空格对齐,星号表示当天。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  本程序遵循国际惯例,与我们日常生活中的使用日历习惯相似,仅供参考。

   运行截图:

 程序源代码:

package Joey1;
import java.time.*;
import java.util.*;
public class j {
    public static void main(String args[])
    {
        LocalDate date = LocalDate.now();
        int year = date.getYear();//The year is...
        int mouth = date.getMonthValue();//The mouth is...
        int today = date.getDayOfMonth();//The today is...
        System.out.println(mouth+" "+year);
        date = date.minusDays(today-1);//Set to start of mouth
        DayOfWeek weekday = date.getDayOfWeek();
        int value = weekday.getValue();//1 = Monday...
        System.out.println("SUN MON TUE WED THU FRI SAT");
        for(int i = 0;i < value&&value < 7;i++)
            System.out.print("    ");
        while(date.getMonthValue() == mouth)
        {
            System.out.printf("%3d",date.getDayOfMonth());
            if(date.getDayOfMonth() == today)
                System.out.print("*");//The date of the day is marked.
            else
                System.out.print(" ");
            date = date.plusDays(1);//next day
            if(date.getDayOfWeek().getValue() == 7)
                System.out.println();//Line feed printing
        }
        if(date.getDayOfWeek().getValue() == 7)
            System.out.println();//Line feed printing
    }
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joey.Chao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值