欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Date转LocalDate

程序员文章站 2022-06-09 16:05:43
...
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
 
public class DateUtils{
 
    /**
     * LocalDate转Date
     * @param localDate
     * @return
     */
    public static Date localDate2Date(LocalDate localDate) {
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }
 
    /**
     * Date转LocalDate
     * @param date
     */
    public static LocalDate date2LocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    
}
	/**
	 * 时间差
	 */
   public static Long subtraction (Date startDate, Date endDate ) {
   ocalDateTime endDateLocal = endDate .toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
   LocalDateTime startDateLocal  = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
     long daysDiff = ChronoUnit.MINUTES.between(startDateLocal ,endDateLocal );
    return daysDiff;
}
}

相关标签: java