【hutool】使用问题

【hutool】使用问题,第1张

如何将Date转化为LocalDateTime
package cn.hutool.core.date;

public class LocalDateTimeUtil {
	/**
	 * {@link Date}转{@link LocalDateTime},使用默认时区
	 *
	 * @param date Date对象
	 * @return {@link LocalDateTime}
	 */
	public static LocalDateTime of(Date date) {
		if (null == date) {
			return null;
		}

		if (date instanceof DateTime) {
			return of(date.toInstant(), ((DateTime) date).getZoneId());
		}
		return of(date.toInstant());
	}
}
如何将LocalDateTime、LocalDate转化为Date
  • 方案1

对纯原生的转化方式Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant())进行简单封装。即先使用TemporalAccessorUtil#toInstant(TemporalAccessor)LocalDateTime转化为Instant(底层就是执行localDateTime.atZone(ZoneId.systemDefault()).toInstant()

package cn.hutool.core.date;

public class TemporalAccessorUtil extends TemporalUtil{
	/**
	 * {@link TemporalAccessor}转换为 {@link Instant}对象
	 *
	 * @param temporalAccessor Date对象
	 * @return {@link Instant}对象
	 * @since 5.3.10
	 */
	public static Instant toInstant(TemporalAccessor temporalAccessor) {
		if (null == temporalAccessor) {
			return null;
		}

		Instant result;
		if (temporalAccessor instanceof Instant) {
			result = (Instant) temporalAccessor;
		} else if (temporalAccessor instanceof LocalDateTime) {
			result = ((LocalDateTime) temporalAccessor).atZone(ZoneId.systemDefault()).toInstant();
		} else if (temporalAccessor instanceof ZonedDateTime) {
			result = ((ZonedDateTime) temporalAccessor).toInstant();
		} else if (temporalAccessor instanceof OffsetDateTime) {
			result = ((OffsetDateTime) temporalAccessor).toInstant();
		} else if (temporalAccessor instanceof LocalDate) {
			result = ((LocalDate) temporalAccessor).atStartOfDay(ZoneId.systemDefault()).toInstant();
		} else if (temporalAccessor instanceof LocalTime) {
			// 指定本地时间转换 为Instant,取当天日期
			result = ((LocalTime) temporalAccessor).atDate(LocalDate.now()).atZone(ZoneId.systemDefault()).toInstant();
		} else if (temporalAccessor instanceof OffsetTime) {
			// 指定本地时间转换 为Instant,取当天日期
			result = ((OffsetTime) temporalAccessor).atDate(LocalDate.now()).toInstant();
		} else {
			// issue#1891@Github
			// Instant.from不能完成日期转换
			//result = Instant.from(temporalAccessor);
			result = toInstant(LocalDateTimeUtil.of(temporalAccessor));
		}

		return result;
	}
}

再使用Date#from(Instant)

package java.util;

public class Date implements java.io.Serializable, Cloneable, Comparable<Date> {
	/**
     * Obtains an instance of {@code Date} from an {@code Instant} object.
     * 

* {@code Instant} uses a precision of nanoseconds, whereas {@code Date} * uses a precision of milliseconds. The conversion will truncate any * excess precision information as though the amount in nanoseconds was * subject to integer division by one million. *

* {@code Instant} can store points on the time-line further in the future * and further in the past than {@code Date}. In this scenario, this method * will throw an exception. * * @param instant the instant to convert * @return a {@code Date} representing the same point on the time-line as * the provided instant * @exception NullPointerException if {@code instant} is null. * @exception IllegalArgumentException if the instant is too large to * represent as a {@code Date} * @since 1.8 */ public static Date from(Instant instant) { try { return new Date(instant.toEpochMilli()); } catch (ArithmeticException ex) { throw new IllegalArgumentException(ex); } } }

  • 方案2

DateTime类继承于java.util.Date类,为Date类扩展了众多简便方法,这些方法多是DateUtil静态方法的对象表现形式,使用DateTime对象可以完全替代开发中Date对象的使用。

package cn.hutool.core.date;

public class DateUtil extends CalendarUtil {
	/**
	 * {@link TemporalAccessor}类型时间转为{@link DateTime}
* 始终根据已有{@link TemporalAccessor} 产生新的{@link DateTime}对象 * * @param temporalAccessor {@link TemporalAccessor},常用子类: {@link LocalDateTime}、 LocalDate * @return 时间对象 * @since 5.0.0 */
public static DateTime date(TemporalAccessor temporalAccessor) { return new DateTime(temporalAccessor); } }
  • 示例
public class Test {
	public static void main(String[] args) {
        final LocalDateTime localDateTime = LocalDateTime.of(2022, 5, 9, 11, 36);
        final Date date1 = Date.from(TemporalAccessorUtil.toInstant(localDateTime));
        final DateTime date2 = DateUtil.date(localDateTime);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/883575.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-14
下一篇 2022-05-14

发表评论

登录后才能评论

评论列表(0条)

保存