1,、取得指定日期是星期幾 取得指定日期是星期幾可以采用下面兩種方式取得日期是星期幾: a,、使用Calendar類(lèi) [java] //根據(jù)日期取得星期幾 public static String getWeek(Date date){ String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; Calendar cal = Calendar.getInstance(); cal.setTime(date); int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1; if(week_index<0){ week_index = 0; } return weeks[week_index]; } b、使用SimpleDateFormat類(lèi) [java] //根據(jù)日期取得星期幾 public static String getWeek(Date date){ SimpleDateFormat sdf = new SimpleDateFormat("EEEE"); String week = sdf.format(date); return week; } 注:格式化字符串存在區(qū)分大小寫(xiě) 對(duì)于創(chuàng)建SimpleDateFormat傳入的參數(shù):EEEE代表星期,,如“星期四”;MMMM代表中文月份,,如“十一月”,;MM代表月份,如“11”,; yyyy代表年份,,如“2010”;dd代表天,,如“25” 2,、取得日期是某年的第幾周 根據(jù)日期入得日期是某年的第幾周。 [java] //取得日期是某年的第幾周 public static int getWeekOfYear(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); int week_of_year = cal.get(Calendar.WEEK_OF_YEAR); return week_of_year; } 3,、得到某年的某個(gè)月有多少天 已知年份和月份,,取得該月有多少天。 [java] //取得某個(gè)月有多少天 public static int getDaysOfMonth(int year,int month){ Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month-1); int days_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH); return days_of_month; } 4、取得兩個(gè)日期之間的相差多少天 已知兩個(gè)日期,,計(jì)算它們之間相差多少天,。 [java] <pre name="code" class="java">// 取得兩個(gè)日期之間的相差多少天 public static long getDaysBetween(Date date0, Date date1) { long daysBetween = (date0.getTime() - date1.getTime() + 1000000) / 86400000;// 86400000=3600*24*1000 用立即數(shù),減少乘法計(jì)算的開(kāi)銷(xiāo) return daysBetween; } 5,、完整的測(cè)試代碼 [java] package org.ml.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class CalendarDemo { public static void main(String[] args) { String strDate = "2013-03-08";// 定義日期字符串 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// 定義日期格式 Date date = null; try { date = format.parse(strDate);// 將字符串轉(zhuǎn)換為日期 } catch (ParseException e) { System.out.println("輸入的日期格式不合理,!"); } System.out.println(strDate + "是:" + getWeek(date)); System.out.println(strDate + "是一年的第:" + getWeekOfYear(date) + "周"); System.out.println(strDate + "是一年的" + (date.getMonth() + 1) + "月有:" + getDaysOfMonth(date.getYear(), date.getMonth() + 1) + "天"); System.out.println(strDate + "距離" + (format.format(new Date())) + "還有" + getDaysBetween(date, new Date()) + "天"); } // 根據(jù)日期取得星期幾 public static String getWeek(Date date) { // String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; // Calendar cal = Calendar.getInstance(); // cal.setTime(date); // int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1; // if(week_index<0){ // week_index = 0; // } // return weeks[week_index]; SimpleDateFormat sdf = new SimpleDateFormat("EEEE"); String week = sdf.format(date); return week; } // 取得日期是某年的第幾周 public static int getWeekOfYear(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); int week_of_year = cal.get(Calendar.WEEK_OF_YEAR); return week_of_year; } // 取得某個(gè)月有多少天 public static int getDaysOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month-1); int days_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH); return days_of_month; } // 取得兩個(gè)日期之間的相差多少天 public static long getDaysBetween(Date date0, Date date1) { long daysBetween = (date0.getTime() - date1.getTime() + 1000000) / 86400000;// 86400000=3600*24*1000 用立即數(shù),減少乘法計(jì)算的開(kāi)銷(xiāo) return daysBetween; } }
|
|