SlideShare a Scribd company logo
Programming in Java
Topic: Date Time API
Contents…
 Introduction
 Local Date
 Local Time
 Local Date Time
Introduction
 New DateTime API is introduced in jdk8.
 LocalDate, LocalTime and LocalDateTime classes are provided
in java.time package.
 The Date class encapsulates the current date and time.
Java Date and Time API goals
 Classes and methods should be straight forward.
 The API should support fluent API approach.
 Instances of Date and Time objects should be immutable.
 Should be thread safe.
 Use ISO standard to define Date and Time.
 API should support strong type checks.
 Allows developers to extend API.
Working with Local Date and Time
 Java.time package provides two classes for working with local
Date and Time.
 LocalDate
 Does not include time
 A year-month-day representation
 toString – ISO 8601 format(YYYY-MM-DD)
 LocalTime
 Does not include date
 Stores hours:minutes:seconds:nanoseconds
 toString- (HH:mm:ss.SSS)
LocalDate Class
LocalDate Class
 A date without a time-zone in the ISO-8601 calendar system,
such as 2021-02-03(YYYY-MM-DD).
 LocalDate is an immutable date-time object that represents a
date, often viewed as year-month-day.
 Other date fields, such as day-of-year, day-of-week and week-
of-year, can also be accessed.
 This class does not store or represent a time or time-zone so its
portable across time zones.
Methods of LocalDate
 public static LocalDate now()
 public static LocalDate now(ZoneId zone)
 public static LocalDate of(int year, Month month, int
dayOfMonth)
 public static LocalDate of(int year, int month, int dayOfMonth)
Note: DateTimeException can be thrown.
 public static LocalDate parse(CharSequence text)
Note: DateTimeParseException can be thrown.
Example
LocalDate ldt = LocalDate.now();
ldt = LocalDate.of(2015, Month.FEBRUARY, 28);
ldt = LocalDate.of(2015, 2, 13);
ldt = LocalDate.parse("2017-02-28”);
Methods of LocalDate
Methods
 public int getYear()
 public int getMonthValue()
 public Month getMonth()
 public int getDayOfMonth()
 public int getDayOfYear()
 public DayOfWeek getDayOfWeek()
 public boolean isLeapYear()
Methods of LocalDate
 public LocalDate withYear(int year)
 public LocalDate withMonth(int month)
 public LocalDate withDayOfMonth(int dayOfMonth)
 public LocalDate withDayOfYear(int dayOfYear)
Methods of LocalDate
 public LocalDate plusYears(long yearsToAdd)
 public LocalDate plusMonths(long monthsToAdd)
 public LocalDate plusWeeks(long weeksToAdd)
 public LocalDate plusDays(long daysToAdd)
 public LocalDate minusYears(long yearsToSubtract)
 public LocalDate minusMonths(long monthsToSubtract)
 public LocalDate minusWeeks(long weeksToSubtract)
 public LocalDate minusDays(long daysToSubtract)
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args){
LocalDate date=LocalDate.now();
LocalDate yesterday=date.minusDays(1);
LocalDate tomorrow=yesterday.plusDays(2);
System.out.println("Today date: "+date);
System.out.println("Yesterday date:"+yesterday);
System.out.println("Tommorow date: "+tomorrow);
}
}
import java.time.LocalDate;
import java.time.Month;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println("Present date : " +now);
int year = now.getYear();
Month month = now.getMonth();
int day = now.getDayOfMonth();
System.out.println("year : " + year + " , month : " + month + ",
day : " + day);
int monthInt = now.getMonthValue();
System.out.println("today date : " + year + "-" + monthInt +
"-" + day);
LocalDate oldDate = LocalDate.of(2015, 2,2);
System.out.println("Old date : " + oldDate);
}
}
LocalTime Class
LocalTime Class
 A time without a time-zone in the ISO-8601 calendar system,
