日期時間格式的工具DateUtils整理


關於一些時間工具的處理

 * 描述:此類用於取得當前日期相對應的月初,月末,季初,季末,年初,年末,返回值均為String字符串
* 1、得到當前日期 today()
* 2、得到當前月份月初 thisMonth()
* 3、得到當前月份月底 thisMonthEnd()
* 4、得到當前季度季初 thisSeason()
* 5、得到當前季度季末 thisSeasonEnd()
* 6、得到當前年份年初 thisYear()
* 7、得到當前年份年底 thisYearEnd()
* 8、判斷輸入年份是否為閏年 leapYear
* 9、得到當前的月份 localMonth()
* 注意事項: 日期格式為:yyyy-MM-dd (eg: 2007-12-05)

import java.util.Calendar;

/**
 * 描述:此類用於取得當前日期相對應的月初,月末,季初,季末,年初,年末,返回值均為String字符串
 * 1、得到當前日期         today()
 * 2、得到當前月份月初      thisMonth()
 * 3、得到當前月份月底      thisMonthEnd()
 * 4、得到當前季度季初      thisSeason()
 * 5、得到當前季度季末      thisSeasonEnd()
 * 6、得到當前年份年初      thisYear()
 * 7、得到當前年份年底      thisYearEnd()
 * 8、判斷輸入年份是否為閏年 leapYear
 * 9、得到當前的月份        localMonth()
 * <p>
 * 注意事項:  日期格式為:yyyy-MM-dd (eg: 2007-12-05)
 * <p>
 * 實例:
 *
 * @author pure
 */
public class DateUtils {
    private int x;                  // 日期屬性:年
    private int y;                  // 日期屬性:月
    private int z;                  // 日期屬性:日
    private Calendar localTime;     // 當前日期

    public DateUtils() {
        localTime = Calendar.getInstance();
    }

    /**
     * 功能:得到當前日期 格式為:yyyy-MM-dd (eg: 2007-12-05)<br>
     *
     * @return String
     * @author pure
     */
    public String today() {
        String strY = null;
        String strZ = null;
        x = localTime.get(Calendar.YEAR);
        y = localTime.get(Calendar.MONTH) + 1;
        z = localTime.get(Calendar.DATE);
        strY = y >= 10 ? String.valueOf(y) : ("0" + y);
        strZ = z >= 10 ? String.valueOf(z) : ("0" + z);
        return x + "-" + strY + "-" + strZ;
    }
    /**
     * 功能:得到當前月份 格式為:yyyy.MM (eg: 2007.12)<br>
     *
     * @return String
     * @author pure
     */
    public String thisMonth() {
        String strY = null;
        x = localTime.get(Calendar.YEAR);
        y = localTime.get(Calendar.MONTH) + 1;
        strY = y >= 10 ? String.valueOf(y) : ("0" + y);
        return x + "." + strY ;
    }
    /**
     * 功能:得到當前月份月初 格式為:yyyy-MM-dd (eg: 2007-12-01)<br>
     *
     * @return String
     * @author pure
     */
    public String thisMonthStart() {
        String strY = null;
        x = localTime.get(Calendar.YEAR);
        y = localTime.get(Calendar.MONTH) + 1;
        strY = y >= 10 ? String.valueOf(y) : ("0" + y);
        return x + "-" + strY + "-01";
    }

