Java练习小项目

2023-11-30 18:43:34 浏览数 (2)

Java练习小项目:

打印本月日期:

代码语言:javascript复制
package com.javatest.datetest;

import java.time.*;

/*
 * @version 1.0
 * @author Tan name
 * 该类功能可实现打印本月的日期
 */
public class DateTest {

	public static void main(String[] args) {
		//创建date对象
		LocalDate date = LocalDate.now();
		//获取月份
		int month = date.getMonthValue();
		//获取日期
		int today = date.getDayOfMonth();
		
		//将日期调整为每月的第一天
		date = date.minusDays(today - 1);
		//创建weekday对象
		DayOfWeek weekday = date.getDayOfWeek();
		//返回获取1-7的数字代表礼拜一到礼拜天
		int value = weekday.getValue();
		
		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("=",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();
		}
	}

}

0 人点赞