such as 10:15:30. 13
 LocalTime is an immutable date-time object that represents a
time, often viewed as hour-minute-second.
 Time is represented to nanosecond precision.
 For example, the value "13:45:30.123" can be stored in a
LocalTime.
 This class does not store or represent a date or time-zone.
Methods of LocalTime
Methods
 public static LocalTime now()
 public static LocalTime now(ZoneId zone)
 public static LocalTime of(int hour, int minute)
 public static LocalTime of(int hour, int minute, int second)
 public static LocalTime of(int hour, int min, int sec, int nsec)
 public static LocalTime parse(CharSequence text)
Methods of LocalTime
Methods
 public int getHour()
 public int getMinute()
 public int getSecond()
 public int getNano()
 public boolean isAfter(LocalTime l)
 public boolean isBefore(LocalTime l)
 public LocalDateTime atDate(LocalDate d)
Methods of LocalTime
Methods
 public LocalTime withHour(int hour)
 public LocalTime withMinute(int min)
 public LocalTime withSecond(int sec)
 public LocalTime withNano(int ns)
Methods of LocalTime
Methods
 public LocalTime plusHours(long hours)
 public LocalTime plusMinutes(long min)
 public LocalTime plusSeconds(long sec)
 public LocalTime plusNanos(long ns)
 public LocalTime minusHours(long hours)
 public LocalTime minusMinutes(long min)
 public LocalTime minusSeconds(long sec)
 public LocalTime minusNanos(long ns)
