【java时间工具】Java获取当天、本周、本月、本季度、本年等 开始及结束时间



import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @time 2020年5月11日 下午3:55:18
 */
public class DateUtil {

    /**
     * YYYY_MM_ddHHmmss字符串转  YYYYMMdd
     *
     * @param Str
     * @return
     */
    public static String getYYYYMMddStr(String Str) {
        String needStr = "";
        try {
            Date date = getDateYYYY_MM_ddHHmmss(Str);
            needStr = getStrYYYYMMdd(date);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return needStr;
    }

    /**
     * YYYYMMdd字符串转  YYYY-MM-dd
     *
     * @param Str
     * @return
     */
    public static String getYYYY_MM_ddStr(String Str) {
        String needStr = "";
        try {
            Date date = getDateYYYYMMdd(Str);
            needStr = getDateStr(date, "yyy-MM-dd");
        } catch (Exception e) {
            // TODO: handle exception
        }
        return needStr;
    }

    /**
     * YYYYMMdd字符串转  月份
     *
     * @param Str
     * @return
     */
    public static String getMonthStr(String Str) {
        String[] months = {"一月", "二月", "三月", "四月", "五月", "六月",
                "七月", "八月", "九月", "十月", "十一月", "十二月"};
        String needStr = "";
        try {
            Date date = getDateYYYYMMdd(Str);
            needStr = getDateStr(date, "yyy-MM-dd");
        } catch (Exception e) {
            // TODO: handle exception
        }
        Integer monthIndex = Integer.parseInt(needStr.split("-")[1]) - 1;

        return months[monthIndex];
    }

    public static String getDateStr(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    public static String getStrYYYYMMdd(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(date);
    }

    public static String getStrYYYYMMddHHmmss(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(date);
    }

    public static String getStrYYYY_MM_ddHHmmss(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }

    public static String getCurrentDateTime() {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }

    public static Date getDateYYYYMMdd(String str) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.parse(str);
    }

    public static Date getDateYYYYMMddHHmmss(String str) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.parse(str);
    }

    public static Date getDateYYYY_MM_ddHHmmss(String str) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.parse(str);
    }

    public static Date getDate(Date date) throws ParseException {
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sDate = sdf2.format(date);
        //Date date1 = getDateYYYY_MM_ddHHmmss(sDate);
        Date date1 = Timestamp.valueOf(sDate);
        return date1;
    }

    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    public static ArrayList<String> getFetureDateAll(int intervals) {
        ArrayList<String> fetureDaysList = new ArrayList<>();
        for (int i = 0; i < intervals; i++) {
            fetureDaysList.add(getFetureDate(i));
        }
        return fetureDaysList;
    }

    public static String dateToWeek(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        Date date;
        try {
            date = sdf.parse(datetime);
            cal.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDays[w];
    }


    public static String getOffsetStr(String format, Integer offsetDay) {
        //获取多少天之前的
        Calendar calendar1 = Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat(format);
        calendar1.add(Calendar.DATE, offsetDay);
        String agoDate = sdf1.format(calendar1.getTime());
        return agoDate;
    }


    /**
     * 获得当天0点时间
     *
     * @return
     */
    public static Date getTimesMorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static String getTimesMorningStr() {
        Date date = getTimesMorning();
        return getStrYYYY_MM_ddHHmmss(date);
    }


    /**
     * 获得本周一0点时间
     *
     * @return
     */
    public static Date getTimesWeekMorning() {
        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_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    public static String getTimesWeekMorningStr() {
        Date date = getTimesWeekMorning();
        return getStrYYYY_MM_ddHHmmss(date);
    }

    /**
     * 获得本月第一天0点时间
     *
     * @return
     */
    public static Date getTimesMonthMorning() {
        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();
    }

    public static String getTimesMonthMorningStr() {
        Date date = getTimesMonthMorning();
        return getStrYYYY_MM_ddHHmmss(date);
    }


    /**
     * 获取本季度零点
     *
     * @return
     */
    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    public static String getCurrentQuarterStartTimeStr() {
        Date date = getStartDayOfQuarter();
        return getStrYYYY_MM_ddHHmmss(date);
    }

    /**
     * 本年度开始时间
     *
     * @return
     */
    public static Date getCurrentYearStartTime() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }


    public static String getCurrentYearStartTimeStr() {
        Date date = getCurrentYearStartTime();
        return getStrYYYY_MM_ddHHmmss(date);
    }

    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }


    /**
     * 获取某季度的第一天
     * @return
     */
    public static Date getStartDayOfQuarter() {
        LocalDate resDate = LocalDate.now();
        Month month = resDate.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        resDate = LocalDate.of(resDate.getYear(), firstMonthOfQuarter, 1);

        return Date.from(resDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
    }


    public static void main(String[] args) {
        String result = getCurrentQuarterStartTimeStr();
        System.out.println(result);
    }

}

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值