Showing posts with label Joda-Time. Show all posts
Showing posts with label Joda-Time. Show all posts

Tuesday, August 16, 2016

How to convert date into datetime in java

Java 8 has adopted Jodatime in Jave SE8. Joda time is now introduced in java.time package.

Joda time was de facto standard date and time library prior to Java SE8.

 Following are the ways to convert the java util dates with java.time.* and vice-versa.

The idle way (for all these conversions) is to convert to Instant. This can be converted to LocalDateTime by telling the system which timezone to use. This needs to be the system default locale, otherwise the time will change.

Convert java.util.Date to java.time.LocalDateTime


Date ts = new Date();
Instant instant = Instant.ofEpochMilli(ts.getTime());

LocalDateTime res = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());


Convert java.util.Date to java.time.LocalDate

Date date = new Date();
Instant instant = Instant.ofEpochMilli(date.getTime());
LocalDate res = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate();

Convert java.util.Date to java.time.LocalTime

Date time = new Date();
Instant instant = Instant.ofEpochMilli(time.getTime());
LocalTime res = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalTime();

Convert java.time.LocalDateTime to java.util.Date

LocalDateTime ldt = LocalDateTime.now();
Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
Date res = Date.from(instant);

Convert java.time.LocalDate to java.util.Date

LocalDate ld =  LocalDate.now();

Instant instant = ld.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date res = Date.from(instant);

Convert java.time.LocalTime to java.util.Date

LocalTime lt = LocalTime.now();
Instant instant = lt.atDate(LocalDate.of(A_YEAR, A_MONTH, A_DAY)).
        atZone(ZoneId.systemDefault()).toInstant();

Date time = Date.from(instant);

A list of key features provided at joda-time official website:

A selection of key features:
  • LocalDate - date without time
  • LocalTime - time without date
  • Instant - an instantaneous point on the time-line
  • DateTime - full date and time with time-zone
  • DateTimeZone - a better time-zone
  • Duration and Period - amounts of time
  • Interval - the time between two instants

Wednesday, August 19, 2015

Time and Date APIs in Java 8

The standard date and time classes prior to Java SE 8 are poor. By tackling this problem head-on, Joda-Time has become the de facto standard date and time library for Java.

For example, the existing classes (such as java.util.Date and SimpleDateFormatter) aren’t thread-safe, leading to potential concurrency issues for users—not something the average developer would expect to deal with when writing date-handling code.

Some of the date and time classes also exhibit quite poor API design. For example, years in java.util.Date start at 1900, months start at 1, and days start at 0—not very intuitive.

These issues, and several others, have led to the popularity of third-party date and time libraries, such as Joda-Time. 

The new changes will appear in the Java SE8 package java.time.

A selection of key features:
  • LocalDate - date without time
  • LocalTime - time without date
  • Instant - an instantaneous point on the time-line
  • DateTime - full date and time with time-zone
  • DateTimeZone - a better time-zone
  • Duration and Period - amounts of time
  • Interval - the time between two instants
  • A comprehensive and flexible formatter-parser

Traditional Threads vs. Virtual Threads: A Performance Benchmark on Spring Boot 4

Traditional Threads vs. Virtual Threads: A Performance Benchmark on Spring Boot 4 Bottom line: For high-concurrency Spring Boot apps on mo...