DateUtil常用的一些静态方法,日期转换字符串等

总结了常用的一些日期和日期字符串以及年月计算环比同比的日期类型常用方法
/**
 * 获取当前小时数 24进制
 * @param date
 * @return
 */
public static int getCurTimeHour(Date date) {
    SimpleDateFormat format=new SimpleDateFormat("HH");
    String formatHour = format.format(date);
    return Ints.tryParse(formatHour);
}
/**
 * 获取今日的日期字符串类型
 * @param date
 * @return
 */
public static String getCurTimeDay(Date date) {
    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
    String formatDay = format.format(date);
    return formatDay;
}

/**
 * 获取今日的环比时间
 * @return
 */
public static LocalDateTime getPreviousDay() {
    return LocalDateTime.now().minusDays(1);
}

/**
 * 获取前一日的开始时间
 * @return
 */
public static LocalDateTime getPreviousDayStartTime() {
    LocalDate yesterday = LocalDate.now().minusDays(1);
    LocalTime midnight = LocalTime.MIN;
    return LocalDateTime.of(yesterday, midnight);
}

/**
 * 获取本周的环比时间
 * @return
 */
public static LocalDateTime getPreviousWeek() {
    return LocalDateTime.now().minusWeeks(1);
}

/**
 * 获取本月的环比时间
 * @return
 */
public static LocalDateTime getPreviousMonth() {
    return LocalDateTime.now().minusMonths(1);
}

/**
 * 求2个日期之间的天数
 * @param start
 * @param end
 * @return 不包含当天
 */
public static long getBetweenDay(LocalDate start, LocalDate end) {
    return end.toEpochDay() - start.toEpochDay();
}

/**
 * 获取本月月初日期
 * @return
 */
public static LocalDate getStartOfMonth() {
    return LocalDate.now().withDayOfMonth(1);
}

/**
 * 获取上月月初日期
 * @return
 */
public static LocalDate getStartOfPreviousMonth() {
    return LocalDate.now().minusMonths(1).withDayOfMonth(1);
}

/**
 * 获取上月和今天同一日期
 * @return
 */
public static LocalDate getSameDateOfPreviousMonth() {
    LocalDate today = LocalDate.now();
    return today.minusMonths(1);
}

/**
 * 获取上一年同月月初日期
 * @return
 */
public static LocalDate getStartOfSameMonthPreviousYear() {
    LocalDate today = LocalDate.now();
    LocalDate startOfPreviousYear = today.minusYears(1);
    return startOfPreviousYear.withMonth(today.getMonthValue()).withDayOfMonth(1);
}

/**
 * 获取上一年同月和今天同一日期
 * @return
 */
public static LocalDate getSameDateOfPreviousYear() {
    LocalDate today = LocalDate.now();
    LocalDate sameDayPreviousYear = today.minusYears(1);
    return sameDayPreviousYear.withMonth(today.getMonthValue()).withDayOfMonth(today.getDayOfMonth());
}

/**
 * 获取本年第一天日期
 * @return
 */
public static LocalDate getStartOfYear() {
    return LocalDate.now().withDayOfYear(1);
}

/**
 * 获取本月的总天数
 */
public static int getTotalDaysOfMonth() {
    YearMonth currentMonth = YearMonth.now();
    return currentMonth.lengthOfMonth();
}

/**
 * 获取今天是本月的第几天
 * @return
 */
public static int getDayOfMonth() {
    LocalDate today = LocalDate.now();
    return today.getDayOfMonth();
}

/**
 * 获取本月是本年的第几个月
 * @return
 */
public static int getCurrentMonthNumber() {
    LocalDate today = LocalDate.now();
    return today.getMonthValue();
}

/**
 * 字符串 日期时间 转换为日期格式
 * @param dateTimeString
 * @return
 */
public static String convertToDateString(String dateTimeString) {
    LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, DateTimeFormatter.ofPattern("yyyy.M.d H:mm"));
    return dateTime.format(DateTimeFormatter.ofPattern("yyyy.M.d"));
}

/**
 * 字符串日期时间 转换为 时间格式
 * @param dateTimeString
 * @return
 */
public static String convertToTimeString(String dateTimeString) {
    LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, DateTimeFormatter.ofPattern("yyyy.M.d H:mm"));
    return dateTime.format(DateTimeFormatter.ofPattern("HH:mm"));
}

/**
 * 将日期类型转换为字符串
 * @param date
 * @param pattern
 * @return
 */
public static String formatDateToStr(Date date, String pattern) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    return sdf.format(date);
}

/**
 * 将日期字符串转换为日期
 * @param dateString
 * @param pattern
 * @return
 * @throws ParseException
 */
public static Date parseStrToDate(String dateString, String pattern) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    return sdf.parse(dateString);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力终会有回报

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值