Java小Case之日期

根据当前时间获取相应的时间点

  1. 秒数转为时分秒
  2. 获取当前月份的月初 格式为 yyyy-MM-01
  3. 获取本月份月底
  4. 获取当前季度月初 yyyy-MM-01
  5. 本季度月末 yyyy-MM-dd
  6. 获取指定年月最后一天
    来啊,上代码~~
public class DateUtil {
    private static int x;                  // 日期属性:年
    private static int y;                  // 日期属性:月
    private static int z;                  // 日期属性:日
    private static Calendar localTime = Calendar.getInstance();     // 当前日期

    /**
     * 获取当前日期 格式为 yyyy-MM-dd
     *
     * @return
     */
    public static 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;
    }
    /**
     * @method 秒数转为时分秒
     * @param second
     * @return
     */
    public static String secondToTime(long second){
    	try {
    		long days = second / 86400;           
            second = second % 86400;            
            long hours = second / 3600;            
            second = second % 3600;                
            long minutes = second /60;           
            second = second % 60;             
            if(days>0){
                return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
            }else if(hours>0){
                return hours + "小时" + minutes + "分" + second + "秒";
            }else if (minutes > 0) {
            	return minutes + "分" + second + "秒";
            }else {
            	return second + "秒";
            }
		} catch (Exception e) {
			return "";
		}
        
    }

    /**
     * 获取当前月份的月初 格式为 yyyy-MM-01
     *
     * @return
     */
    public static 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";
    }

    /**
     * 获取本月份月底
     *
     * @return
     */
    public static 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-01
     *
     * @return
     */
    public static 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
     *
     * @return
     */
    public static 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-01-01
     *
     * @return
     */
    public static String thisYear() {
        x = localTime.get(Calendar.YEAR);
        return x + "-01" + "-01";
    }

    /**
     * 获取当前年底 yyyy-12-31
     * @return
     */
    public static String thisYearEnd() {
        x = localTime.get(Calendar.YEAR);
        return x + "-12" + "-31";
    }

    public static String getLastDayOfMonth(String year, String month) {
        Calendar cal = Calendar.getInstance();
        //年
        cal.set(Calendar.YEAR, Integer.parseInt(year));
        //月,因为Calendar里的月是从0开始,所以要-1
        cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        //日,设为一号
        cal.set(Calendar.DATE, 1);
        //月份加一,得到下个月的一号
        cal.add(Calendar.MONTH,1);
        //下一个月减一为本月最后一天
        cal.add(Calendar.DATE, -1);
        return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));//获得月末是几号
    }

    public static void main(String[] argc) {
        System.out.println(getLastDayOfMonth("2019", "09"));
        System.out.println(thisMonthEnd());
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值