import java.time.LocalTime;
public class LocalTimeExample {
public static void main(String[] args) {
LocalTime now = LocalTime.now();
System.out.println("Present time : " +now);
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.println("Hour : " + hour + " ,Minute : " + minute + ",
Second : " + second);
int nanoSec = now.getNano();
System.out.println("time now in format : " + hour + ":" + minute
+ ":0" + second + ":" + nanoSec);
LocalTime oldTime = LocalTime.of(13, 20,25);
System.out.println("Old time : " + oldTime);
}
}
LocalDateTime Class
LocalDateTime Class
 A date-time without a time-zone in the ISO-8601 calendar
system, such as 2007-12-03T10:15:30.
 LocalDateTime is an immutable date-time object that
represents a date-time, often viewed as year-month-day-hour-
minute-second.
 Other date and time fields, such as day-of-year, day-of-week
and week-of-year, can also be accessed.
 Time is represented to nanosecond precision.
 For example, the value "2nd October 2007 at
13:45.30.123456789" can be stored in a LocalDateTime.
Methods of LocalDateTime
Methods
 public static LocalDateTime now()
 public static LocalDateTime now(ZoneId zone)
 public static LocalDateTime of(int year, int mnth, int day, int
hour, int mint)
 public static LocalDateTime of(int year, int mnth, int day, int
hour, int mint, int sec)
 public static LocalDateTime of(int year, int mnth, int day, int
hour, int mint, int sec, int nsec)
 public static LocalDateTime of(LocalDate d, LocalTime t)
 public static LocalDateTime parse(CharSequence text)
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
class LocalDateTimeExample {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("Present date : " +now);
int year = now.getYear();
Month month = now.getMonth();
int day = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.println("year : " + year + " ,month : " + month + ", day : " + day + "Hour :
" + hour+ " , Minute : " + minute + ", Second :" + second);
LocalDateTime oldDate = LocalDateTime.of(2000, 12, 10, 10, 10, 10);
System.out.println("Old date : " + oldDate);
LocalDate newlocalDate = now.toLocalDate();
System.out.println("newlocalDate:" +newlocalDate);
LocalTime newlocalTime = now.toLocalTime();
System.out.println("newlocalTime:"+ newlocalTime);
LocalDateTime strToDate =LocalDateTime.parse("2020-11-10T10:10:10");
System.out.println("String to Date : " +strToDate);
} }
Working with dates and
times across time zones
Date/Time Formatting
 java.time.format.DateTimeFormatter class is used for
printing and parsing date-time objects.
 This class works by using:
Predefined constants, such as ISO_LOCAL_DATE
Pattern letters, such as yyyy-MMM-dd
Localized styles, such as long or medium
Date/Time Formatting
 The date-time classes provide two methods - one for
formatting and one for Parsing.
Formatting Example:
LocalDateTime dt =
LocalDateTime.of( 2010, Month.JULY, 03, 09, 0, 30 );
String isoDateTime =
dt.format(DateTimeFormatter.ISO_DATE_TIME);
Parsing Example:
LocalDate dt = LocalDate.parse("2014/09/19 14:05:12",
DateTimeFormatter.ofPattern( "yyyy/MM/dd kk:mm:ss" ) );
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
class ZonedDateTimeExample {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("India current DateTime : " + currentDateTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy
HH:mm:ss");
ZonedDateTime zonedDateTime =
currentDateTime.atZone(ZoneId.of("Australia/Sydney"));
System.out.println("Aus Zoned time : " + formatter.format(zonedDateTime));
ZoneId zoneId = zonedDateTime.getZone();
System.out.println("zone id : " + zoneId);
ZonedDateTime pstZoneTime = zonedDateTime.withZoneSameInstant(ZoneId.of
("America/Los_Angeles"));
System.out.println("PST time now : " + formatter.format(pstZoneTime));
}
}
ABSTRACT CLASS
vs
INTERFACE
METHOD
vs
CONSTRUCTOR
ENUM
vs
CLASS
METHOD OVERLOADING
vs
METHOD OVERRIDDING
IN JAVA
STATIC BLOCK
vs
ANONYMOUS BLOCK
STATIC VARIABLE
vs
LOCAL VARIABLE
Vs
INSTANCE VARIABLE

More Related Content

What's hot

REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
Joshua Long
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
Emanuele DelBono
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
Rohit Verma
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Scott Leberknight
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
David Gómez García
 
Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.
boyney123
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show file
Saurabh Tripathi
 
React js
React jsReact js
React js
Alireza Akbari
 
How Hashmap works internally in java
How Hashmap works internally  in javaHow Hashmap works internally  in java
How Hashmap works internally in java
Ramakrishna Joshi
 
Spring Core
Spring CoreSpring Core
Spring Core
Pushan Bhattacharya
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
Purbarun Chakrabarti
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
Knoldus Inc.
 
Java8 features
Java8 featuresJava8 features
Java8 features
Elias Hasnat
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
Information Technology
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
Pravin Pundge
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
NexThoughts Technologies
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolved
trxcllnt
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
Yao Nien Chung
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Serhat Can
 

What's hot (20)

REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show file
 
React js
React jsReact js
React js
 
How Hashmap works internally in java
How Hashmap works internally  in javaHow Hashmap works internally  in java
How Hashmap works internally in java
 
Spring Core
Spring CoreSpring Core
Spring Core
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
 
Java8 features
Java8 featuresJava8 features
Java8 features
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolved
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 

Similar to 15. DateTime API.ppt

Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8
Fulvio Corno
 
ThreeTen
ThreeTenThreeTen
ThreeTen
彥彬 洪
 
JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8
Serhii Kartashov
 
Java22_1670144363.pptx
Java22_1670144363.pptxJava22_1670144363.pptx
Java22_1670144363.pptx
DilanAlmsa
 
Date object.pptx date and object v
Date object.pptx date and object        vDate object.pptx date and object        v
Date object.pptx date and object v
22x026
 
Java Day-7
Java Day-7Java Day-7
Java Day-7
People Strategists
 
Java dates
Java datesJava dates
Java dates
Sujit Kumar
 
Php date & time functions
Php date & time functionsPhp date & time functions
Php date & time functions
Programmer Blog
 
Jsr310
Jsr310Jsr310
Jsr310
彥彬 洪
 
How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3
allanh0526
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Codemotion
 
17 ruby date time
17 ruby date time17 ruby date time
17 ruby date time
Walker Maidana
 
Util
UtilUtil
Utility classes
Utility classesUtility classes
Utility classes
Icancode
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...
MaruMengesha
 
Class & Objects in JAVA.ppt
Class & Objects in JAVA.pptClass & Objects in JAVA.ppt
Class & Objects in JAVA.ppt
RohitPaul71
 
Java 8
Java 8Java 8
Java 8
Raghda Salah
 
Computer programming 2 Lesson 14
Computer programming 2  Lesson 14Computer programming 2  Lesson 14
Computer programming 2 Lesson 14
MLG College of Learning, Inc
 
C++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdfC++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdf
jaipur2
 
Please I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdfPlease I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdf
ankit11134
 

Similar to 15. DateTime API.ppt (20)

Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8
 
ThreeTen
ThreeTenThreeTen
ThreeTen
 
JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8
 
Java22_1670144363.pptx
Java22_1670144363.pptxJava22_1670144363.pptx
Java22_1670144363.pptx
 
Date object.pptx date and object v
Date object.pptx date and object        vDate object.pptx date and object        v
Date object.pptx date and object v
 
Java Day-7
Java Day-7Java Day-7
Java Day-7
 
Java dates
Java datesJava dates
Java dates
 
Php date & time functions
Php date & time functionsPhp date & time functions
Php date & time functions
 
Jsr310
Jsr310Jsr310
Jsr310
 
How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
 
17 ruby date time
17 ruby date time17 ruby date time
17 ruby date time
 
Util
UtilUtil
Util
 
Utility classes
Utility classesUtility classes
Utility classes
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...
 
Class & Objects in JAVA.ppt
Class & Objects in JAVA.pptClass & Objects in JAVA.ppt
Class & Objects in JAVA.ppt
 
Java 8
Java 8Java 8
Java 8
 
Computer programming 2 Lesson 14
Computer programming 2  Lesson 14Computer programming 2  Lesson 14
Computer programming 2 Lesson 14
 
C++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdfC++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdf
 
Please I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdfPlease I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdf
 

Recently uploaded

Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
integral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdfintegral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdf
gaafergoudaay7aga
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...
IJECEIAES
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
Madan Karki
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
riddhimaagrawal986
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 

Recently uploaded (20)

Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
integral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdfintegral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdf
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...An improved modulation technique suitable for a three level flying capacitor ...
An improved modulation technique suitable for a three level flying capacitor ...
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
People as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimalaPeople as resource Grade IX.pdf minimala
People as resource Grade IX.pdf minimala
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 

15. DateTime API.ppt

