System.out.println("北京地区此时间对应的纽约的时间:" + ZonedDateTime.ofInstant(localDateTime.toInstant(ZoneOffset.ofHours(8)), ZoneOffset.ofHours(-4))); System.out.println("北京地区此时间对应的纽约的时间:" + ZonedDateTime.ofInstant(localDateTime, ZoneOffset.ofHours(8), ZoneOffset.ofHours(-4)))...
LocalDateTime now=LocalDateTime.now();//format 日期类型转换为日期字符串(使用系统默认格式)DateTimeFormatter isoLocalDateTime =DateTimeFormatter.ISO_LOCAL_DATE_TIME; String format=now.format(isoLocalDateTime); System.out.println(format);//format 日期类型转换为日期字符串(使用指定格式)DateTimeFormatter formatter ...
public static LocalDateTime toLocalDateTime(Date date) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()); } public static ZonedDateTime toZonedDateTime(Calendar calendar) { ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(calendar.getTimeIn...
ZonedDateTime zonedDate = ZonedDateTime.of(localDateTime, ZoneId.of("Asia/Shanghai")); System.out.println(zonedDate.getOffset() + "--> " + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSSS") .format(zonedDate)); ZonedDateTime hkZonedDate = ZonedDateTime.of(localDateTime, ZoneId...
你真正想要的是 ZonedDateTime 。如果您想从字符串表示中删除时区,您可以将其转换为 LocalDateTime。 你要找的是: ZonedDateTime swissZonedDateTime = withZoneSameInstant(ZoneId.of("Europe/Zurich")); LocalDateTime - 这基本上是 Date 和Time 的美化字符串表示;它与时区无关,这意味着它不代表时间轴上的...
LocalDateTime localDateTime = LocalDateTime.now(); Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); 为了将LocalDateTime转换为Date,我们需要首先将其与默认时区结合以创建ZonedDateTime,然后将其转换为Instant,最后使用Date.from()方法创建Date对象。 3. 将Date转换为LocalDate impo...
ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zoneDateTime = localDateTime.atZone(zoneId); //再转成 java.util.Date() Date ndate = Date.from(zoneDateTime.toInstant()); System.out.println("change to date:"); System.out.println(ndate); ...
Java ZonedDateTime转换成 LocalDateTime方法代码及之间区别 - cjavapy于20211002发布在抖音,已经收获了1250个喜欢,来抖音,记录美好生活!
方案一:先转成ZonedDateTime,通过ZonedDateTime.toLocalDateTime()得到LocalDateTime。具体实现参照UTC字符串转ZonedDateTime。 方案二:先转成Instant,通过LocalDateTime.ofInstant(Instant instant, ZoneId zone)得到LocalDateTime。 publicstaticLocalDateTimeconvertLocalDateTime(Stringutc){if(StringUtils.isBlank(utc)){returnnu...
LocalDateTime ZonedDateTime 1、LocalDate LocalDate只是一个日期,没有时间。 这意味着我们只能获得当前日期,但没有一天的具体时间。 LocalDate date = LocalDate.now(); // 获取当前日期 1. 使用DateTimeFormatter格式化,得到结果只显示年月日,例如: public static void main(String[] args) { LocalDate date =...