    /**
     * 功能:得到當前月份月底 格式為:yyyy-MM-dd (eg: 2007-12-31)<br>
     *
     * @return String
     * @author pure
     */
    public String thisMonthEnd() {
        String strY = null;
        String strZ = null;
        boolean leap = false;
        x = localTime.get(Calendar.YEAR);
        y = localTime.get(Calendar.MONTH) + 1;
        if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
            strZ = "31";
        }
        if (y == 4 || y == 6 || y == 9 || y == 11) {
            strZ = "30";
        }
        if (y == 2) {
            leap = leapYear(x);
            if (leap) {
                strZ = "29";
            } else {
                strZ = "28";
            }
        }
        strY = y >= 10 ? String.valueOf(y) : ("0" + y);
        return x + "-" + strY + "-" + strZ;
    }

    /**
     * 功能:得到當前季度季初 格式為:yyyy-MM-dd (eg: 2007-10-01)<br>
     *
     * @return String
     * @author pure
     */
    public String thisSeason() {
        String dateString = "";
        x = localTime.get(Calendar.YEAR);
        y = localTime.get(Calendar.MONTH) + 1;
        if (y >= 1 && y <= 3) {
            dateString = x + "-" + "01" + "-" + "01";
        }
        if (y >= 4 && y <= 6) {
            dateString = x + "-" + "04" + "-" + "01";
        }
        if (y >= 7 && y <= 9) {
            dateString = x + "-" + "07" + "-" + "01";
        }
        if (y >= 10 && y <= 12) {
            dateString = x + "-" + "10" + "-" + "01";
        }
        return dateString;
    }

    /**
     * 功能:得到當前季度季末 格式為:yyyy-MM-dd (eg: 2007-12-31)<br>
     *
     * @return String
     * @author pure
     */
    public String thisSeasonEnd() {
        String dateString = "";
        x = localTime.get(Calendar.YEAR);
        y = localTime.get(Calendar.MONTH) + 1;
        if (y >= 1 && y <= 3) {
            dateString = x + "-" + "03" + "-" + "31";
        }
        if (y >= 4 && y <= 6) {
            dateString = x + "-" + "06" + "-" + "30";
        }
        if (y >= 7 && y <= 9) {
            dateString = x + "-" + "09" + "-" + "30";
        }
        if (y >= 10 && y <= 12) {
            dateString = x + "-" + "12" + "-" + "31";
        }
        return dateString;
    }

    /**
     * 功能:得到當前年份年初 格式為:yyyy-MM-dd (eg: 2007-01-01)<br>
     *
     * @return String
     * @author pure
     */
    public String thisYear() {
        x = localTime.get(Calendar.YEAR);
        return x + "-01" + "-01";
    }

    /**
     * 功能:得到當前年份年底 格式為:yyyy-MM-dd (eg: 2007-12-31)<br>
     *
     * @return String
     * @author pure
     */
    public String thisYearEnd() {
        x = localTime.get(Calendar.YEAR);
        return x + "-12" + "-31";
    }

    /**
     * 功能:判斷輸入年份是否為閏年<br>
     *
     * @param year
     * @return 是:true  否:false
     * @author pure
     */
    public boolean leapYear(int year) {
        boolean leap;
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                if (year % 400 == 0) leap = true;
                else leap = false;
            } else leap = true;
        } else leap = false;
        return leap;
    }

    /**
     * 功能:返回月份
     *
     * @param i 返回近幾月就寫幾
     * @return 例:當前2月,i=0,返回2月;i=1,返回1月;i=2,返回上12月
     */
    public String localMonth(int i) {
        localTime.add(Calendar.MONTH, -i);
        y = localTime.get(Calendar.MONTH) + 1;
        return Integer.toString(y);
    }

    /**
     * 功能:幾個月的時間范圍,格式為:yyyy-MM-dd (eg: 2007-12-31)<br>
     *
     * @return 例:當前2018-3-10,1個月范圍,返回2018-2-28
     */
    public String lastMonthDay(int month) {

        String strY = null;
        String strZ = null;
        boolean leap = false;
        localTime.add(Calendar.MONTH, -month);
        x = localTime.get(Calendar.YEAR);
        y = localTime.get(Calendar.MONTH) + 1;
        if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
            strZ = "31";
        }
        if (y == 4 || y == 6 || y == 9 || y == 11) {
            strZ = "30";
        }
        if (y == 2) {
            leap = leapYear(x);
            if (leap) {
                strZ = "29";
            } else {
                strZ = "28";
            }
        }
        strY = y >= 10 ? String.valueOf(y) : ("0" + y);
        return x + "-" + strY + "-" + strZ;
    }

}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM