您的位置:首页 > 其它

根据输入的日期,控制台打印格式化日历

2015-07-30 23:30 10 查看
package com.aming.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class TestViewDate {
public static void main(String args[]){
System.out.println("请输入一个指定的日期(2015-08-22):");
Scanner scan = new Scanner(System.in);
String inDate = scan.nextLine();
//String inDate = "2016-08-20";
//Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(inDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar c = new GregorianCalendar();
c.setTime(date);
int tody = c.get(Calendar.DATE);
c.set(Calendar.DATE,1);
int week = c.get(Calendar.DAY_OF_WEEK);
int maxDay = c.getActualMaximum(Calendar.DATE);
//System.out.println(maxDay);
//System.out.println(week);
System.out.println("日\t"+"一\t"+"二\t"+"三\t"+"四\t"+"五\t"+"六");
for(int i =1;i<=maxDay+week-1;i++){
if(i<week){
System.out.print("\t");
}else{
if(tody == i-week+1){
//System.out.print("*");
System.out.print(i-week+1+"*\t");
}else{
System.out.print(i-week+1+"\t");
}

}

if(i%7==0)
{
System.out.println();
}
}
}
}

运行效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: