現在の日付に対応する月初め、月末、季初、季末、年初、年末時間を取得するために使用されます.

5335 ワード

package com.zrar.date;
import java.util.Calendar;
/**
 * 
 *   :                ,  ,  ,  ,  ,  ,     String   
 *      1、               today()
 *      2、              thisMonth()
 *      3、              thisMonthEnd()
 *      4、              thisSeason()
 *      5、              thisSeasonEnd()
 *      6、              thisYear()
 *      7、              thisYearEnd()
 *      8、            leapYear
 *      
 *     :       :xxxx-yy-zz (eg: 2007-12-05)
 *
 *   :
 *
 * @author pure
 */
public class DateThis {
    private int x;                  //     : 
    private int y;                  //     : 
    private int z;                  //     : 
    private Calendar localTime;     //     
    public DateThis() {
        localTime = Calendar.getInstance();
    }
    /**
     *   :          :xxxx-yy-zz (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;
    }
    /**
     *   :            :xxxx-yy-zz (eg: 2007-12-01)<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 + "-01";
    }
    /**
     *   :            :xxxx-yy-zz (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;
    }
    /**
     *   :            :xxxx-yy-zz (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;
    }
    /**
     *   :            :xxxx-yy-zz (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;
    }
    /**
     *   :            :xxxx-yy-zz (eg: 2007-01-01)<br> 
     * @return String
     * @author pure
     */
    public String thisYear() {
        x = localTime.get(Calendar.YEAR);
        return x + "-01" + "-01";
    }
    /**
     *   :            :xxxx-yy-zz (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;
    }
}