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);
}

你可能感兴趣的:(java)