Sitemap

Date & Time API of java 8

1 min readSep 30, 2024

--

Press enter or click to view image in full size
java 8 Date API vs Preceding class

Preceding to the java 8 Date API

        Date currentDate = new Date();     
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String formattedDate = formatter.format(currentDate);
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // Months are zero-based
int day = calendar.get(Calendar.DAY_OF_MONTH);
        LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = currentDateTime.format(formatter);
int year = currentDateTime.getYear();
int month = currentDateTime.getMonthValue();
int day = currentDateTime.getDayOfMonth();

As exemplified from the flow diagram and the code , it is incomprehensible . Besides it is also not thread safe, thus the coder needs to take care of synchronous logic . thus java 8 Stream acts as a remedy for hitherto concern.

Get Manoj AKP’s stories in your inbox

Join Medium for free to get updates from this writer.

It provides classes like ZonedDateTime, Duration, and Period for handling time zones, time durations, and date differences. The API helps avoid common catch related to time calculations and daylight saving time adjustments that were problematic with the older date and time classes.

--

--

Manoj AKP
Manoj AKP

Written by Manoj AKP

Software Engineer, java, javaScript,Spring Boot, React/Native, passionate in System Design & Distribution Environment.