【日期-時間】Java中Calendar的使用

主要介紹了Calendar類的使用

輸出

  
  
  
  
* 時間格式化 * 當前時間:2016-12-02 16:46:27.079 * * 轉換:String-->Date-->Calendar * 設置時間(方式1):2014-08-01 09:18:34.123 * 設置時間(方式2):2016-12-01 19:08:54.123 * * ----YEAR-MONTH---- * YEAR:2016 * MONTH:11 * ----Day----- * 本月第幾天:1 * 本週幾:5 * 今年的第 N :336 * DAY_OF_WEEK_IN_MONTH:1 * ----Hour----- * Hour:7 * HOUR_OF_DAY:19 * ----MINUTE----- * MINUTE:8 * * 向後推遲一個月(方式1):2017-01-01 19:08:54.123 * 向後推遲一個月(方式2):2017-02-01 19:08:54.123 * 向後推遲一小時(方式1):2017-02-01 20:08:54.123 * 向前推遲一小時(方式2):2017-02-01 19:08:54.123 * 設置爲30分鐘:2017-02-01 19:30:00.123 * * 開始時間:2014-12-15 16:46:27.116,毫秒數:1418633187116 * 結束時間:2016-12-05 12:15:35.117,毫秒數:1480911335117 * 相差天數:721 * * 本月最大天數:31

程序
   
   
   
   
package com.sssppp.DateTime;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class CalendarExample { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); System.out.println("時間格式化"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); System.out.println("當前時間:" + sdf.format(cal.getTime())); System.out.println(); try { System.out.println("轉換:String-->Date-->Calendar"); cal.setTime(sdf.parse("2014-8-1 9:18:34.123")); System.out.println("設置時間(方式1):" + sdf.format(cal.getTime())); } catch (ParseException e) { e.printStackTrace(); } //2016-12-01 19:08:54 cal.set(2016, Calendar.DECEMBER, 1, 19, 8, 54); System.out.println("設置時間(方式2):" +sdf.format(cal.getTime())); System.out.println(); System.out.println("----YEAR-MONTH----"); System.out.println("YEAR:" + cal.get(Calendar.YEAR)); System.out.println("MONTH:" + cal.get(Calendar.MONTH)); System.out.println("----Day-----"); System.out.println("本月第幾天:" + cal.get(Calendar.DAY_OF_MONTH)); System.out.println("本週幾:" + cal.get(Calendar.DAY_OF_WEEK)); System.out.println("今年的第 N天 :" + cal.get(Calendar.DAY_OF_YEAR)); System.out.println("DAY_OF_WEEK_IN_MONTH:" + cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)); System.out.println("----Hour-----"); System.out.println("Hour:" + cal.get(Calendar.HOUR)); System.out.println("HOUR_OF_DAY:" + cal.get(Calendar.HOUR_OF_DAY)); System.out.println("----MINUTE-----"); System.out.println("MINUTE:" + cal.get(Calendar.MINUTE)); System.out.println(); cal.set(Calendar.MONTH,cal.get(Calendar.MONTH)+1); System.out.println("向後推遲一個月(方式1):" + sdf.format(cal.getTime())); cal.add(Calendar.MONDAY, 1); System.out.println("向後推遲一個月(方式2):" + sdf.format(cal.getTime())); cal.set(Calendar.HOUR,cal.get(Calendar.HOUR)+1); System.out.println("向後推遲一小時(方式1):" + sdf.format(cal.getTime())); cal.add(Calendar.HOUR, -1); System.out.println("向前推遲一小時(方式2):" + sdf.format(cal.getTime())); cal.set(Calendar.MINUTE,30); cal.set(Calendar.SECOND,00); System.out.println("設置爲30分鐘:" + sdf.format(cal.getTime())); cal.getTimeInMillis(); System.out.println(); Calendar calStart = Calendar.getInstance(); calStart.set(2014, Calendar.DECEMBER, 15); System.out.println("開始時間:" + sdf.format(calStart.getTimeInMillis()) + ",毫秒數:" + calStart.getTimeInMillis()); Calendar calEnd = Calendar.getInstance(); calEnd.set(2016, Calendar.DECEMBER, 5, 12, 15, 35); System.out.println("結束時間:" + sdf.format(calEnd.getTimeInMillis()) + ",毫秒數:" + calEnd.getTimeInMillis()); int day =getDaysBetween(calEnd,calStart); System.out.println("相差天數:" + day); //計算某一月份的最大天數 System.out.println(); Calendar time=Calendar.getInstance(); time.clear(); // 在使用set方法以前,必須先clear一下,不然不少信息會繼承自系統當前時間 time.set(Calendar.YEAR, 2016); int dayCount = time.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月份的天數 System.out.println("本月最大天數:" + dayCount); } /** * 計算兩個時間之間相隔天數(不夠精確) * * @param startday * 開始時間 * @param endday * 結束時間 * @return */ public static int getIntervalDays(Calendar startday, Calendar endday) { // 確保startdayendday以前 if (startday.after(endday)) { Calendar cal = startday; startday = endday; endday = cal; } // 分別獲得兩個時間的毫秒數 long sl = startday.getTimeInMillis(); long el = endday.getTimeInMillis(); long ei = el - sl; // 根據毫秒數計算間隔天數 return (int) (ei / (1000 * 60 * 60 * 24)); } /** * 計算兩個時間之間相隔天數(不夠精確) * * @param startday * 開始時間 * @param endday * 結束時間 * @return */ public int getIntervalDays(Date startday, Date endday) { // 確保startdayendday以前 if (startday.after(endday)) { Date cal = startday; startday = endday; endday = cal; } // 分別獲得兩個時間的毫秒數 long sl = startday.getTime(); long el = endday.getTime(); long ei = el - sl; // 根據毫秒數計算間隔天數 return (int) (ei / (1000 * 60 * 60 * 24)); } /** * 精確計算兩個日期之間的天數 * * @param d1 * @param d2 * @return */ public static int getDaysBetween(Calendar d1, Calendar d2) { if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end java.util.Calendar swap = d1; d1 = d2; d2 = swap; } int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR); int y2 = d2.get(Calendar.YEAR); if (d1.get(Calendar.YEAR) != y2) { d1 = (Calendar) d1.clone(); do { days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 獲得當年的實際天數 d1.add(Calendar.YEAR, 1); } while (d1.get(Calendar.YEAR) != y2); } return days; } }










相關文章
相關標籤/搜索