java使用Calendar類獲得指定日期
???關于指定日期的獲取,是根據指定日期和當前日期相差的天數,然后使用set方法設置Calendar.DAY_OF_MONTH的值。
? ? Calendar cal = Calendar.getInstance();
? ? cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - dayPlus);
? ? (dayPlus表示指定日期和當前日期相差的天數)
? ? 不進行set,默認是獲取系統時間,初步測試沒有問題,可正確獲得指定日期。
? ? *************************************************************************************************
? ? 代碼:
? ? // 獲得當前日期
? ? public static String getDate() {
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???return getDate(cal);
? ? }
? ? // 獲得日期
? ? private static String getDate(Calendar cal) {
? ?? ???String v_strDate = "";
? ?? ???int v_intYear = cal.get(Calendar.YEAR);
? ?? ???int v_intMonth = cal.get(Calendar.MONTH) + 1;
? ?? ???int v_intDAY = cal.get(Calendar.DAY_OF_MONTH);
? ?? ???int v_intHOUR = cal.get(Calendar.HOUR_OF_DAY);
? ?? ???int v_intMINUTE = cal.get(Calendar.MINUTE);
? ?? ???int v_intSECOND = cal.get(Calendar.SECOND);
? ?? ???
? ?? ???if (v_intDAY < 10) {
? ?? ?? ?? ?v_strDate = v_strDate + "0" + v_intDAY + "-";
? ?? ???}else {
? ?? ?? ?? ?v_strDate = v_strDate + v_intDAY + "-";
? ?? ???}
? ?? ???if (v_intMonth < 10) {
? ?? ?? ?? ?v_strDate = v_strDate + "0" + v_intMonth + "-";
? ?? ???}else {
? ?? ?? ?? ?v_strDate = v_strDate + v_intMonth + "-";
? ?? ???}
? ?? ???v_strDate = v_strDate + v_intYear + " ";
? ?? ???
? ?? ???if (v_intHOUR < 10) {
? ?? ?? ?? ?v_strDate = v_strDate + "0" + v_intHOUR + ":";
? ?? ???}else {
? ?? ?? ?? ?v_strDate = v_strDate + v_intHOUR + ":";
? ?? ???}
? ?? ???if (v_intMINUTE < 10) {
? ?? ?? ?? ?v_strDate = v_strDate + "0" + v_intMINUTE + ":";
? ?? ???} else {
? ?? ?? ?? ?v_strDate = v_strDate + v_intMINUTE + ":";
? ?? ???}
? ?? ???if (v_intSECOND < 10) {
? ?? ?? ?? ?v_strDate = v_strDate + "0" + v_intSECOND;
? ?? ???} else {
? ?? ?? ?? ?v_strDate = v_strDate + v_intSECOND;
? ?? ???}
? ?? ???cal = null;
? ?? ???return v_strDate;
? ? }
? ? //獲得當前日期與本周日相差的天數
? ? private static int getMondayPlus() {
? ?? ???Calendar cd = Calendar.getInstance();
? ?? ???// 獲得今天是一周的第幾天,星期日是第一天,星期二是第二天......
? ?? ???int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因為按中國禮拜一作為第一天所以這里減1
? ?? ???if (dayOfWeek == 1) {
? ?? ?? ?? ?return 0;
? ?? ???} else {
? ?? ?? ?? ?return dayOfWeek - 1;
? ?? ???}
? ? }
? ? //獲得本周一的日期
? ? public static String getThisMondayDate() {
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???int mondayPlus = getMondayPlus();
? ?? ???cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-mondayPlus);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 0);
? ?? ???cal.set(Calendar.MINUTE, 0);
? ?? ???cal.set(Calendar.SECOND, 0);
? ?? ???return getDate(cal);
? ? }
? ??
? ? //獲得本月1號的日期
? ? public static String getCurrentMonthBeginDate(){
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???cal.set(Calendar.DAY_OF_MONTH, 1);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 0);
? ?? ???cal.set(Calendar.MINUTE, 0);
? ?? ???cal.set(Calendar.SECOND, 0);
? ?? ???return getDate(cal);
? ? }
? ??
? ? //獲得上周一的日期
? ? public static String getLastMondayDate(){
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???int dayPlus = getMondayPlus()+7;
? ?? ???cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-dayPlus);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 0);
? ?? ???cal.set(Calendar.MINUTE, 0);
? ?? ???cal.set(Calendar.SECOND, 0);
? ?? ???return getDate(cal);
? ? }
? ??
? ? //獲得上周日的日期
? ? public static String getLastSundayDate(){
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???int dayPlus = getMondayPlus()+1;
? ?? ???cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-dayPlus);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 23);
? ?? ???cal.set(Calendar.MINUTE, 59);
? ?? ???cal.set(Calendar.SECOND, 59);
? ?? ???return getDate(cal);
? ? }
? ??
? ? //獲得上月1號的日期
? ? public static String getLastMonthBeginDate(){
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
? ?? ???cal.set(Calendar.DAY_OF_MONTH,1);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 0);
? ?? ???cal.set(Calendar.MINUTE, 0);
? ?? ???cal.set(Calendar.SECOND, 0);
? ?? ???return getDate(cal);
? ? }
? ??
? ? //獲得上個月最后一天的日期
? ? public static String getLastMonthEndDate(){
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???cal.set(Calendar.DAY_OF_MONTH, 0);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 23);
? ?? ???cal.set(Calendar.MINUTE, 59);
? ?? ???cal.set(Calendar.SECOND, 59);
? ?? ???return getDate(cal);
? ? }
? ? public static void main(String[] args) {
? ?? ???System.out.println(getDate());
? ?? ???System.out.println(getThisMondayDate());
? ?? ???System.out.println(getCurrentMonthBeginDate());
? ?? ???System.out.println(getLastMondayDate());
? ?? ???System.out.println(getLastSundayDate());
? ?? ???System.out.println(getLastMonthBeginDate());
? ?? ???System.out.println(getLastMonthEndDate());
? ? }
? ? Calendar cal = Calendar.getInstance();
? ? cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - dayPlus);
? ? (dayPlus表示指定日期和當前日期相差的天數)
? ? 不進行set,默認是獲取系統時間,初步測試沒有問題,可正確獲得指定日期。
? ? *************************************************************************************************
? ? 代碼:
? ? // 獲得當前日期
? ? public static String getDate() {
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???return getDate(cal);
? ? }
? ? // 獲得日期
? ? private static String getDate(Calendar cal) {
? ?? ???String v_strDate = "";
? ?? ???int v_intYear = cal.get(Calendar.YEAR);
? ?? ???int v_intMonth = cal.get(Calendar.MONTH) + 1;
? ?? ???int v_intDAY = cal.get(Calendar.DAY_OF_MONTH);
? ?? ???int v_intHOUR = cal.get(Calendar.HOUR_OF_DAY);
? ?? ???int v_intMINUTE = cal.get(Calendar.MINUTE);
? ?? ???int v_intSECOND = cal.get(Calendar.SECOND);
? ?? ???
? ?? ???if (v_intDAY < 10) {
? ?? ?? ?? ?v_strDate = v_strDate + "0" + v_intDAY + "-";
? ?? ???}else {
? ?? ?? ?? ?v_strDate = v_strDate + v_intDAY + "-";
? ?? ???}
? ?? ???if (v_intMonth < 10) {
? ?? ?? ?? ?v_strDate = v_strDate + "0" + v_intMonth + "-";
? ?? ???}else {
? ?? ?? ?? ?v_strDate = v_strDate + v_intMonth + "-";
? ?? ???}
? ?? ???v_strDate = v_strDate + v_intYear + " ";
? ?? ???
? ?? ???if (v_intHOUR < 10) {
? ?? ?? ?? ?v_strDate = v_strDate + "0" + v_intHOUR + ":";
? ?? ???}else {
? ?? ?? ?? ?v_strDate = v_strDate + v_intHOUR + ":";
? ?? ???}
? ?? ???if (v_intMINUTE < 10) {
? ?? ?? ?? ?v_strDate = v_strDate + "0" + v_intMINUTE + ":";
? ?? ???} else {
? ?? ?? ?? ?v_strDate = v_strDate + v_intMINUTE + ":";
? ?? ???}
? ?? ???if (v_intSECOND < 10) {
? ?? ?? ?? ?v_strDate = v_strDate + "0" + v_intSECOND;
? ?? ???} else {
? ?? ?? ?? ?v_strDate = v_strDate + v_intSECOND;
? ?? ???}
? ?? ???cal = null;
? ?? ???return v_strDate;
? ? }
? ? //獲得當前日期與本周日相差的天數
? ? private static int getMondayPlus() {
? ?? ???Calendar cd = Calendar.getInstance();
? ?? ???// 獲得今天是一周的第幾天,星期日是第一天,星期二是第二天......
? ?? ???int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因為按中國禮拜一作為第一天所以這里減1
? ?? ???if (dayOfWeek == 1) {
? ?? ?? ?? ?return 0;
? ?? ???} else {
? ?? ?? ?? ?return dayOfWeek - 1;
? ?? ???}
? ? }
? ? //獲得本周一的日期
? ? public static String getThisMondayDate() {
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???int mondayPlus = getMondayPlus();
? ?? ???cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-mondayPlus);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 0);
? ?? ???cal.set(Calendar.MINUTE, 0);
? ?? ???cal.set(Calendar.SECOND, 0);
? ?? ???return getDate(cal);
? ? }
? ??
? ? //獲得本月1號的日期
? ? public static String getCurrentMonthBeginDate(){
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???cal.set(Calendar.DAY_OF_MONTH, 1);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 0);
? ?? ???cal.set(Calendar.MINUTE, 0);
? ?? ???cal.set(Calendar.SECOND, 0);
? ?? ???return getDate(cal);
? ? }
? ??
? ? //獲得上周一的日期
? ? public static String getLastMondayDate(){
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???int dayPlus = getMondayPlus()+7;
? ?? ???cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-dayPlus);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 0);
? ?? ???cal.set(Calendar.MINUTE, 0);
? ?? ???cal.set(Calendar.SECOND, 0);
? ?? ???return getDate(cal);
? ? }
? ??
? ? //獲得上周日的日期
? ? public static String getLastSundayDate(){
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???int dayPlus = getMondayPlus()+1;
? ?? ???cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-dayPlus);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 23);
? ?? ???cal.set(Calendar.MINUTE, 59);
? ?? ???cal.set(Calendar.SECOND, 59);
? ?? ???return getDate(cal);
? ? }
? ??
? ? //獲得上月1號的日期
? ? public static String getLastMonthBeginDate(){
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
? ?? ???cal.set(Calendar.DAY_OF_MONTH,1);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 0);
? ?? ???cal.set(Calendar.MINUTE, 0);
? ?? ???cal.set(Calendar.SECOND, 0);
? ?? ???return getDate(cal);
? ? }
? ??
? ? //獲得上個月最后一天的日期
? ? public static String getLastMonthEndDate(){
? ?? ???Calendar cal = Calendar.getInstance();
? ?? ???cal.set(Calendar.DAY_OF_MONTH, 0);
? ?? ???cal.set(Calendar.HOUR_OF_DAY, 23);
? ?? ???cal.set(Calendar.MINUTE, 59);
? ?? ???cal.set(Calendar.SECOND, 59);
? ?? ???return getDate(cal);
? ? }
? ? public static void main(String[] args) {
? ?? ???System.out.println(getDate());
? ?? ???System.out.println(getThisMondayDate());
? ?? ???System.out.println(getCurrentMonthBeginDate());
? ?? ???System.out.println(getLastMondayDate());
? ?? ???System.out.println(getLastSundayDate());
? ?? ???System.out.println(getLastMonthBeginDate());
? ?? ???System.out.println(getLastMonthEndDate());
? ? }