Java 时间工具类

这是一个包含多个Java日期和时间操作的实用工具类,包括Date到ZonedDateTime的转换、日期格式化、时间字符串解析、倒计时、获取当前时间、计算日期间隔等方法。这些方法有助于在Java应用中进行日期和时间的处理。
摘要由CSDN通过智能技术生成
/**
 * Date类型的时间转 ZonedDateTime
 *
 * @param utilDate Date
 * @return ZonedDateTime
 */
public static ZonedDateTime toZonedDateTime(Date date) {
    if (date == null) {
        return null;
    }
    final ZoneId zoneId = ZoneId.systemDefault();
    return ZonedDateTime.ofInstant(utilDate.toInstant(), zoneId);
}

/**
 * 将 Date 时间类转为指定格式的字符串
 *
 * @param date 时间类
 * @param type 1:年-月-日 时:分:秒; 2:年-月-日 时:分; 3:年-月-日; 4:年月日
 * @return String
 */
public static String dateToStr(Date date, int type) {
    SimpleDateFormat format;
    if (type == 0) {
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    } else if (type == 1) {
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    } else if (type == 3) {
        format = new SimpleDateFormat("yyyy-MM-dd");
    } else {
        format = new SimpleDateFormat("yyyyMMdd");
    }
    return format.format(date);
}

/**
 * 将时间字符串转为 Date
 *
 * @param date 时间字符串
 * @param type 1:年-月-日 时:分:秒; 2:年-月-日 时:分 ;3:年-月-日
 * @return Date
 */
public static Date strToDate(String date, int type) {
    try {
        if (Objects.isNull(date) || "".equals(date)) {
            return new Date();
        }
        SimpleDateFormat format;
        if (type == 0) {
            format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else if (type == 1) {
            format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        } else {
            format = new SimpleDateFormat("yyyy-MM-dd");
        }
        return format.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
        return new Date();
    }
}

//倒计时
public void countDown(String time) {
    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        Date parse = format.parse(time);
        long millisecond = parse.getTime() - date.getTime();
        long second = millisecond / 1000;
        while (second > 0) {
            second--;
            Thread.sleep(1000);
            long hh = second / 60 / 60 % 60;
            long mm = second / 60 % 60;
            long ss = second % 60;
            System.out.println("距离下班还剩" + hh + "小时" + mm + "分钟" + ss + "秒");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * 当天几点钟
 *
 * @return
 */
public static Date getTodayHour(int hour) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hour);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    Date date = c.getTime();
    return date;
}

/**
 * 根据距离1900年1月1日的天数,获取日期
 *
 * @param days 天数
 * @return String
 */
public static String getDate(Integer days) {
    Calendar calendar = new GregorianCalendar(1900, 0, -1);
    Date date = calendar.getTime();
    Date d = DateUtils.addDays(date, days);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    return simpleDateFormat.format(d);
}

/**
 * 计算两个日期之间的相差天数
 *
 * @param startDate 开始时间 日期格式:yyyy-MM-dd
 * @param endDate   结束时间 日期格式:yyyy-MM-dd
 * @return 天数
 */
public static long getDays(String startDate, String endDate) {
    try {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startParse = simpleDateFormat.parse(startDate);
        Date endParse = simpleDateFormat.parse(endDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startParse);
        long startMillis = calendar.getTimeInMillis();
        calendar.setTime(endParse);
        long endMillis = calendar.getTimeInMillis();
        Long time = endMillis - startMillis;
        long days = time / 86400000;
        return days;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}

/**
 * 将 ZonedDateTime 转为 yyyy-MM-dd 的日期格式
 *
 * @param zonedDateTime ZonedDateTime
 * @return yyyy-MM-dd
 */
public static String getDate(ZonedDateTime zonedDateTime) {
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    return dateTimeFormatter.format(zonedDateTime);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值