JAVA 日期转换、日期获取

public static String getDateString(Date date) {
    if (date == null) {
        return null;
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(date);
}
public static Date StringToDate(String dateStr) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
    try {
        return simpleDateFormat.parse(dateStr);
    } catch (ParseException e) {
        return null;
    }
}

 

/**
 * 获取当前日期最小时间
 *
 * @param curDate
 * @return
 */
public static Date getMinDateTime(Date curDate) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String curStr = sdf.format(curDate);
    DateTime now = new DateTime(curStr);
    Date beginDate = now.millisOfDay().withMinimumValue().toDate();
    return beginDate;
}
/**
 * 获取当前日期最大时间
 *
 * @param curDate
 * @return
 */
public static Date getMaxDateTime(Date curDate) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String curStr = sdf.format(curDate);
    DateTime now = new DateTime(curStr);
    Date endDate = now.millisOfDay().withMaximumValue().toDate();
    return endDate;
}

 

/**
 * 增加或减少日期
 *
 * @param date
 * @return
 */
public static String addOrSubDay(Date date, Integer n) {
    Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_MONTH, n); //当前时间减去一天,即一天前的时间
    Date time = calendar.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(time);
}

 

/**
 * 增加或减少小时
 *
 * @param date
 * @param n
 * @return
 */
public static Date addOrSubHourDate(Date date, Integer n) {
    Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
    calendar.setTime(date);
    calendar.add(Calendar.HOUR_OF_DAY, n); //当前时间减去一小时,即小时前的时间
    return calendar.getTime();
}

 

/**
 * 计算时差
 *
 * @param max
 * @param min
 * @return
 */
public static String timeDiff(Date max, Date min) {
    if (max == null || min == null) {
        return "";
    }
    long s = max.getTime() - min.getTime();
    long hours = s / (3600 * 1000);
    long leave2 = s % (3600 * 1000); //计算小时数后剩余的毫秒数
    long minutes = leave2 / (60 * 1000);//计算相差分钟数
    long leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
    long seconds = leave3 / 1000;
    String result = "";
    if (hours != 0L) {
        result += hours + "时";
    }
    if (minutes != 0L) {
        result += minutes + "分";
    }
    if (seconds != 0L) {
        result += seconds + "秒";
    }

    if (hours == 0L && minutes == 0L && seconds == 0L) {
        result = "0秒";
    }
    return result;
}

 

/**
 * 计算时差(精确到分)秒级的,如果超过30秒,计为1分,没超过,计为0分
 *
 * @param max
 * @param min
 * @return
 */
public static String timeDiffMinute(Date max, Date min) {
    if (max == null || min == null) {
        return "";
    }
    long s = max.getTime() - min.getTime();
    long minutes = s / (60 * 1000);//计算相差分钟数
    long leaveSec = s % (60 * 1000); //计算分钟数后剩余的毫秒数
    long seconds = leaveSec / 1000;
    String result = "";
    if (seconds >= 30L) {
        result = (minutes + 1) + "分";
    } else {
        result = minutes + "分";
    }
    return result;
}

 

/**
 * 计算时分秒
 * 
 * @param secNum
 * @return
 */
public static String transTimeLongToStr(Long secNum) {
    long hours = secNum / 3600;
    long leave2 = secNum % (3600); //计算小时数后剩余的毫秒数
    long minutes = leave2 / (60);//计算相差分钟数
    long seconds = leave2 % (60); //计算分钟数后剩余的毫秒数
    String result = "";
    if (hours != 0L) {
        result += hours + "时";
    }
    if (minutes != 0L) {
        result += minutes + "分";
    }
    if (seconds != 0L) {
        result += seconds + "秒";
    }

    if (hours == 0L && minutes == 0L && seconds == 0L) {
        result = "0秒";
    }
    return result;
}

 

/**
 * 计算两个日期的时间间隔
 * 返回 X年X月X天X小时X分钟
 *
 * @param fromDate
 * @param toDate
 */
