java8中LocalDateTime工具类(持续更新)

因为Date的过时,想要使用最新的LocalDateTime,闲暇之余整理了LocalDateTime工具类,并且会持续更新。
https://www.linuxidc.com/Linux/2019-03/157267.htm

import io.swagger.annotations.Api;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Date;

/**
 * @author xinyi
 * @version 2019/12/8
 */
@Api(tags = "LocalDateTime工具类")
public class LocalDateTimeUtil {

    //构建LocalDateTime对象
    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDateTime localDateTime2 = LocalDateTime.of(2019,12,8,16,12,30
    );

    //比较当前时间和指定时间
    boolean flag = LocalDateTime.now().isBefore(localDateTime);
    boolean flag2 = LocalDateTime.now().isEqual(localDateTime);
    boolean flag3 = LocalDateTime.now().isAfter(localDateTime);

    //获得当前时间的毫秒数
    Long milli = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    //秒数
    Long second = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();


    //将Date转换为LocalDateTime
    public static LocalDateTime date2LocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    //将LocalDateTime转换为Date
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    //将LocalDateTime转换为指定格式的字符串
    public static String localDateTime2String(LocalDateTime localDateTime,String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    //
    //日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
    public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
        return time.plus(number, field);
    }
    //日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
    public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){
        return time.minus(number,field);
    }

    /**
     * 获取两个日期的差  field参数为ChronoUnit.*
     * @param startTime
     * @param endTime
     * @param field  单位(年月日时分秒)
     * @return long
     */
    public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
        Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
        if (field == ChronoUnit.YEARS) return period.getYears();
        if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths();
        return field.between(startTime, endTime);
    }

    //获取一天的开始时间,2017,7,22 00:00
    public static LocalDateTime getDayStart(LocalDateTime time) {
        return time.withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
    }

    //获取一天的结束时间,2017,7,22 23:59:59.999999999
    public static LocalDateTime getDayEnd(LocalDateTime time) {
        return time.withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(999999999);
    }

}

2019年12月30日更新

#LocalDateTime
1.创建对象
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime = LocalDateTime.of(2019,12,30,19,53,23);
LocalDate
LocalTime
2.和字符串的转换(Date抛出parseException,localDateTime抛出dateTimeParseException)
time-string
String str = simpleDateFormat.format(date);
string str = LocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

string-time
Date date = simpleDateFormat.parse(str);
LocalDateTime date = LocalDateTime.parse(str,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
3.和时间戳的转换
time-long
long time = date.getTime();
long time = localDateTime.atZone(ZoneId.systemDeafult()).toInstant().toEpochMilli();
long time = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();

long-time
Date date = new Date(long);
LocalDateTime localDateTime = LocalDateTime.ofInstant(long,ZoneId.systemDeafult());
4.和Date互转
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDeafult());
Date date = Date.from(localDateTime.atZone(ZoneId.systemDeafult()).toInstant());
5.simpleDateFormat是线程非安全的,而DateTimeFormatter是线程安全的。
Date线程不安全,getset的时候容易出问题。
LocalDateTimes是线程安全的,类属性被final修饰。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值