0% found this document useful (0 votes)
31 views10 pages

Utility Class

Uploaded by

Ruchita Maaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

Utility Class

Uploaded by

Ruchita Maaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

DATE AND CALENDAR - UTILITY CLASSES

Introduction to Date
and Calendar Utilities
in Java
Java Calendar class is an abstract class that provides methods for
converting date between a specific instant in time and a set of calendar
fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and
implements the Comparable interface..
The java.time Package

1 Modern Approach 2 Immutable Objects


The java.time package, introduced in Date and time objects are immutable,
Java 8, represents a significant leap promoting thread safety and
forward in date and time handling. preventing accidental modifications.

3 Clarity and Readability 4 Comprehensive Functionality


The API offers intuitive classes with The package provides a rich set of
clear names and methods for easy classes for dates, times, durations,
comprehension. periods, and zones.
Representing Dates and Times

LocalDate LocalTime LocalDateTime

Represents a date without Represents a time-of-day Represents a date and


time-of-day information. without date information. time combined.
Date and Time Formatting
DateTimeFormatter Predefined Patterns
The DateTimeFormatter class provides Leverage predefined patterns like
flexible formatting options for ISO_DATE or ISO_TIME for common
converting date and time objects to date and time formats.
strings and vice versa.

Custom Patterns
Define custom patterns using symbols like yyyy, MM, dd, HH, mm, ss to format dates
and times according to your requirements.
Performing Date and Time Calculations

Period Duration Calculating Differences


Represents a period of Represents a duration of Calculate differences
time in terms of years, time in terms of seconds between dates or times
months, and days. and nanoseconds. using methods like
between() or until().
DATE AND CALENDAR PROGRAM
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class DateAndCalendarUtils {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
LocalTime currentTime = LocalTime.now();
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
System.out.println("Formatted Date: " + currentDate.format(dateFormatter));
System.out.println("Formatted Time: " + currentTime.format(timeFormatter));
LocalDate parsedDate = LocalDate.parse("14-06-2024", dateFormatter);
LocalTime parsedTime = LocalTime.parse("14:30:00", timeFormatter);
LocalDate tomorrow = currentDate.plusDays(1);
LocalTime inTwoHours = currentTime.plusHours(2);
OUTPUT:
The current date is : Thu Jan 19 18:47:02 IST 2017
15 days ago: Wed Jan 04 18:47:02 IST 2017
4 months later: Thu May 04 18:47:02 IST 2017
2 years later: Sat May 04 18:47:02 IST 2019
Conclusion and Best Practices

Choose the Right Handle Time Zones Format Consistently Test Thoroughly
Tools Be mindful of time Use consistent Thoroughly test
Select the most zones when formatting patterns your code to verify
appropriate classes working with dates for dates and times that date and time
from the java.time and times, to ensure clarity calculations are
package for your especially in global and readability. accurate.
specific needs. applications.
THANK YOU
By N.KAYALVIZHI
252310024

You might also like