Skip to main content

Java8 Features - Date and Time API enhancements

Date and Time API - JSR 310 - Need for the enhancement

Anybody who has worked with Java date, calendar and time prior to java8 has had to google at least a few times before their code worked fine. that was how tedious or non-intuitive the entire code surrouding the date and time model was. 

Secondly, the objects related to date time were all mutable, causing issues in multithreaded functionalities. (classic example use of SimpleDateFormat.parse()).

Third, missing common operations like finding time in TZ x knowing current time in TZ y, month, day, year operations on date etc.

Updates in java8

Completely new model of date and time based on the calendar system defined in ISO-8601. java.time.* package 
Two types of times:
  • human time (LocalDate, LocalTime, LocalDateTime, ZonedDateTime,Period)
  • continuous time / unix timestamp (Instant, duration)
One can implement their own calendar system by extending the ChronoLocalDate.

Sample code

Ease of doing date manipulations:
 //code to print next Friday  
 LocalDate today= LocalDate.now();
 System.out.println(today.plusWeeks(1).with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));

Ease of doing formatting
 LocalDateTime now = LocalDateTime.now();
 System.out.println(now.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")));


Ease of date parsing
 LocalDate d3 = LocalDate.parse("18/07/2017",DateTimeFormatter.ofPattern("dd/MM/yyyy"));
 System.out.println(d3);

Ease of period /duration between 2 dates:
 LocalDate travelDate=LocalDate.of(2015, Month.NOVEMBER, 21);
 LocalDate returnDate = LocalDate.now();
 Period travelPeriod = Period.between(travelDate, returnDate);
 System.out.println("You were travelling for a period of "+travelPeriod.getYears()+" years and "+travelPeriod.getMonths()+ " months and  "+travelPeriod.getDays()+" days ( total of "+ChronoUnit.DAYS.between(travelDate, returnDate)+" days)");

Popular posts from this blog

AWS Developer Associate certification

Almost a month since i completed my AWS DA certification. Got 94% overall score. This post is about my preparation time and resources. I spent almost 4-5 hours  on weekdays for 1 calendar month to learn about AWS. I did not have any background of AWS before this. I researched on which resources to use to learn and prepare for the certification. Undoubtedly, acloudguru comes up as the most popular study guide followed by whizlabs for the timed exam preparation. I bought the course on acloudguru and whizlabs. I setup my AWS free tier account. And at the end of each lesson i did the labs on my aws account and read the FAQ about that lesson from the AWS website. My approach to preparation was as much hands-on as possible. I found the course material for developer associate on acloudguru an OK resource. Something that will help you focus on what topics to cover rather than something that helps you understand the AWS ocean. The quizzes are nowhere close to the exam. Real exam has ...

why not to sysout from inside a loop!!

After 2 year of being an out of touch coder, I committed the sin and the penalty -- "a code that is 5 times slower." try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { while (...) { ...                 System.out.println(..myresult..); ... } } v/s try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printWriter = new PrintWriter(System.out)) { while (...) {             ... printWriter.write(..myresult..+"\n"); ... } printWriter.flush(); } the improvised code can by written in multiple ways.. use StringBuilder/buffer and append the result inside the loop and sysout at the end of the loop OR like the code above spool it to writer and flush it.

Java 8 Tutorial for Lambda

For those of you who are searching internet for good resource on Lambda, you must read the oracle tutorial.  Java 8 Lambda Quickstart I think this is one of the best tutorial with some simple examples that help you understand when and how to use Lambdas.