//import java.text.SimpleDateFormat; //格式化日期//import java.util.Calendar; //日期//import java.util.Date; //时间//import java.util.GregorianCalendar; //Calendar的子类
import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;public class TestDate { public static void main(String[] args) throws ParseException { String strdate = "2019-10-19"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar gc = new GregorianCalendar(); Date date1 = df.parse(strdate); gc.setTime(date1); // 把一个日期设置到对象里面 int days = gc.get(Calendar.DAY_OF_MONTH); System.out.println("日\t一\t二\t三\t四\t五\t六"); gc.set(Calendar.DAY_OF_MONTH,1); //把日前设置成1号【因为要从1号打印】 //补充几个空格 for (int i=0;i < gc.get(Calendar.DAY_OF_WEEK)-1;i++){ System.out.print("\t"); } int maxday = gc.getActualMaximum(Calendar.DATE); gc.set(Calendar.DAY_OF_MONTH,1); //设置成第一天 for (int j = 1 ;j<maxday;j++){ if (days ==gc.get(Calendar.DAY_OF_MONTH)){ System.out.print(gc.get(Calendar.DAY_OF_MONTH)+"*"+"\t"); }else{ System.out.print(gc.get(Calendar.DAY_OF_MONTH)+"\t");} //循环的时候等于一周的时候换行 if (gc.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){ System.out.println(""); } gc.add(Calendar.DAY_OF_WEEK,1); } }}
JAVA_常用类
点赞
收藏