精华内容
下载资源
问答
  • LocalDateTime转Date

    万次阅读 2021-01-19 09:58:25
    LocalDateTime转Date LocalDateTime localDateTime = LocalDateTime.now(); Date date = Date.from( localDateTime.atZone( ZoneId....Date转LocalDateTime Date todayDate = new Date(); LocalDateTime ldt = toda

    LocalDateTime转Date

    LocalDateTime localDateTime = LocalDateTime.now();
    
    Date date = Date.from( localDateTime.atZone( ZoneId.systemDefault()).toInstant());
    
    System.out.println(date);
    
    

    Date转LocalDateTime

    Date todayDate = new Date();
    
    LocalDateTime ldt = todayDate.toInstant()
            .atZone( ZoneId.systemDefault() )
            .toLocalDateTime();
    
    System.out.println(ldt);
    
    

    再省事点就写一个DateUtil类

    import java.time.Instant;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.util.Date;
     
    public class DateUtils {
     
        public static Date asDate(LocalDate localDate) {
            return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        }
     
        public static Date asDate(LocalDateTime localDateTime) {
            return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        }
     
        public static LocalDate asLocalDate(Date date) {
            return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
        }
     
        public static LocalDateTime asLocalDateTime(Date date) {
            return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
        }
    }
    
    展开全文
  • localDateTime 转date

    2020-11-19 12:51:56
    LocalDateTime localDateTime = LocalDateTime.ofInstant(mcRegister.getCreateTime().toInstant(), zoneId); LocalDateTime localDate1 = LocalDateTime.now(); long betweenDays = ChronoUnit.DAYS.between...
    ZoneId zoneId = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(mcRegister.getCreateTime().toInstant(), zoneId);
    LocalDateTime localDate1 = LocalDateTime.now();
    long betweenDays = ChronoUnit.DAYS.between(localDateTime.toLocalDate(), localDate1.toLocalDate());
    int daysTotal =Integer.parseInt(pregnantWeek)*7+Integer.parseInt(pregnantDay)+(int)betweenDays;
    a = daysTotal / 7;//孕周
    b = daysTotal % 7;//孕天
    展开全文
  • java8 LocalDateTime Date

    2021-07-22 11:21:05
    Date date = Date.from(LocalDateTime.now() .atZone(ZoneId.systemDefault()) .toInstant());
    Date date = Date.from(LocalDateTime.now()
        .atZone(ZoneId.systemDefault())
        .toInstant());

    展开全文
  • * Date转LocalDateTime * @param date * @return */ public static LocalDateTime date2LocalDate ( Date date ) { return date . toInstant ( ) . atZone ( ZoneId . systemDefault ( ) ) . ...

    Java8 时间使用

    获取当前日期

    //2019-06-19
    LocalDate nowLocalDate = LocalDate.now();
    
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    
    //2019-06-19
    String nowString = nowLocalDate.format(formatter);
    

    获取当前时间

    //10:03:22.237
    LocalTime localTime = LocalTime.now();
    
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
    
    //10:03:22
    String stringTime = localTime.format(formatter)
    

    获取当前日期加时间

    //2019-06-19T10:06:05.463
    LocalDateTime localDateTime = LocalDateTime.now();
    
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    //2019-06-19 10:06:05
    String dateTime = LocalDateTime.now().format(formatter);
    
    //2019-06-19T10:37:07.407
    String dateTime = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    

    自定义时间(LocalDate)

    //10:09:20
    LocalTime localTime = LocalTime.parse("10:09:20");
    
    //2019-06-19
    LocalDate localDate = LocalDate.parse("2019-06-19");
    
    //2019-06-19T10:09:30
    LocalDateTime localDateTime = LocalDateTime.parse("2019-06-19T10:09:30");
    

    计算两个日期的时间差

    //18066 获取从1970年以来的天数:toEpochDay
    long days = LocalDate.now().toEpochDay();
    
    //565 获取两个时间的天数差
    long day = LocalDate.now().toEpochDay() - LocalDate.parse("2017-12-01").toEpochDay();
    

    判断日期的先后,isAfter/isBefore

    LocalDate date1 = LocalDate.parse("2019-06-19");
    
    LocalDate date2 = LocalDate.parse("2019-06-20");
    //date1是否在date2时间之前
    if (date1.isBefore(date2)) {
        System.out.println("date1在date2之前");
    }
    //date2是否在date1时间之后
    if (date2.isAfter(date1)) {
        System.out.println("date2在date1之前");
    }
    

    Date和LocalDateTime转换

    /**
     * Date转LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime date2LocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
    
    /**
     * Date转LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDate(Date date) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
    
    }
    
    /**
     * LocalDateTime转Date
     * @param localDateTime
     * @return
     */
    public static Date localDate2Date(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * LocalDateTime转Date
     * @param localDateTime
     * @return
     */
    public static Date localDateToDate(LocalDateTime localDateTime) {
        return new Date(localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
    }
    

    LocalDateTime与时间戳的使用

    //获取时间戳秒数
    Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
    //获取时间戳毫秒数
    Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    
    /**
     * 时间戳转LocalDateTime
     * @param timestamp
     * @return
     */
    public LocalDateTime timestamToLocalDateTime(long timestamp){
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
    }
    
    /**
     * LocalDateTime转时间戳
     * @param localDateTime
     * @return
     */
    public long localDateTimeToTimestamp(LocalDateTime localDateTime){
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }
    

    其它API

    
    //2019-06-19T10:25:30.846
    LocalDateTime localDateTime = LocalDateTime.now();
    
    //19 今天是当前月份的第几天
    int dayOfMonth = localDateTime.getDayOfMonth();
    
    //170 今天是今年的第几天
    int dayOfYear = localDateTime.getDayOfYear();
    
    //WEDNESDAY 今天是周几
    DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
    
    //3 今天是本周第几天
    int dayofWeek = dayOfWeek.getValue();
    
    //2019-06-18T10:25:30.846 获取当前时间前1天时间
    LocalDateTime beforeDateTime = LocalDateTime.now().minusDays(1);
    
    //2019-06-20T10:25:30.846 获取当前时间1天后的时间
    LocalDateTime nextDateTime = LocalDateTime.now().plusDays(1);
    
    //2019-06-19T08:27:30.400 获取当前时间的2小时之前的时间
    LocalDateTime beforeHourTime = LocalDateTime.now().minusHours(2);
    
    //2019-06-19T10:49:20.996 获取当前时间20分钟后的时间
    LocalDateTime afterMinuteTime = LocalDateTime.now().plusMinutes(20);
    
    //false 判断今年是否是闰年
    boolean isLeapYear =  LocalDate.now().isLeapYear();
    
    展开全文
  • Java LocalDateTime转Date

    千次阅读 2020-06-16 17:42:18
    Date startDate = Date.from(startLocalDateTime.toInstant(ZoneOffset.of("+8"))); Date endDate = Date.from(endLocalDateTime.toInstant(ZoneOffset.of("+8")));
  • String timeStr = "2019-08-26 18:03:33"; DateTimeFormatter timeDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); ... LocalDateTime localDateTime = LocalDateTime.parse(time...
  • 关于时间戳和LocalDateTime和Date互转时间戳与LocalDateTime互转方式...:LocalDateTime与Date互转Date转LocalDateTime方式一 :方式二 :LocalDateTime 转 Date方式一 :方式二 :LocalDate与Date互转LocalDate 转 DateDat
  • LocalDateTimeDate 本篇文章你将了解到: 为什么需要LocalDate、LocalTime、LocalDateTime【java8新提供的类】 java8新的时间API的使用方式,包括创建、格式化、解析、计算、修改 为什么需要LocalDate...
  • LocalDateTime转换为Date

    2019-08-23 09:35:59
    public Date fromLocalDateTime(LocalDateTime ldt){ ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime = LocalDateTime.now(); ZonedDateTime zdt = localDateTime.atZone(zoneId); Date date...
  • 本文目前提供:LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互DateLocalDateTime 文中都使用的时区都是东8区,也就是北京时间。这是为了防止服务器设置时区错误时导致时间不对,如果您是...
  • JAVA 8 LocalDateTimeDate转换

    千次阅读 2020-05-22 17:49:09
    不可不说在Java里面Date的转换是十分麻烦,并且还会存在线程不安全的问题,后期争取慢慢试着使用LocalDateTime~,即使刚开始有的只是不习惯。... //获取当前时间并LocalDateTime Date date = new Date();
  • Java8 LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互DateLocalDateTime LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互DateLocalDateTime 文中都使用的时区都...
  • LocalDateTime Date示例2. Date LocalDateTime示例参考文献 本页将提供从Java java.time.LocalDateTime到java.util.Date以及从java.util.Date到java.time.LocalDateTime的转换实例。 Java 8中引入的...
  • Java8-LocalDate、LocalDateTime、Date、时间戳的转化1.LocalDate转Date2.LocalDateTime转Date3.Date转LocalDate,LocalDateTime4.LocalDate转时间戳5.LocalDateTime转时间戳6.时间戳转为LocalDate(LocalDateTime) 1...
  • LocalDateTime & Date相互转换

    千次阅读 2018-10-18 10:05:50
    public Date fromLocalDateTime(LocalDateTime ldt){ ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime = LocalDateTime.now(); ZonedDateTime zdt = localDateTime.atZone(zoneId); ...
  • LocalDateTime a = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); System.out.println(a); Date b = Date.from(a.atZone(ZoneId.systemDefault()).toInstant()); System.out.println....
  • Java8 LocalDateTimeDate相互转换 import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class DateUtils { //...
  • package ... import org.apache.commons.lang3.StringUtils; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Local...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 18,760
精华内容 7,504
关键字:

localdatetime转date