KEMBAR78
2 Data & Time Random | PDF | Java (Programming Language) | Computer Science
0% found this document useful (0 votes)
12 views19 pages

2 Data & Time Random

Java 8 introduced the java.time package to improve date and time handling, addressing issues of mutability, confusion, and thread safety found in the older java.util.Date and java.util.Calendar. Key classes include LocalDate, LocalTime, LocalDateTime, and ZonedDateTime, each serving specific needs for date and time representation, manipulation, and comparison. The document also covers the Period and Duration classes for handling time intervals, as well as utility frameworks for common programming tasks.

Uploaded by

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

2 Data & Time Random

Java 8 introduced the java.time package to improve date and time handling, addressing issues of mutability, confusion, and thread safety found in the older java.util.Date and java.util.Calendar. Key classes include LocalDate, LocalTime, LocalDateTime, and ZonedDateTime, each serving specific needs for date and time representation, manipulation, and comparison. The document also covers the Period and Duration classes for handling time intervals, as well as utility frameworks for common programming tasks.

Uploaded by

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

Working with Data & Time

• Java 8 introduced new APIs for Date and Time (java.time ) to


address the shortcomings of the older java.util.Date and
java.util.Calendar.
• Drawbacks of old date-time API
– Mutatable (accidentally changing date objects without realizing it)
– Confusing (months started from 0, seriously?)
– Not thread-safe
– A pain to format and parse
• Core classes of java.time package,
– LocalDate
– LocalTime
– LocalDateTime
– ZonedDateTime
– Period
– Duration and their supported APIs.
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 1
CAMPUS ANNA UNIVERSITY
Working with Data & Time – Cont’d
• LocalDate class (Just the Date)
– If we are building a birthday reminder app we don’t need
the time — just the date.
– The LocalDate represents a date in ISO format (yyyy-
MM-dd) without time.
– An instance of current date can be created from the
system clock:
• LocalDate localDate = LocalDate.now();
– We can get the LocalDate representing a specific day,
month and year by using the of method or the parse
method.
– For example, these code snippets represent the LocalDate
for August 14, 2025:
• LocalDate.of(2025, 08, 14);
• LocalDate.parse("2025-08-14");
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 2
CAMPUS ANNA UNIVERSITY
Working with Data & Time – Cont’d
• Accessing date components:
– getYear(): Returns the year.
– getMonth(): Returns the month as a Month enum.
– getMonthValue(): Returns the month as an int (1-12).
– getDayOfMonth(): Returns the day of the month.
– getDayOfWeek(): Returns the day of the week as a DayOfWeek
enum.
– getDayOfYear(): Returns the day of the year
• Modifying dates:
– plusYears(long years): Adds years to the LocalDate.
– minusYears(long years): Subtracts years from the LocalDate.
– plusMonths(long months): Adds months to the LocalDate.
– minusMonths(long months): Subtracts months from the LocalDate.
– plusDays(long days): Adds days to the LocalDate.
– minusDays(long days): Subtracts days from the LocalDate
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 3
CAMPUS ANNA UNIVERSITY
Working with Data & Time – Cont’d
• Comparing dates
– isBefore(ChronoLocalDate other): Checks if this date is before another
date.
– isAfter(ChronoLocalDate other): Checks if this date is after another date.
– isEqual(ChronoLocalDate other): Checks if this date is equal to another
date.
• Other useful methods
– isLeapYear(): Checks if the year is a leap year.
– lengthOfMonth(): Returns the number of days in the month represented
by the date.
– lengthOfYear(): Returns the number of days in the year represented by
the date.
– format(DateTimeFormatter formatter): Formats the LocalDate into a
string representation using a specified DateTimeFormatter.
– toEpochDay(): Converts the LocalDate to the number of days since the
epoch (January 1, 1970)

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 4
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 5
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 6
CAMPUS ANNA UNIVERSITY
Working with Data & Time – Cont’d
• LocalTime class (Just the Time)
– The LocalTime class represents time without a date.
– when we care about the clock but not the calendar —
like setting a reminder for “5:30 PM every day”.
– An instance of the current time can be created using
the LocalTime.now() method.
• LocalTime now = LocalTime.now(); // 14:07:12.503202341
– By using of or parse method we can create an instance
of a LocalTime for a specific time.
• LocalTime eightFifteen = LocalTime.parse("08:15"); // 08:15
• LocalTime eightFifty = LocalTime.of(8, 50); // 08:50

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 7
CAMPUS ANNA UNIVERSITY
Working with Data & Time – Cont’d
• We can retrieve time information using
methods like,
– getHour(), getMinute(), getSecond(),
and getNano().
• For manipulating LocalTime objects,
– plusHours(), minusHours(), and withHour()
• Comparison can be done using methods
– compareTo(), isAfter(), and isBefore().

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 8
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 9
CAMPUS ANNA UNIVERSITY
Working with Data & Time – Cont’d
• LocalDateTime – Date and Time
– Sometimes we need both — like scheduling a meeting
or logging events.
– LocalDateTime is used to represent a combination of
date and time.
– An instance of LocalDateTime can be obtained from the
system clock similar to LocalDate and LocalTime:
• LocalDateTime.now();
– to create an instance using the factory “of” and “parse”
methods.
– The result would be a LocalDateTime instance
representing February 20, 2015, 6:30 a.m.:
• LocalDateTime.of(2015, Month.FEBRUARY, 20, 06, 30);
• LocalDateTime.parse("2015-02-20T06:30:00");

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 10
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 11
CAMPUS ANNA UNIVERSITY
Working with Data & Time – Cont’d
• ZonedDateTime class:
– ZonedDateTime is provided by Java8 to handle time-zone-
specific dates and times.
– we can get a set of all zone ids:
Set<String> allZoneIds = ZoneId.getAvailableZoneIds();
• The zone Ids would be as Asia/Aden, America/Cuiaba, etc
– We can convert the LocalDateTime to a specific zone.
LocalDateTime now = LocalDateTime.now();
ZonedDateTime zonedDT = ZonedDateTime.of(now,
ZoneId.of("Africa/Nairobi"));
– The ZonedDateTime also provides the parse method to get
time-zone-specific date-time.
ZonedDateTime zonedDT = ZonedDateTime.parse("2025-08-
20T10:15:30+05:00[Asia/Karachi]");
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 12
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 13
CAMPUS ANNA UNIVERSITY
Working with Data & Time – Cont’d
• Period Class:
– The Period class is used to modify the values of a given date
and to get the difference between two given class.
– The Period class has various getter methods such
as getYears, getMonths and getDays to get values from
a Period object.
• Duration
– Similar to Period, the Duration class is used to deal with Time.
– We can get the Duration between two instants as either
a Duration or a specific unit.
• Period is for humans — "3 years, 2 months, and 1 day."
• Duration is for machines — "in milliseconds or hours."

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 14
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 15
CAMPUS ANNA UNIVERSITY
Working with Data & Time – Cont’d
• Formatting & Parsing – Make Dates Human-Friendly
– If we want to show the date as 14-Apr-2025 instead of the
default format, we can use DateTimeFormatter and using
this we can also turn a string into a date

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 16
CAMPUS ANNA UNIVERSITY
Utility framework
• In Java, a "utility framework" often refers to a collection of
classes and interfaces designed to provide common,
reusable functionalities that assist in various programming
tasks
• java.util package:
– This package is the cornerstone of Java's built-in utility classes.
– It encompasses
• Collections Framework
• Date and Time Facilities
• Event Model
• Random Number Generation
• String Parsing and Scanning

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 17
CAMPUS ANNA UNIVERSITY
Random Number Generation
• We can create Random Number using random
class
• To use the Random Class to generate random
numbers, follow the steps below:
– Import the class java.util.Random
– Make the instance of the class Random,
i.e., Random rand = new Random()
– Invoke the nextInt(upperbound) method of rand
object to generate random numbers in the range 0
to upperbound-1.

BVL_DR.KALAM COMPUTING CENTRE, MIT


20-Aug-25 18
CAMPUS ANNA UNIVERSITY
BVL_DR.KALAM COMPUTING CENTRE, MIT
20-Aug-25 19
CAMPUS ANNA UNIVERSITY

You might also like