java创建一个日历(某个月)实例

'''

  package helloworld;




  //需要导入py4j.jar包
  import py4j.GatewayServer;
  import java.util.*;
  import java.lang.reflect.Array;
  import java.math.BigDecimal;
  import java.text.DecimalFormat;
  import java.text.FieldPosition;
  import java.util.AbstractList;
  import java.util.ArrayList;
  import java.util.Vector;
  import java.time.*;





  public class AdditionApplication//输入测试
  {
      public static void main(String[]  args)
      {
	//LocalDate date=LocalDate.now();//get  present date,构造一个对象,并用当前的日期初始化
	
	LocalDate date=LocalDate.of(2020, 9, 12);//指定特定的日期
	int month=date.getMonthValue();//get  month
	//int month =newyears.getMonthValue();
	int today=date.getDayOfMonth();//get today
	//int today=newyears.getDayOfMonth();//get today
	
	
	date=date.minusDays(today-1);//set to start of month
	DayOfWeek weekday=date.getDayOfWeek();
	int value=weekday.getValue();//1=Monday...得到星期几的数值表示
	
	System.out.println("MonTueWedThuFriSatSUn");//打印表头
	for (int i=1;i<value;i++)
		System.out.print("  ");//注意到日历是有缩进的,如果是星期一,那么就不缩进,如果是星期7,那么缩进6个单位
	while(date.getMonthValue()==month)//循环结束的条件
	{
		System.out.printf("%3d",date.getDayOfMonth());//打印日历
		if (date.getDayOfMonth()==today)
		{ System.out.print("*");}//如果date是今天,用*标记
		else
			{System.out.print("");}
		date=date.plusDays(1);//推迟一天
		if(date.getDayOfWeek().getValue()==1) System.out.println();//到达新的一周的第一天,那么就换行
	}
	if(date.getDayOfWeek().getValue() !=1) System.out.println();
		


		
		
      }
	
	
  }
posted @ 2020-09-08 21:36  heyhy  Views(703)  Comments(0Edit  收藏  举报
Title