龙空技术网

DATE #4、Java操作日期时间-⑥.各种日期类型之间转换

狮子哥哥520 159

前言:

如今姐妹们对“java的日期格式转换”大概比较关心,兄弟们都需要分析一些“java的日期格式转换”的相关内容。那么小编也在网络上收集了一些对于“java的日期格式转换””的相关知识,希望兄弟们能喜欢,小伙伴们一起来学习一下吧!

1.java.util.Date转java.sql.Date

 //1.初始化日期 Date date = new Date(); log.debug("`java.util.Date`: {}",date); //2.转sql.date java.sql.Date sqlDate = new java.sql.Date(date.getTime()); log.debug("`java.util.Date`转`java.sql.Date`: {}",sqlDate); //3.转java.sql.Timestamp java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime()); log.debug("`java.util.Date`转`java.sql.Timestamp`: {}",timestamp); // - `java.util.Date`: Sun Jun 20 22:11:09 CST 2021 // - `java.util.Date`转`java.sql.Date`: 2021-06-20 // - `java.util.Date`转`java.sql.Timestamp`: 2021-06-20 22:11:09.379
2.java.util.Date转java.time.LocalDate
 //1.初始化日期 Date date = new Date(); log.debug("`java.util.Date`: {}",date); //2.`java.util.Date`转`java.time.LocalDate`: LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); log.debug("`java.util.Date`转`java.time.LocalDate`: {}",localDate); //2.`java.util.Date`转`java.time.LocalDate`: LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); log.debug("`java.util.Date`转`java.time.LocalDateTime`: {}",localDateTime); // - `java.util.Date`: Sun Jun 20 22:07:39 CST 2021 // - `java.util.Date`转`java.time.LocalDate`: 2021-06-20 // - `java.util.Date`转`java.time.LocalDateTime`: 2021-06-20T22:07:39.591
3.java.sql.Date转java.time.LocalDate
 //1.初始化日期 java.sql.Date sqlDate = java.sql.Date.valueOf("2017-06-16"); log.debug("`java.sql.Date`: {}",sqlDate); java.time.LocalDate localDate = sqlDate.toLocalDate(); log.debug("`java.sql.Date`转`java.time.LocalDate`: {}",localDate); java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2017-06-16 12:24:09"); log.debug("`java.sql.Timestamp`: {}",timestamp); java.time.LocalDateTime localDateTime = timestamp.toLocalDateTime(); log.debug("`java.sql.Timestamp`转`java.time.LocalDateTime`: {}",localDateTime); // - `java.sql.Date`: 2017-06-16 // - `java.sql.Date`转`java.time.LocalDate`: 2017-06-16 // - `java.sql.Timestamp`: 2017-06-16 12:24:09.0 // - `java.sql.Timestamp`转`java.time.LocalDateTime`: 2017-06-16T12:24:09
4.java.time.LocalDate转java.sql.Date
 //1.初始化日期 LocalDate localDate = LocalDate.now(); log.debug("`java.time.LocalDate`: {}",localDate); java.sql.Date sqlDate = java.sql.Date.valueOf(localDate); log.debug("`java.time.LocalDate`转`java.sql.Date`: {}",sqlDate); LocalDateTime localDateTime = LocalDateTime.now(); log.debug("`java.time.LocalDateTime`: {}",localDateTime); java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(localDateTime); log.debug("`java.time.LocalDateTime`转`java.sql.TimeStamp`: {}",timestamp); // - `java.time.LocalDate`: 2021-06-20 // - `java.time.LocalDate`转`java.sql.Date`: 2021-06-20 // - `java.time.LocalDateTime`: 2021-06-20T22:31:03.124 // - `java.time.LocalDateTime`转`java.sql.TimeStamp`: 2021-06-20 22:31:03.124
5.Calendar转LocalDate
 Calendar calendar = Calendar.getInstance(); log.debug("java.util.Calendar: {}",calendar); ZoneId zoneId = calendar.getTimeZone().toZoneId(); ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(calendar.toInstant(), zoneId); log.debug("java.util.Date: {}",zonedDateTime); LocalDate localDate = zonedDateTime.toLocalDate();

标签: #java的日期格式转换 #java中转换日期格式 #java时间转格式 #时间转换 java