import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class Date_test {
public static void main(String[] args) throws ParseException {
System.out.println(new Date());
SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(f1.format(new Date()));
System.out.println("----------------------------------------------------");
String s2 = "2020-03-04";
SimpleDateFormat f21 = new SimpleDateFormat("yyyy-MM-dd");
Date date2;
try {
date2 = f21.parse(s2);
SimpleDateFormat f22 = new SimpleDateFormat("yyyy年MM月dd日");
String s22 = f22.format(date2);
System.out.println(s22);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("----------------------------------------------------");
String s3 = "2018年2月14日";
SimpleDateFormat f31=new SimpleDateFormat("yyyy年MM月dd日");
Date date3 = f31.parse(s3);
Calendar c=Calendar.getInstance();
c.setTime(date3);
int date32 = c.get(Calendar.DAY_OF_WEEK);
day(date32);
System.out.println("-----------------------------------------------------");
Scanner sc = new Scanner(System.in);
System.out.println("请输入年:");
int year = sc.nextInt();
System.out.println("请输入月: ");
int month = sc.nextInt();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
Date startData = df.parse("2020-02-01");
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
cal.set(Calendar.DAY_OF_MONTH, 1);
int weekStart = cal.get(Calendar.DAY_OF_WEEK);
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (int i=1;i<weekStart;i++){
System.out.printf("%-8s","");
}
int countRelax = 0;
int countWeekend = 0;
int maxDate = cal.getActualMaximum(Calendar.DATE);
for (int i=1;i<= maxDate;i++){
cal.set(Calendar.DAY_OF_MONTH, i);
Date temp = cal.getTime();
int gap = getDifferentDay(startData, temp);
if (gap%4==1){
System.out.printf("%-8s","["+cal.get(Calendar.DAY_OF_MONTH)+"]");
countRelax++;
if (cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
countWeekend++;
}
}else {
System.out.printf("%-8s", cal.get(Calendar.DAY_OF_MONTH));
}
if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
System.out.println();
}
cal.add(Calendar.DAY_OF_MONTH,1);
}
System.out.println("本月休息的天数有:"+countRelax);
System.out.println("本月轮到周末休息的天数有:"+countWeekend);
}
private static int getDifferentDay(Date from, Date to){
long fromTime = from.getTime();
long toTime = to.getTime();
int days = (int) ((toTime - fromTime) / (1000 * 60 * 60 * 24));
return days;
}
public static void day(int i) {
switch (i) {
case 1:
System.out.println("星期天");
break;
case 2:
System.out.println("星期一");
break;
case 3:
System.out.println("星期二");
break;
case 4:
System.out.println("星期三");
break;
case 5:
System.out.println("星期四");
break;
case 6:
System.out.println("星期五");
break;
case 7:
System.out.println("星期六");
break;
}
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-2797.html