时间工具类

1.本周开始时间戳 - 以星期一为本周的第一天

public static String getWeekStartTime() {

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    Calendar cal = Calendar.getInstance();

    int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 1;

    if (day_of_week == 0) {

        day_of_week = 7;

}

    cal.add(Calendar.DATE, -day_of_week + 1);

    return simpleDateFormat.format(cal.getTime());

}

2.本周结束时间戳 - 以星期一为本周的第一天

public static String getWeekEndTime() {

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    Calendar cal = Calendar.getInstance();

    int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 1;

    if (day_of_week == 0) {

        day_of_week = 7;

}

    cal.add(Calendar.DATE, -day_of_week + 7);

    return simpleDateFormat.format(cal.getTime());

}

 3.本月开始时间戳

public static String getMonthStartTime() {

    Calendar c = Calendar.getInstance();

    c.add(Calendar.MONTH, 0);

    c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天

    //将小时至0

    c.set(Calendar.HOUR_OF_DAY, 0);

    //将分钟至0

    c.set(Calendar.MINUTE, 0);

    //将秒至0

    c.set(Calendar.SECOND, 0);

    //将毫秒至0

    c.set(Calendar.MILLISECOND, 0);

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    String dateString = format.format(c.getTime());

    Date date = null;

    try {

        date = format.parse(dateString);

    } catch (ParseException e) {

        e.printStackTrace();

}

    return format.format(date);

}


4.获取当前的星期数

public static int setCurrentDayOfWeek(Calendar calendar) {

    //一周第一天是否为星期天

    boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY);

    //获取周几

    int weekDay = calendar.get(Calendar.DAY_OF_WEEK);

    calendar.setFirstDayOfWeek(Calendar.MONDAY);

    //若一周第一天为星期天,则-1

    if (isFirstSunday) {

        weekDay = weekDay - 1;

        if (weekDay == 0) {

            weekDay = 7;

}

}

    return weekDay;

}


5. 获取date类型的时分秒

public static String getTime(Date date) {

    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    return dateFormat.format(date);

}


 6.获取date类型的年月日

public static String getDate(Date date) {

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    return dateFormat.format(date);

}

7.根据日期获取本周一的时间

public static Date getThisWeekMonday(Date date) {

    Calendar cal = Calendar.getInstance();

    cal.setTime(date);

    // 获得当前日期是一个星期的第几天

    int dayWeek = cal.get(Calendar.DAY_OF_WEEK);

    if (1 == dayWeek) {

        cal.add(Calendar.DAY_OF_MONTH, -1);

}

    // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一

    cal.setFirstDayOfWeek(Calendar.MONDAY);

    // 获得当前日期是一个星期的第几天

    int day = cal.get(Calendar.DAY_OF_WEEK);

    // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值

    cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);

    return cal.getTime();

}


8.时间字符串加/减天数

public static String dateStrToAddDay(String strDate, int dayNums) {

    Calendar c = Calendar.getInstance();

    c.setTime(DateTime.dateToString(strDate, "yyyy-MM-dd"));

    c.add(Calendar.DAY_OF_MONTH, dayNums);

    return DateTime.dateToString(c.getTime(), "yyyy-MM-dd");

}



 9.返回当前时间的前7天集合 

public static List getFrontSeven(String strDate) {

    List dateList = new ArrayList<>();

    Calendar c = Calendar.getInstance();

    c.setTime(DateTime.dateToString(strDate, "yyyy-MM-dd"));

    for (int i = 0; i < 7; i++) {

        c.add(Calendar.DATE, -1);

        dateList.add(DateTime.dateToString(c.getTime(), "yyyy-MM-dd"));

}

    return dateList;

}



10.给定开始时间 结束时间 返回天数集合

public static List getStartTime(String startTime, String endTime) {

    int interval = 0;

    try {

        if (startTime.length() == "yyyy-MM-dd HH:mm:ss".length()) {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date d1 = sdf.parse(startTime);

            Date d2 = sdf.parse(endTime);

            interval = daysBetween(d1, d2);

}

        if (startTime.length() == "yyyy-MM-dd".length()) {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Date d1 = sdf.parse(startTime);

            Date d2 = sdf.parse(endTime);

            interval = daysBetween(d1, d2);

}

    } catch (ParseException e) {

        e.printStackTrace();

}

    List dateList = new ArrayList<>();

    Calendar c = Calendar.getInstance();

    c.setTime(DateTime.dateToString(startTime, "yyyy-MM-dd"));

    for (int i = 0; i <= interval; i++) {

        dateList.add(DateTime.dateToString(c.getTime(), "yyyy-MM-dd"));

        c.add(Calendar.DATE, 1);

}

    return dateList;

}


11.获取今天日期  yyyy-MM-dd HH:mm:ss 

