[java]代码库
package cn.bjsxt.date;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
/**
* 可视化日期小程序
* @author 刘涛
*
*/
public class VisableCalendar {
public static void main(String[] args) {
makeCalendar();
}
static void makeCalendar(){
System.out.println("请输入日期:(按照格式:2017-12-17):");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String inputStr = sc.nextLine();
System.out.println(new Date(System.currentTimeMillis()).toLocaleString());
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = df.parse(inputStr);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
//获取当前的日期
int day = calendar.get(Calendar.DATE);
//设置一号的位置
calendar.set(Calendar.DATE,1);
//获取本月最后一天是多少
int maxDate = calendar.getActualMaximum(Calendar.DATE);
System.out.println("日\t一\t二\t三\t四\t五\t六");
//判断一号的位置,确定后将一号位置之前的位置打印制表符
for(int i = 1; i < calendar.get(Calendar.DAY_OF_WEEK); i ++){
System.out.print(" \t ");
}
for(int i = 1; i <= maxDate; i ++){
//判断当前日期与所输入的日期是否相等,若相等则添加标志
if(day == i){
System.out.print("@");
}
System.out.print(i + "\t");
//获取当前日期为本周的周几
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
if(weekday == Calendar.SATURDAY){
System.out.println();
}
calendar.add(Calendar.DATE, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}