简单的java编程(练手),日历的制作

package com.company;

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 Main {

public static void main(String[] args) {
System.out.println("请输入如期(格式如:2018-04-02)");
Scanner scanner = new Scanner(System.in);
String temp = scanner.nextLine();
DateFormat dateformat = new SimpleDateFormat("yy-MM-dd");
try {
Date date = dateformat.parse(temp);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
int day = calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE,1);


System.out.println(date);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
System.out.println(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<=calendar.getActualMaximum(Calendar.DATE);i++){
if (i==day){
System.out.print("*");
}
System.out.print(i+"\t");
int w = calendar.get(Calendar.DAY_OF_WEEK);
if (w==Calendar.SATURDAY){
System.out.println();
}
calendar.add(Calendar.DATE,1);
}
} catch (ParseException e) {
e.printStackTrace();
}

}
}
posted @ 2018-04-11 14:26  TesterMa  阅读(2082)  评论(0编辑  收藏  举报