写了一个日期工具类

效果:

写了一个日期工具类_第1张图片

代码:

package com.juanzhijia.juan.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类.
 */
public class DateUtil {

    /**
     * 获取一天开始时间.
     *
     * @return Date
     */
    public static Date getStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        return cal.getTime();
    }

    /**
     * 获取一天结束时间.
     *
     * @return Date
     */
    public static Date getEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        return cal.getTime();
    }

    /**
     * 获取前一天开始时间.
     *
     * @return Date
     */
    public static Date getLastStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH) - 1, 0, 0, 0);
        return cal.getTime();
    }

    /**
     * 获取前一天结束时间.
     *
     * @return Date
     */
    public static Date getLastEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH) - 1, 23, 59, 59);
        return cal.getTime();
    }

    /**
     * 获得本月第一天0点时间.
     *
     * @return Date
     */
    public static Date getMonthBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    /**
     * 获得本月最后一天23:59:59.
     *
     * @return Date
     */
    public static Date getMonthEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        return cal.getTime();
    }


    /**
     * 获取上月第一天0点.
     *
     * @return Date
     */
    public static Date getLastMonthBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY) - 1, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    /**
     * 获取上月最后一天23:59:59.
     *
     * @return Date
     */
    public static Date getLastMonthEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) - 1, cal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    /**
     * 获取本年的开始时间.
     *
     * @return Date
     */
    public static Date getYearBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), Calendar.JANUARY, 1, 0, 0, 0);
        return cal.getTime();
    }

    /**
     * 获取本年的结束时间.
     *
     * @return Date
     */
    public static Date getYearEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), Calendar.DECEMBER, 31, 23, 59, 59);
        return cal.getTime();
    }

    /**
     * 获取上一年开始时间.
     *
     * @return Date
     */
    public static Date getLastYearBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR) - 1, Calendar.JANUARY, 1, 0, 0, 0);
        return cal.getTime();
    }

    /**
     * 获取上一年的结束时间.
     *
     * @return Date
     */
    public static Date getLastYearEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR) - 1, Calendar.DECEMBER, 31, 23, 59, 59);
        return cal.getTime();
    }
    
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("获取一天开始时间----------" + sdf.format(getStartTime()));
        System.out.println("获取一天结束时间----------" + sdf.format(getEndTime()));
        System.out.println("获取前一天开始时间--------" + sdf.format(getLastStartTime()));
        System.out.println("获取前一天结束时间--------" + sdf.format(getLastEndTime()));
        System.out.println("获得本月第一天0点时间-----" + sdf.format(getMonthBegin()));
        System.out.println("获得本月最后一天23:59:59--" + sdf.format(getMonthEnd()));
        System.out.println("获取上月第一天0点---------" + sdf.format(getLastMonthBegin()));
        System.out.println("获取上月最后一天23:59:59--" + sdf.format(getLastMonthEnd()));
        System.out.println("获取本年的开始时间--------" + sdf.format(getYearBegin()));
        System.out.println("获取本年的结束时间--------" + sdf.format(getYearEnd()));
        System.out.println("获取上一年开始时间--------" + sdf.format(getLastYearBegin()));
        System.out.println("获取上一年的结束时间------" + sdf.format(getLastYearEnd()));
    }
}


你可能感兴趣的:(java开发应用)