用Java做一个简单的日历

package com.company;
import java.time.DayOfWeek;
import java.time.LocalDate;


public class Main {

    public static void main(String[] args) {
   // write your code here
        LocalDate date=LocalDate.now();
        int month=date.getMonthValue();
        int today=date.getDayOfMonth();

        date=date.minusDays(today-1);//set the start of the month
        DayOfWeek weekday=date.getDayOfWeek();
        int value=weekday.getValue();//monday=1...sunday=7

        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());
            if(date.getDayOfMonth()==today)
                System.out.print("*");
            else
                System.out.print(" ");
            date=date.plusDays(1);
            if(date.getDayOfWeek().getValue()==1)
                System.out.println();
        }
        if (date.getDayOfWeek().getValue()!=1)
            System.out.println();
    }
}

猜你喜欢

转载自blog.csdn.net/billy1900/article/details/80516686
今日推荐