此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为String字符串

来源:互联网 发布:淘宝管控记录得扣分吗 编辑:程序博客网 时间:2024/05/01 02:04
01.package vivi.test;  
 02. 
 03.import java.util.Calendar;  
 04./** 
 05.  * 
 06.  * 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为String字符串 
 07.  *      1、得到当前日期         today() 
 08.  *      2、得到当前月份月初      thisMonth() 
 09.  *      3、得到当前月份月底      thisMonthEnd() 
 10.  *      4、得到当前季度季初      thisSeason() 
 11.  *      5、得到当前季度季末      thisSeasonEnd() 
 12.  *      6、得到当前年份年初      thisYear() 
 13.  *      7、得到当前年份年底      thisYearEnd() 
 14.  *      8、判断输入年份是否为闰年 leapYear   
 15.  * 注意事项:  日期格式为:xxxx-yy-zz (eg: 2007-12-05) 
 16.  * 实例: 
 17.  * @author pure 
 18.  */ 
 19.public class Test {  
 20.    private int x;                  // 日期属性:年  
 21.    private int y;                  // 日期属性:月  
 22.    private int z;                  // 日期属性:日  
 23.    private Calendar localTime;     // 当前日期  
 24.      
 25.    public Test() {  
 26.        localTime = Calendar.getInstance();  
 27.    }  
 28.      
 29.    public static void main(String[] args){  
 30.        Test test = new Test();  
 31.        test.today();  
 32.    }  
 33.    /** 
 34.     * 功能:得到当前日期 格式为:xxxx-yy-zz (eg: 2007-12-05) 
 35.     * @return String 
 36.     * @author pure 
 37.     */ 
 38.    public String today() {  
 39.        String strY = null;  
 40.        String strZ = null;  
 41.        x = localTime.get(Calendar.YEAR);  
 42.        y = localTime.get(Calendar.MONTH) + 1;  
 43.        z = localTime.get(Calendar.DATE);  
 44.        strY = y >= 10 ? String.valueOf(y) : ("0" + y);  
 45.        strZ = z >= 10 ? String.valueOf(z) : ("0" + z);  
 46.        return x + "-" + strY + "-" + strZ;  
 47.    }  
 48.    /** 
 49.      * 功能:得到当前月份月初 格式为:xxxx-yy-zz (eg: 2007-12-01) 
 50.      * @return String 
 51.      * @author pure 
 52.      */ 
 53.    public String thisMonth() {  
 54.        String strY = null;  
 55.        x = localTime.get(Calendar.YEAR);  
 56.        y = localTime.get(Calendar.MONTH) + 1;  
 57.        strY = y >= 10 ? String.valueOf(y) : ("0" + y);return x + "-" + strY + "-01";  
 58.    }  
 59.    /** 
 60.      * 功能:得到当前月份月底 格式为:xxxx-yy-zz (eg: 2007-12-31) 
 61.      * @return String 
 62.      * @author pure 
 63.      **/ 
 64.    public String thisMonthEnd() {  
 65.        String strY = null;  
 66.        String strZ = null;  
 67.        boolean leap = false;  
 68.        x = localTime.get(Calendar.YEAR);  
 69.        y = localTime.get(Calendar.MONTH) + 1;  
 70.        if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {  
 71.            strZ = "31";  
 72.        }  
 73.        if (y == 4 || y == 6 || y == 9 || y == 11) {  
 74.            strZ = "30";  
 75.        }  
 76.        if (y == 2) {  
 77.            leap = leapYear(x);  
 78.            if (leap) {  
 79.                strZ = "29";  
 80.            }else {  
 81.                strZ = "28";  
 82.            }  
 83.        }  
 84.        strY = y >= 10 ? String.valueOf(y) : ("0" + y);  
 85.        return x + "-" + strY + "-" + strZ;  
 86.    }  
 87. 
 88.    /** 
 89.      * 功能:得到当前季度季初 格式为:xxxx-yy-zz (eg: 2007-10-01)<br> 
 90.      * @return String 
 91.      * @author pure 
 92.      */ 
 93.    public String thisSeason() {  
 94.        String dateString = "";  
 95.        x = localTime.get(Calendar.YEAR);  
 96.        y = localTime.get(Calendar.MONTH) + 1;  
 97.        if (y >= 1 && y <= 3) {  
 98.            dateString = x + "-" + "01" + "-" + "01";  
 99.        }  
 100.        if (y >= 4 && y <= 6) {  
 101.            dateString = x + "-" + "04" + "-" + "01";  
 102.        }  
 103.        if (y >= 7 && y <= 9) {  
 104.            dateString = x + "-" + "07" + "-" + "01";  
 105.        }  
 106.        if (y >= 10 && y <= 12) {  
 107.            dateString = x + "-" + "10" + "-" + "01";  
 108.        }  
 109.        return dateString;  
 110.        }  
 111. 
 112.    /** 
 113.      * 功能:得到当前季度季末 格式为:xxxx-yy-zz (eg: 2007-12-31)<br> 
 114.      * @return String 
 115.      * @author pure 
 116.      */ 
 117.    public String thisSeasonEnd() {  
 118.        String dateString = "";  
 119.        x = localTime.get(Calendar.YEAR);  
 120.        y = localTime.get(Calendar.MONTH) + 1;  
 121.        if (y >= 1 && y <= 3) {  
 122.            dateString = x + "-" + "03" + "-" + "31";  
 123.        }  
 124.        if (y >= 4 && y <= 6) {  
 125.            dateString = x + "-" + "06" + "-" + "30";  
 126.        }  
 127.        if (y >= 7 && y <= 9) {  
 128.            dateString = x + "-" + "09" + "-" + "30";  
 129.        }  
 130.        if (y >= 10 && y <= 12) {  
 131.            dateString = x + "-" + "12" + "-" + "31";  
 132.        }  
 133.        return dateString;  
 134.    }  
 135. 
 136.    /** 
 137.      * 功能:得到当前年份年初 格式为:xxxx-yy-zz (eg: 2007-01-01)<br> 
 138.      * @return String 
 139.      * @author pure 
 140.      */ 
 141.    public String thisYear() {  
 142.        x = localTime.get(Calendar.YEAR);  
 143.        return x + "-01" + "-01";  
 144.    }  
 145.      
 146.    /** 
 147.      * 功能:得到当前年份年底 格式为:xxxx-yy-zz (eg: 2007-12-31)<br> 
 148.      * @return String 
 149.      * @author pure 
 150.      */ 
 151.    public String thisYearEnd() {  
 152.        x = localTime.get(Calendar.YEAR);  
 153.        return x + "-12" + "-31";  
 154.    }  
 155.      
 156.    /** 
 157.      * 功能:判断输入年份是否为闰年<br> 
 158.      * @param year 
 159.      * @return 是:true  否:false 
 160.      * @author pure 
 161.      */ 
 162.    public boolean leapYear(int year) {  
 163.        boolean leap;  
 164.        if (year % 4 == 0) {  
 165.            if (year % 100 == 0) {  
 166.                if (year % 400 == 0) leap = true;  
 167.                    else leap = false;  
 168.                }  
 169.            else leap = true;  
 170.        }  
 171.        else leap = false;  
 172.        return leap;  
 173.    }  
 174.}