相关文章推荐
卖萌的黄豆  ·  山煤国际董事长协助调查丁书苗案 ...·  1 年前    · 
痴情的眼镜  ·  达晨创投美女总裁邵红霞身着舒朗优雅亮相峰会_论坛·  2 年前    · 
酷酷的馒头  ·  《致命录像带94》全集高清完整版免费在线观看 ...·  2 年前    · 
坐怀不乱的鸡蛋  ·  2023年4月份宝马X5销量5907台, ...·  2 年前    · 
迷茫的鸵鸟  ·  网络小说 - 网络小说网址大全 - 资源猫·  2 年前    · 
小百科  ›  Convert OffsetDateTime to LocalDateTime - Spring Framework Guru
localtime date convert
伤情的豆腐
3 年前
  • Home
  • Start Here
  • Courses
    • Intro to Spring (Free)
    • Spring Framework 5
    • Spring Boot Microservices
    • Testing Spring Boot
    • Spring Security Core
    • Spring Framework 4
    • Apache Maven
    • Docker for Java Developers
    • SQL Beginner to Guru
  • Guru Gear
  • My Courses
  • Blog
  • Write for SFG
  • About
  • Contact

Spring Framework Guru

Become a Spring Framework Guru
  • Home
  • Start Here
  • Courses
    • Intro to Spring (Free)
    • Spring Framework 5
    • Spring Boot Microservices
    • Testing Spring Boot
    • Spring Security Core
    • Spring Framework 4
    • Apache Maven
    • Docker for Java Developers
    • SQL Beginner to Guru
  • Guru Gear
  • My Courses
  • Blog
  • Write for SFG
  • About
  • Contact

Convert OffsetDateTime to LocalDateTime

Home › Blog › Convert OffsetDateTime to LocalDateTime
Spring Framework Guru

Convert OffsetDateTime to LocalDateTime

By SFG Contributor July 22, 2020 Java
1 Comment

Java 8 introduced a new date and time API defined inside java.time package . The key date and time classes defined in this package are LocalDateTime , LocalDateTime , and OffsetDateTime .

OffsetDateTime represents a date-time with an offset. This class stores all date and time fields, to a precision of nanoseconds, as well as the offset from UTC/Greenwich. For example, the value 2nd December 2018 at 15:35.40.123456789 +03:00 can be stored in an OffsetDateTime .

On the other hand, LocalDateTime represents a date and time without time zone information. An example is 2020-07-12T17:23:10.205580 .

In addition to LocalDateTime , you can also use the LocalDate and LocalTime classes to work with date and time without time-zone separately.

An example of LocalDate is 2018-10-05 . An example of LocalTime is 15:19:47.459 .

In my previous post, I explained how to convert OffsetDateTime to ZonedDateTime.

In this post, I’ll discuss how to convert OffsetDateTime to LocalDateTime , LocalDate , and LocalTime .

Converting OffsetDateTime To LocalDateTime

The code for converting OffsetDateTime to LocalDateTime is this.

public LocalDateTime convertToLocalDateTime() {
   OffsetDateTime offsetDateTime = OffsetDateTime.now();
   System.out.println(offsetDateTime);
   LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
   System.out.println("localDateTime = " + localDateTime);
   return  localDateTime;

In the preceding code, OffsetDateTime.now() gets the current date-time in the default time-zone with an offset. Then offsetDateTime.toLocalDateTime() converts the current date-time LocalDateTime .

Let’s call this method from our main class.

DateConverter dateConvertor = new DateConverterImpl();
dateConvertor.convertToLocalDateTime();

On running the application, the output is this.

2020-07-12T18:51:36.240918

Converting OffsetDateTime To LocalDate

At times, you might want only the local date instead of a full date and time with offset. For that, you need to convert the OffsetDateTime to LocalDate.

The code is this.

public LocalDate convertToLocalDate() {
   OffsetDateTime offsetDateTime = OffsetDateTime.now();
   System.out.println(offsetDateTime);
   LocalDate localDate=offsetDateTime.toLocalDate();
   System.out.println(localDate);
   return  localDate;

The output on calling the preceding method is this.

2020-07-12

Converting OffsetDateTime To LocalTime

The OffsetDateTime class also allows conversion to local time.

The code for converting OffsetDateTime to LocalTime is this.

public LocalTime convertToLocalTime() {
   OffsetDateTime offsetDateTime = OffsetDateTime.now();
   System.out.println(offsetDateTime);
 
推荐文章
卖萌的黄豆  ·  山煤国际董事长协助调查丁书苗案 或与利益输送有关_三湘风纪网_湖南省纪委监委官方网站
1 年前
痴情的眼镜  ·  达晨创投美女总裁邵红霞身着舒朗优雅亮相峰会_论坛
2 年前
酷酷的馒头  ·  《致命录像带94》全集高清完整版免费在线观看-2021恐怖片-VS影院
2 年前
坐怀不乱的鸡蛋  ·  2023年4月份宝马X5销量5907台, 同比增长1492.18%_销量数据 - 车主之家
2 年前
迷茫的鸵鸟  ·  网络小说 - 网络小说网址大全 - 资源猫
2 年前
Link管理   ·   Sov5搜索   ·   小百科
小百科 - 百科知识指南