使用Calendar日曆物件對時間的1.獲取 2.新增 3,修改
阿新 • • 發佈:2019-09-15
//用Calendar日曆物件 public static void main(String[] args) throws IOException, ClassNotFoundException { test3(); } /** * 使用Calendar日曆物件對時間的1.獲取 年,月,日, * @throws IOException */ private static void test1() throws IOException { Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH)+1; int day = c.get(Calendar.DAY_OF_MONTH); int week_day =c.get(Calendar.DAY_OF_WEEK); System.out.println(year+"年"+month+"月"+day+"日"+"星期"+week_day); } /** * 使用Calendar日曆物件對時間的1.修改 年,月,日, * @throws IOException */ private static void test2() throws IOException { Calendar c = Calendar.getInstance(); //修改時間 c.add(Calendar.YEAR,1); c.add(Calendar.MONTH,-1); c.add(Calendar.DAY_OF_MONTH,-6); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH)+1; int day = c.get(Calendar.DAY_OF_MONTH); int week_day =c.get(Calendar.DAY_OF_WEEK); System.out.println(year+"年"+month+"月"+day+"日"+"星期"+week_day); } /** * 使用Calendar日曆物件對時間的1.設定 年,月,日, * @throws IOException */ private static void test3() throws IOException { Calendar c = Calendar.getInstance(); //設定時間 c.set(Calendar.YEAR,2011); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH)+1; int day = c.get(Calendar.DAY_OF_MONTH); int week_day =c.get(Calendar.DAY_OF_WEEK); System.out.println(year+"年"+month+"月"+day+"日"+"星