public static String getIntervalStr(Date fromDate, Date toDate) {
    if (fromDate == null || toDate == null) {
        return "";
    }
    //An instantaneous point on the time-line.(时间线上的一个瞬时点。)
    Instant fromInstant = fromDate.toInstant();
    //An instantaneous point on the time-line.(时间线上的一个瞬时点。)
    Instant toInstant = toDate.toInstant();
    //A time-zone ID, such as {@code Europe/Paris}.(时区)
    ZoneId zoneId = ZoneId.systemDefault();
    LocalDateTime fromDateTime = fromInstant.atZone(zoneId).toLocalDateTime();
    LocalDateTime toDateTime = toInstant.atZone(zoneId).toLocalDateTime();
    LocalDateTime tempDateTime = LocalDateTime.from(fromDateTime);
    long years = tempDateTime.until(toDateTime, ChronoUnit.YEARS);
    tempDateTime = tempDateTime.plusYears(years);
    long months = tempDateTime.until(toDateTime, ChronoUnit.MONTHS);
    tempDateTime = tempDateTime.plusMonths(months);
    long days = tempDateTime.until(toDateTime, ChronoUnit.DAYS);
    tempDateTime = tempDateTime.plusDays(days);
    long hours = tempDateTime.until(toDateTime, ChronoUnit.HOURS);
    tempDateTime = tempDateTime.plusHours(hours);
    long minutes = tempDateTime.until(toDateTime, ChronoUnit.MINUTES);
    tempDateTime = tempDateTime.plusMinutes(minutes);
    long seconds = tempDateTime.until(toDateTime, ChronoUnit.SECONDS);
    if (years != 0L) {
        return years + "年" + months + "个月" + days + "天" + hours + "小时" + minutes + "分钟";
    } else if (months != 0L) {
        return months + "个月" + days + "天" + hours + "小时" + minutes + "分钟";
    } else if (days != 0L) {
        return days + "天" + hours + "小时" + minutes + "分钟";
    } else if (hours != 0L) {
        return hours + "小时" + minutes + "分钟";
    } else if (minutes != 0L) {
        return minutes + "分钟" + seconds + "秒";
    } else if (seconds != 0L) {
        return seconds + "秒";
    } else {
        return "";
    }
}

 

 

/**
 * 计算平均小时
 * 
 * @param entryUserTasks
 * @return
 */
public static BigDecimal getAvgHour(List<EntryUserTask> entryUserTasks) {
    BigDecimal hoursTotal = BigDecimal.ZERO;
    for (EntryUserTask row : entryUserTasks) {
        long s = row.getCheckTime().getTime() - row.getGetTime().getTime();
        long hours = s / (3600 * 1000);
        long leave2 = s % (3600 * 1000); //计算小时数后剩余的毫秒数
        long minutes = leave2 / (60 * 1000);//计算相差分钟数
        BigDecimal decimal = new BigDecimal(minutes).divide(new BigDecimal(60), 2, RoundingMode.HALF_UP);
        BigDecimal add = new BigDecimal(hours).add(decimal);
        hoursTotal = hoursTotal.add(add);
    }
    return hoursTotal.divide(new BigDecimal(entryUserTasks.size()), 2, RoundingMode.HALF_UP);
}
/**
 * 获取当月开始时间
 *
 * @param date
 * @return
 */
public static String getDateMonthEnd(Date date) {
    Calendar calendar = Calendar.getInstance();// 获取当前日期
    calendar.setTime(date);
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 999);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(calendar.getTime());
}

 

/**
 * 获取当月结束时间
 *
 * @param date
 * @return
 */
public static String getDateMonthBegin(Date date) {
    Calendar calendar = Calendar.getInstance();// 获取当前日期
    calendar.setTime(date);
    calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(calendar.getTime());
}

 

/**
 * 获取本周周一  xx:xx:xx
 *
 * @param today
 * @return
 */
public static Date getWeekDateFirst(Date today) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();
    cal.setTime(today);
    //设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    //获得当前日期是一个星期的第几天
    int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayWeek == 1) {
        dayWeek = 8;
    }
    // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
    cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
    Date mondayDate = cal.getTime();
    String weekBegin = sdf.format(mondayDate);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        Date parse = simpleDateFormat.parse(weekBegin + " 00:00:00");
        return parse;
    } catch (ParseException e) {
        return null;
    }
}

 

 

public static String getNextWeekFirst(Date today) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();
    cal.setTime(today);
    //设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    //获得当前日期是一个星期的第几天
    int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayWeek == 1) {
        dayWeek = 8;
    }
    // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
    cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
    cal.add(Calendar.DATE, 5 + cal.getFirstDayOfWeek());
    Date sundayDate = cal.getTime();
    String nextFirst = sdf.format(sundayDate);
    return nextFirst + " 00:00:00";
}
/**
 * 获取下周周一  xx:xx:xx 00:00:00
 *
 * @param today
 * @return
 */
public static String getWeekEnd(Date today) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();
    cal.setTime(today);
    //设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    //获得当前日期是一个星期的第几天
    int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayWeek == 1) {
        dayWeek = 8;
    }
    // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
    cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
    cal.add(Calendar.DATE, 4 + cal.getFirstDayOfWeek());
    Date sundayDate = cal.getTime();
    String weekEnd = sdf.format(sundayDate);
    return weekEnd + " 23:59:59";
}

 

/**
 * 获取上月开始时间
 *
 * @param today
 * @return
 */
public static String getLastMonthFist(Date today) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(today);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.add(Calendar.MONTH, -1);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String format = sdf.format(calendar.getTime());
    return format + " 00:00:00";
}

 

/**
 * 获取上月结束时间
 *
 * @param today
 * @return
 */
public static String getLastMonthEnd(Date today) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(today);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String format = sdf.format(calendar.getTime());
    return format + " 23:59:59";
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值