ladma8 时间转化及处理

获取当天的 2010-01-01 00:00:00 和 2010-01-01 23:59:59

		ZoneId zone = ZoneId.systemDefault();
        ZonedDateTime now = LocalDate.now().atStartOfDay(zone);
        startDate = Date.from(now.toInstant());
        endDate = Date.from(now.plusDays(1).minusSeconds(1).toInstant());

获取date类型时间差 天数

	Instant startInstant = startDate.toInstant();
    Instant endInstant = endDate.toInstant();
    LocalDate startLocalDate = startInstant.atZone(ZoneId.systemDefault()).toLocalDate();
    LocalDate endLocalDate = endInstant.atZone(ZoneId.systemDefault()).toLocalDate();
    long dayDiff = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);

LocalDateTime & Date相互转换

public Date fromLocalDateTime(LocalDateTime ldt){
    ZonedDateTime zdt = ldt.atZone(ZoneId.systemDefault());
    Date date = Date.from(zdt.toInstant());
    System.out.println("LocalDateTime = " + localDateTime);
    System.out.println("Date = " + date);
    return date ;
}	

public LocalDateTime fromDate(Date date){
    Instant instant = date.toInstant();
    LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
    System.out.println("Date = " + date);
    System.out.println("LocalDateTime = " + localDateTime);
    return localDateTime ;
}

LocalDateTime与String日期互相转换

public static void main(String[] args) {
         DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
         LocalDateTime time = LocalDateTime.now();
         String localTime = df.format(time);
         LocalDateTime ldt = LocalDateTime.parse("2018-01-12 17:07:05",df);
         System.out.println("LocalDateTime转成String类型的时间:"+localTime);
         System.out.println("String类型的时间转成LocalDateTime:"+ldt);
}

你可能感兴趣的:(java)