public static String getDataFroDatTime() {

    Date date = new Date();

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    String data = formatter.format(date);

    return data;

}


12.获取两个时间段的所有日期

public static List getBetweenDate(String begin, String end) {

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    List betweenList = new ArrayList();

    try {

        Calendar startDay = Calendar.getInstance();

        startDay.setTime(format.parse(begin));

        startDay.add(Calendar.DATE, -1);

        while (true) {

            startDay.add(Calendar.DATE, 1);

            Date newDate = startDay.getTime();

            String newend = format.format(newDate);

            betweenList.add(newend);

            if (end.equals(newend)) {

                break;

}

}

    } catch (Exception e) {

        e.printStackTrace();

}

    return betweenList;

}


 13.判断当前时间是否在时间段内

public static Boolean belongCalendar(String beginTime, String endTime) {

    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式

    Date now = null;

    Date beginTimeByDate = null;

    Date endTimeByDate = null;

    try {

        now = df.parse(df.format(new Date()));

        beginTimeByDate = df.parse(beginTime);

        endTimeByDate = df.parse(endTime);

    } catch (Exception e) {

        e.printStackTrace();

}

    Boolean flag = belongCalendar(now, beginTimeByDate, endTimeByDate);

    return flag;

}


14.判断时间是否在时间段内

public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {

    Calendar date = Calendar.getInstance();

    date.setTime(nowTime);

    Calendar begin = Calendar.getInstance();

    begin.setTime(beginTime);

    Calendar end = Calendar.getInstance();

    end.setTime(endTime);

    if (date.after(begin) && date.before(end)) {

        return true;

    } else {

        return false;

}

}


15.获取某个时间段内所有日期

public static List getDayBetweenDates(String begin, String end) {

    List lDate = new ArrayList();

    try {

        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

        Date dBegin = sd.parse(begin);

        Date dEnd = sd.parse(end);

        lDate.add(sd.format(dBegin));

        Calendar calBegin = Calendar.getInstance();

        // 使用给定的 Date 设置此 Calendar 的时间

        calBegin.setTime(dBegin);

        Calendar calEnd = Calendar.getInstance();

        // 使用给定的 Date 设置此 Calendar 的时间

        calEnd.setTime(dEnd);

        // 测试此日期是否在指定日期之后

        while (dEnd.after(calBegin.getTime())) {

            // 根据日历的规则,为给定的日历字段添加或减去指定的时间量

            calBegin.add(Calendar.DAY_OF_MONTH, 1);

            lDate.add(sd.format(calBegin.getTime()));

}

    } catch (ParseException e) {

        e.printStackTrace();

}

    return lDate;

}

16.减一天

public static String dataBeforeDay(String dateOld) {

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    /**

    * 减少一天

    */

    try {

        long dif = df.parse(dateOld).getTime() - 86400 * 1000; //减一天

        Date date = new Date();

        date.setTime(dif);

        return df.format(date);

    } catch (ParseException e) {

        e.printStackTrace();

}

    return "";

}

17.增加一天

public static String dataAfterDay(String dateOld) {

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    try {

        Date dd = df.parse(dateOld);

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(dd);

        calendar.add(Calendar.DAY_OF_MONTH, 1);//增加一天

        System.out.println("增加一天之后:" + df.format(calendar.getTime()));

    } catch (ParseException e) {

        e.printStackTrace();

}

    return "";

}


18:判断俩个日期段是否重叠


/**

* 判断俩个日期段是否重叠

*/

public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

public static boolean isOverlap(String startdate1, String enddate1, String startdate2, String enddate2) {

    Date leftStartDate = null;

    Date leftEndDate = null;

    Date rightStartDate = null;

    Date rightEndDate = null;

    try {

        leftStartDate = format.parse(startdate1);

        leftEndDate = format.parse(enddate1);

        rightStartDate = format.parse(startdate2);

        rightEndDate = format.parse(enddate2);

    } catch (ParseException e) {

        return false;

    }

    return

            ((leftStartDate.getTime() >= rightStartDate.getTime()) && leftStartDate.getTime() < rightEndDate.getTime())

                    || ((leftStartDate.getTime() > rightStartDate.getTime()) && leftStartDate.getTime() < rightEndDate.getTime())

                    || ((rightStartDate.getTime() >= leftStartDate.getTime()) && rightStartDate.getTime() < leftEndDate.getTime())

                    || ((rightStartDate.getTime() > leftStartDate.getTime()) && rightStartDate.getTime() < leftEndDate.getTime());

}


19. 获得某天最大时间2017-10-15 23:59:59

public static Date getEndOfDay(Date date) {

    LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());

    LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);

    return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

}

20. 获得某天最小时间2017-10-15 00:00:00

public static Date getStartOfDay(Date date) {

    LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());

    LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);

    return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());

}

你可能感兴趣的:(时间工具类)