要求
判断输入的时间为周几,本年的第几天
实施
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class Test33 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int year = in.nextInt();
int months = in.nextInt();
int day = in.nextInt();
/*------------------方法一---------------------*/
DateFormat sdf = new SimpleDateFormat("一年中的第 D 天,E");
Date date = new Date(year-1900,months-1,day);
//Date date = new Date("2019/09/09");
System.out.println(sdf.format(date));
/*------------------方法二---------------------*/
Calendar time = Calendar.getInstance();
time.set(year,months-1,day);
int week = time.get(Calendar.DAY_OF_WEEK);
int getDay = time.get(Calendar.DAY_OF_YEAR);
System.out.println("一年中的第 "+getDay+" 天,"+getWeek(week));
}
public static String getWeek(int week){
String[] arr = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
return arr[week-1];
}
}
版权属于:牧瀬くりす
本文链接:https://www.hcyacg.com/tutorial/Java-7.html
本站文章采用知识共享署名4.0 国际许可协议进行许可,允许非商业转载,但必须注明出处!