首先 希望疫情 儘快結束 大家以後拒絕野味呀 !!!
直接上乾貨 代碼
這裏把 主函數上傳上去了 因爲聲明異常了 所以不省略了
public static void main(String[] args) throws ParseException {//這裏偷懶 直接異常聲明
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入你要查詢的日期:");
String dateStr =scanner.next();
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
Date data = sim.parse(dateStr);
Calendar calendar = new GregorianCalendar(); //實例化一個標準日曆
calendar.setTime(data); //將時間賦值給calendar
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,1); //用來賦值當前日期這個月的第幾天
//DAY_OF_MONTH 當前日期 是這個月的第幾天
//Calendar.DAY_OF_YEAR 當前日期是這一年的第幾天
//DAY_OF_WEEK,用來獲得當前日期是一週的第幾天
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (int i = 0; i <calendar.get(Calendar.DAY_OF_WEEK)-1; i ) {
System.out.print("\t");
}
int days = calendar.getActualMaximum(Calendar.DATE); //獲取當前月的最大天數
for (int i = 1; i <=days; i ) {
int nowDay = calendar.get(Calendar.DAY_OF_MONTH);
if(day == nowDay ){ //如果循環日期 等於輸入日期 *號標出
System.out.print(calendar.get(Calendar.DAY_OF_MONTH) "*" "\t");
}else {//否則正常輸出
System.out.print(calendar.get(Calendar.DAY_OF_MONTH) "\t");
}
if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){ //如果是週六換行
System.out.println();
}
calendar.add(Calendar.DAY_OF_MONTH,1); //每次 1
}
}
運行結果如下:
下面 大家來溫故下 Calander 的知識
獲取時間
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalenderTest {
//獲取時間:以目前系統時間 :2019:01:27 16:27:27 爲例
@Test
public void testGetCalender() {
Calendar cal = Calendar.getInstance(); //Calendar類的實例化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //時間的格式化
//當前系統時間
Date date = cal.getTime(); //date=Sun Jan 27 16:27:27 CST 2019
String nowTime = sdf.format(cal.getTime()); //nowTime=2019-01-27 16:27:27
//當前年
int year = cal.get(Calendar.YEAR); //year=2019
//當前月 Calendar.MONTH從0開始 ,使用時通常會 1
int month = cal.get(Calendar.MONTH) 1; //month=1
//當前日:兩種方法等價
int day_of_month = cal.get(Calendar.DAY_OF_MONTH); //day_of_month=27
int day = cal.get(Calendar.DATE); //day=27
//獲取當月day的最大值!!
int max_day_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //max_day_of_month=31
//當前時鐘:24小時制
int hour24 = cal.get(Calendar.HOUR_OF_DAY); //hour24=16
//當前時鐘:12小時制
int hour12 = cal.get(Calendar.HOUR); //hour12=4
//當前:分鐘
int minute = cal.get(Calendar.MINUTE); //minute=27
//當前秒
int second = cal.get(Calendar.SECOND); //second=27
//星期幾:用數字(1~7)表示(星期日~星期六),使用時通常會-1
int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 1; //day_of_week=0
//上午-0;下午-1
int amOrPm = cal.get(Calendar.AM_PM); //amOrPm=1
//當前年的第幾周
int week_of_year = cal.get(Calendar.WEEK_OF_YEAR); //week_of_year=5
//當前月的星期數
int week_of_month = cal.get(Calendar.WEEK_OF_MONTH); //week_of_month=5
//當前月中的第幾個星期
int day_of_week_in_month = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH); //day_of_week_in_month=4
//當前年的第幾天
int day_of_year = cal.get(Calendar.DAY_OF_YEAR); //day_of_year=27
}
}
設置時間
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalenderTest {
//設置時間:以目前系統時間 :2019:01:27 16:27:27 爲例
@Test
public void testSetCalender() {
Calendar cal = Calendar.getInstance(); //Calendar類的實例化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //時間的格式化
//1.當前系統時間
Date date = cal.getTime(); //data=Sun Jan 27 16:27:27 CST 2019
String nowTime = sdf.format(cal.getTime()); //nowTime=2019-01-27 16:27:27
//2.設置時間:不設置項,默認Calendar已經存在日期
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 30);
cal.set(Calendar.SECOND, 30);
String date1 = sdf.format(cal.getTime()); //time=2019-01-27 11:30:30
//3.設置時間:全部設置,則輸出自定義時間
cal.set(Calendar.YEAR, 2018);
cal.set(Calendar.MARCH, 7);
cal.set(Calendar.DATE, 21);
String date2 = sdf.format(cal.getTime()); //time=2018-08-21 11:30:30
//4.時間重置:Date date = new date();
String newDate1 = sdf.format(cal.getTime()); //newDate1=2018-08-21 11:30:30
String newDate2 = sdf.format(new Date()); //newDate2=2019:01:27 16:27:27
}
}
時間運算
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalenderTest {
//時間運算:以目前系統時間 :2019:01:27 16:27:27 爲例
@Test
public void testAddCalender() {
Calendar cal = Calendar.getInstance(); //Calendar類的實例化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //時間的格式化
String nowTime = sdf.format(cal.getTime()); //nowTime=2019-01-27 16:27:27
cal.add(Calendar.YEAR, 1);
String time1 = sdf.format(cal.getTime()); //年:2020-01-27 16:27:27
cal.add(Calendar.MONTH, 2);
String time2 = sdf.format(cal.getTime()); //月:2020-03-27 16:27:27
cal.add(Calendar.DATE, 5);
String time3 = sdf.format(cal.getTime()); //日:2020-04-01 16:27:27
cal.add(Calendar.HOUR_OF_DAY, -1);
String time4 = sdf.format(cal.getTime()); //時:2020-04-01 15:27:27
cal.add(Calendar.MINUTE, 1);
String time5 = sdf.format(cal.getTime()); //分:2020-04-01 15:28:27
cal.add(Calendar.SECOND, 1);
String time6 = sdf.format(cal.getTime()); //秒:2020-04-01 15:27:28
}
}
上述總結的操作時間方法 不是自己敲得 轉載於 點它
有問題評論區見 大家一起探討呀 !!!