Java8 日期工具类

Java8新的日期Api,java.time包下的所有类都是不可变类型而且线程安全。
Date互转LocalDate
以下很多都是demo


import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * Describe: JAVA8 日 期 工 具 类
 * Author: zts
 * CreateTime: 2021/10/20
 *
 * 1、Period 计算两个日期之差
 * 2、YearMonth类 可以得到当月天数,可以用来计算信用卡到期日
 * */
public class DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前Date型日期
     *
     * @return String 当前日期
     */
    public static String getNowDay(){
        LocalDateTime now = LocalDateTime.now();
        return now.format(DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS));
    }

    /**
     * 处理日期
     *
     * @return LocalDate 当前日期
     */
    public static LocalDate handleSpeciDate(){
        LocalDate dateBirth = LocalDate.of(2020, 02, 12);
        int year = dateBirth.getYear();
        String month = dateBirth.getMonthValue() < 10 ? "0" + dateBirth.getMonthValue() : "" + dateBirth.getMonthValue();
        int dayOfMonth = dateBirth.getDayOfMonth();
        System.out.println(month);
        return dateBirth;
    }

    /**
     * 加减日期
     *
     * @return LocalDate 处理日期
     */
    public static LocalDate plusDayOrMinusDay(){
        LocalDate now = LocalDate.now();
        LocalDate localDate1 = now.plusDays(7);
        System.out.println("{localDate1},{}"+ localDate1 );
        LocalDate localDate2 = now.plus(1, ChronoUnit.MONTHS);
        System.out.println("{localDate1},{}"+ localDate2 );
        LocalDate localDate3 = now.minus(1, ChronoUnit.MONTHS);
        System.out.println("{localDate1},{}"+ localDate3 );
        LocalDate localDate4 = now.plusYears(1);
        System.out.println("{localDate1},{}"+ localDate4 );
        return localDate1;

    }

    /**
     * 比较日期
     *
     * @return boolean
     */
    public static boolean compareLocalDate(){
        LocalDate now = LocalDate.now();
        LocalDate dayDate = LocalDate.of(2020, 10, 20);
        if (now.equals(dayDate)){
            System.out.println("equals :" + now.equals(dayDate));
            System.out.println(now + " $$" + dayDate);
        }
        if (dayDate.isAfter(dayDate)){
            System.out.println("isAfter " + dayDate.isAfter(dayDate));
        }
        if (dayDate.isEqual(dayDate)){
            System.out.println("isEqual " + dayDate.isEqual(dayDate));
        }
        return now.equals(dayDate);
    }

    public static YearMonth getYearMonth() {
        YearMonth currentYearMonth = YearMonth.now();
        System.out.printf("Days in yearMonth %s,month day %d%n", currentYearMonth, currentYearMonth.lengthOfMonth());

        YearMonth creditCardExpiry = YearMonth.of(2028, Month.FEBRUARY);
        System.out.printf("Your credit card expires on %s %n", creditCardExpiry);
        return creditCardExpiry;
    }
    /**
     * Date转LocalDate
     * @param date
     */
    public static LocalDate date2LocalDate(Date date) {
        if(null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * LocalDate转Date
     * @param localDate
     * @return
     */
    public static Date localDate2Date(LocalDate localDate) {
        if(null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    public static void main(String[] args) {

        dateToStr();

    }

    /**
     * Str 转  LocalDate
     * @return LocalDate
     */
    public static LocalDate strToDate(){
        LocalDate parse = LocalDate.parse("2020-02-31", DateTimeFormatter.ofPattern(YYYY_MM_DD));
        return parse;
    }

    /**
     * LocalDate 转   Str
     * @return LocalDate
     */
    public static String dateToStr(){
        LocalDate parse = LocalDate.parse("2020-02-31", DateTimeFormatter.ofPattern(YYYY_MM_DD));
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS));
        System.out.println(format);
        return format;
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值