  • 2. Contents…  Introduction  Local Date  Local Time  Local Date Time
  • 3. Introduction  New DateTime API is introduced in jdk8.  LocalDate, LocalTime and LocalDateTime classes are provided in java.time package.  The Date class encapsulates the current date and time.
  • 4. Java Date and Time API goals  Classes and methods should be straight forward.  The API should support fluent API approach.  Instances of Date and Time objects should be immutable.  Should be thread safe.  Use ISO standard to define Date and Time.  API should support strong type checks.  Allows developers to extend API.
  • 5. Working with Local Date and Time  Java.time package provides two classes for working with local Date and Time.  LocalDate  Does not include time  A year-month-day representation  toString – ISO 8601 format(YYYY-MM-DD)  LocalTime  Does not include date  Stores hours:minutes:seconds:nanoseconds  toString- (HH:mm:ss.SSS)
  • 7. LocalDate Class  A date without a time-zone in the ISO-8601 calendar system, such as 2021-02-03(YYYY-MM-DD).  LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day.  Other date fields, such as day-of-year, day-of-week and week- of-year, can also be accessed.  This class does not store or represent a time or time-zone so its portable across time zones.
  • 8. Methods of LocalDate  public static LocalDate now()  public static LocalDate now(ZoneId zone)  public static LocalDate of(int year, Month month, int dayOfMonth)  public static LocalDate of(int year, int month, int dayOfMonth) Note: DateTimeException can be thrown.  public static LocalDate parse(CharSequence text) Note: DateTimeParseException can be thrown.
  • 9. Example LocalDate ldt = LocalDate.now(); ldt = LocalDate.of(2015, Month.FEBRUARY, 28); ldt = LocalDate.of(2015, 2, 13); ldt = LocalDate.parse("2017-02-28”);
  • 10. Methods of LocalDate Methods  public int getYear()  public int getMonthValue()  public Month getMonth()  public int getDayOfMonth()  public int getDayOfYear()  public DayOfWeek getDayOfWeek()  public boolean isLeapYear()
  • 11. Methods of LocalDate  public LocalDate withYear(int year)  public LocalDate withMonth(int month)  public LocalDate withDayOfMonth(int dayOfMonth)  public LocalDate withDayOfYear(int dayOfYear)
  • 12. Methods of LocalDate  public LocalDate plusYears(long yearsToAdd)  public LocalDate plusMonths(long monthsToAdd)  public LocalDate plusWeeks(long weeksToAdd)  public LocalDate plusDays(long daysToAdd)  public LocalDate minusYears(long yearsToSubtract)  public LocalDate minusMonths(long monthsToSubtract)  public LocalDate minusWeeks(long weeksToSubtract)  public LocalDate minusDays(long daysToSubtract)
  • 13. import java.time.LocalDate; public class LocalDateExample { public static void main(String[] args){ LocalDate date=LocalDate.now(); LocalDate yesterday=date.minusDays(1); LocalDate tomorrow=yesterday.plusDays(2); System.out.println("Today date: "+date); System.out.println("Yesterday date:"+yesterday); System.out.println("Tommorow date: "+tomorrow); } }
  • 14. import java.time.LocalDate; import java.time.Month; public class LocalDateExample { public static void main(String[] args) { LocalDate now = LocalDate.now(); System.out.println("Present date : " +now); int year = now.getYear(); Month month = now.getMonth(); int day = now.getDayOfMonth(); System.out.println("year : " + year + " , month : " + month + ", day : " + day); int monthInt = now.getMonthValue(); System.out.println("today date : " + year + "-" + monthInt + "-" + day); LocalDate oldDate = LocalDate.of(2015, 2,2); System.out.println("Old date : " + oldDate); } }
  • 16. LocalTime Class  A time without a time-zone in the ISO-8601 calendar system, such as 10:15:30. 13  LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second.  Time is represented to nanosecond precision.  For example, the value "13:45:30.123" can be stored in a LocalTime.  This class does not store or represent a date or time-zone.
  • 17. Methods of LocalTime Methods  public static LocalTime now()  public static LocalTime now(ZoneId zone)  public static LocalTime of(int hour, int minute)  public static LocalTime of(int hour, int minute, int second)  public static LocalTime of(int hour, int min, int sec, int nsec)  public static LocalTime parse(CharSequence text)
  • 18. Methods of LocalTime Methods  public int getHour()  public int getMinute()  public int getSecond()  public int getNano()  public boolean isAfter(LocalTime l)  public boolean isBefore(LocalTime l)  public LocalDateTime atDate(LocalDate d)
  • 19. Methods of LocalTime Methods  public LocalTime withHour(int hour)  public LocalTime withMinute(int min)  public LocalTime withSecond(int sec)  public LocalTime withNano(int ns)
  • 20. Methods of LocalTime Methods  public LocalTime plusHours(long hours)  public LocalTime plusMinutes(long min)  public LocalTime plusSeconds(long sec)  public LocalTime plusNanos(long ns)  public LocalTime minusHours(long hours)  public LocalTime minusMinutes(long min)  public LocalTime minusSeconds(long sec)  public LocalTime minusNanos(long ns)
  • 21. import java.time.LocalTime; public class LocalTimeExample { public static void main(String[] args) { LocalTime now = LocalTime.now(); System.out.println("Present time : " +now); int hour = now.getHour(); int minute = now.getMinute(); int second = now.getSecond(); System.out.println("Hour : " + hour + " ,Minute : " + minute + ", Second : " + second); int nanoSec = now.getNano(); System.out.println("time now in format : " + hour + ":" + minute + ":0" + second + ":" + nanoSec); LocalTime oldTime = LocalTime.of(13, 20,25); System.out.println("Old time : " + oldTime); } }
  • 23. LocalDateTime Class  A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.  LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour- minute-second.  Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed.  Time is represented to nanosecond precision.  For example, the value "2nd October 2007 at 13:45.30.123456789" can be stored in a LocalDateTime.
  • 24. Methods of LocalDateTime Methods  public static LocalDateTime now()  public static LocalDateTime now(ZoneId zone)  public static LocalDateTime of(int year, int mnth, int day, int hour, int mint)  public static LocalDateTime of(int year, int mnth, int day, int hour, int mint, int sec)  public static LocalDateTime of(int year, int mnth, int day, int hour, int mint, int sec, int nsec)  public static LocalDateTime of(LocalDate d, LocalTime t)  public static LocalDateTime parse(CharSequence text)
  • 25. import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Month; class LocalDateTimeExample { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println("Present date : " +now); int year = now.getYear(); Month month = now.getMonth(); int day = now.getDayOfMonth(); int hour = now.getHour(); int minute = now.getMinute(); int second = now.getSecond(); System.out.println("year : " + year + " ,month : " + month + ", day : " + day + "Hour : " + hour+ " , Minute : " + minute + ", Second :" + second); LocalDateTime oldDate = LocalDateTime.of(2000, 12, 10, 10, 10, 10); System.out.println("Old date : " + oldDate); LocalDate newlocalDate = now.toLocalDate(); System.out.println("newlocalDate:" +newlocalDate); LocalTime newlocalTime = now.toLocalTime(); System.out.println("newlocalTime:"+ newlocalTime); LocalDateTime strToDate =LocalDateTime.parse("2020-11-10T10:10:10"); System.out.println("String to Date : " +strToDate); } }
  • 26. Working with dates and times across time zones
  • 27. Date/Time Formatting  java.time.format.DateTimeFormatter class is used for printing and parsing date-time objects.  This class works by using: Predefined constants, such as ISO_LOCAL_DATE Pattern letters, such as yyyy-MMM-dd Localized styles, such as long or medium
  • 28. Date/Time Formatting  The date-time classes provide two methods - one for formatting and one for Parsing. Formatting Example: LocalDateTime dt = LocalDateTime.of( 2010, Month.JULY, 03, 09, 0, 30 ); String isoDateTime = dt.format(DateTimeFormatter.ISO_DATE_TIME); Parsing Example: LocalDate dt = LocalDate.parse("2014/09/19 14:05:12", DateTimeFormatter.ofPattern( "yyyy/MM/dd kk:mm:ss" ) );
  • 29. import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; class ZonedDateTimeExample { public static void main(String[] args) { LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println("India current DateTime : " + currentDateTime); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm:ss"); ZonedDateTime zonedDateTime = currentDateTime.atZone(ZoneId.of("Australia/Sydney")); System.out.println("Aus Zoned time : " + formatter.format(zonedDateTime)); ZoneId zoneId = zonedDateTime.getZone(); System.out.println("zone id : " + zoneId); ZonedDateTime pstZoneTime = zonedDateTime.withZoneSameInstant(ZoneId.of ("America/Los_Angeles")); System.out.println("PST time now : " + formatter.format(pstZoneTime)); } }
  • 30.