0% found this document useful (0 votes)
159 views32 pages

Date & Time API: Java SE

This document provides an overview of the Date and Time API introduced in Java SE 8. The key points covered include: - The Date and Time API is located in the java.time package and provides classes like LocalDate, LocalTime and LocalDateTime to represent dates, times and datetimes. - The new API was introduced to address thread safety issues in previous APIs and provide a better designed model for working with dates and times. - Classes like Instant, LocalDate, LocalTime, LocalDateTime, ZonedDateTime and OffsetDateTime can be used to work with different representations of dates, times and datetimes. - Methods are provided to create, interpret, change values and format dates and times using the new

Uploaded by

sissthwae oo
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)
159 views32 pages

Date & Time API: Java SE

This document provides an overview of the Date and Time API introduced in Java SE 8. The key points covered include: - The Date and Time API is located in the java.time package and provides classes like LocalDate, LocalTime and LocalDateTime to represent dates, times and datetimes. - The new API was introduced to address thread safety issues in previous APIs and provide a better designed model for working with dates and times. - Classes like Instant, LocalDate, LocalTime, LocalDateTime, ZonedDateTime and OffsetDateTime can be used to work with different representations of dates, times and datetimes. - Methods are provided to create, interpret, change values and format dates and times using the new

Uploaded by

sissthwae oo
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

Date & Time API

Java SE
Contents

• What is Date and Time API?

• Why new API?

• Objects that represent Date and Time

• Using Date and Time


What is Date and Time API?

• Java SE 8 တ"င$ စတင$ပ'ဝင$လ* eသ* အခ/iန$၊ န*ရ4န5င$6


ရက$စ"8မ/*: အ*: အသu<:=ပu?iuင$သ@$6 API အသစ$

• Date And Time API အ*: [Link] package eအ*က$


တ"င$ ထ*:Biပ'သည$

• ရက$စ"8အ* LocalDate Class မ5 ကiuယ$စ*:=ပu၍အခ/iန$အ*:


LocalTime ?5င$6 LocalDateTime တiuမ5ကiuယ$စ*:=ပuပ'သည$
Why new API?
• ယခueန*က$ပiuင$:အခ/iန$မ/*:တ"င$ Multithread Program မ/*:သည$ အသu<:
မ/*:လ*

• ယခင$အသu<:=ပuခ86eသ* [Link] ?5င$6


[Link] တiuသည$ Threadsafe မ=ဖစ$Hကပ'

• ထiuအ=ပင$ API အeနန5င$6လည$ Design eက*င$:မ"န$လ5သည$ဟu မဆiu?uင$ပ'၊


ဥပမ*အ*:=ဖင$6 ရက$စ"8သည$ ၁၉၀၀ခu ၁လ ၀ရက$မ5စတင$ =ခင$:သည$ သင$6
eတ*$လ5သည$ဟuမဆiu?iuင$

• ထiueHက*င$6 Java Developer မ/*:သည$ Joda Time က86သiu Third party


API မ/*:အ*: အသu<:=ပuHကရ၏
Objects That Represent
Date and Time

• Instant

• Date Classes

• Date and Time Classes

• Time Zone Specific Classes


Instant
• Instant အ*: Time Line အeပPBi အခ/iန$အ*: Nano Second အဆင$6=ဖင$6 ကiuယ$
စ*:=ပu ရ*တ"င$အသu<:=ပuQပ4:၊ Machine Time Stamp အ*: Generate လuပ$ရ*တ"င$
အသu<:ဝင$၏

Instant timeStamp = [Link]();


Instant oneHourLater = [Link](1,
[Link]);

[Link](timeStamp);
[Link](oneHourLater);

LocalDateTime ldt = [Link](timeStamp,


[Link]());
[Link](ldt);
Date Classes
• Java Date & Time API တ"င$ ရက$စ"8အ*: ကiuယ$စ*:=ပuရန$
eအ*က$ပ' Class မ/*:အ*:=ပင$ဆင$ထ*:၏

• LocalDate

• YearMonth

• MonthDay

• Year
LocalDate
• LocalDate အ*: Year-Month-Day =ဖင$6eဖ*$=ပeသ*: အခ/iန$ မပ'ဝင$
eသ*: ISO Calendar ရက$စ"8မ/*:အ*: ကiuယ$စ*:=ပuရ*တ"င$ အသu:< =ပu၏

LocalDate memorialDay = [Link](1988, 8, 8);

[Link](memorialDay);

LocalDate nextWed = [Link](


[Link]([Link]));

[Link](nextWed);
YearMonth
• သတ$မ5တ$ထ*:eသ* ?5စ$တစ$?5စ$၏ လအ*:ကiuယ$စ*:=ပuရ*တ"င$ YearMonth အ*: အသu<:=ပuHက၏

• လတစ$လ၏ ရက$အeရအတ"က$အ* ရယRရန$ [Link] Method ကiu အ


သu<:=ပu?iuင$၏

YearMonth ym1 = [Link](2010, [Link]);


YearMonth ym2 = [Link](2012, [Link]);

[Link]("%s : %d%n", ym1, [Link]());


[Link]("%s : %d%n", ym2, [Link]());
MonthDay
• လတစ$လ၏ ရက$မ/*:အ*:ကiuယ$စ*:=ပuရန$ MonthDay အ*: အသu<:=ပuHက၏

• eဖP=ပပ' ရက$စ"8၏ မ5န$ကန$မS6 BiမBiအ*: [Link] =ဖင$6စစ$eဆ:?iuင$၏

MonthDay day = [Link]([Link], 29);

[Link]([Link](2010));
[Link]([Link](2012));
Year
• ?5စ$အ*:ကiuယ$စ*:=ပuရန$ Year အ*: အသu:< =ပuHက၏

• ?5စ$ထပ$?5စ$ကiu စစ$eဆ:ရန$ [Link] Method ကiu အသu:< =ပu?iuင$၏

Year year = [Link](2012);


[Link]([Link]());
Date And Time Classes

• LocalTime

အခ/iန$အ*: ကiuယ$စ*:=ပueသ* Class =ဖစ$၏

• LocalDateTime

ရက$စ"8န5င$6 အခ/iန$အ* တ"8Qပ4:အသu<:=ပuရ*တ"င$ သu<:?iuင$eသ*
Class =ဖစ$၏
LocalTime

LocalTime time1 = [Link]();


LocalTime time2 = [Link](12, 30);

LocalTime time3 = [Link]().toLocalTime();

[Link](time1);
[Link](time2);
[Link](time3);
LocalDateTime
// 2015-03-19T[Link].299
LocalDateTime day1 = [Link]();

// 2015-01-28T00:00
LocalDateTime day2 = [Link]
([Link](2015, [Link], 28), [Link]);

// 2015-03-19T[Link].301
LocalDateTime day3 = LocalDateTime.
ofInstant([Link](), [Link]());

// 2010-02-20T18:30
LocalDateTime day4 = [Link](2010, 2, 20, 18, 30);
Time Zone Specific Classes

• Time Zone =ဖင$6သတ$မ5တ$ထ*:eသ* အခ/iန$မ/*:အ*: အ


သu:< =ပလiueသ* အခ'မ/iu:တ"င$ ZonedDateTime ?5င$6
OffsetDateTime အ*: အသu<:=ပuHကရ၏

• Date & Time API အသစ$တ"င$ Time Zone အ*:eဖ*$=ပရန$


ZoneId ?5င$6 ZoneOffset တiuအ*:အသu<:=ပuရ၏
ZonedDateTime
DateTimeFormatter format = DateTimeFormatter
.ofPattern("dd MMM yyyy HH:mm");

LocalDate date = [Link](2015, [Link], 15);


LocalTime time = [Link](10, 30);

LocalDateTime departure = [Link](date, time);

ZonedDateTime yangonDeparture = ZonedDateTime


.of(departure, [Link]("Asia/Rangoon"));
[Link]([Link](format));

ZonedDateTime tokyoArraival = yangonDeparture


.withZoneSameInstant([Link]("Asia/Tokyo")).plusHours(6);
[Link]([Link](format));
OffsetDateTime

LocalDateTime date = LocalDateTime


.of(2015, [Link], 20, 10, 30);

OffsetDateTime tronto = OffsetDateTime


.of(date, [Link]("-04:00"));
OffsetDateTime lastMonday = tronto
.with([Link]([Link]));

[Link]("Last Monday of March is %s%n”,


[Link]());
Using Date & Time
• Creation of Object

• Interpretation

• Changing Value

• Formatting and Parsing

• Converting form Other Objects

• Period and Duration


Creation
• LocalDate, LocalTime ?5င$6 LocalDateTime Object
မ/*:အ*: Create လiuပ$ရန$ eအ*က$ပ' Static Method မ/*
အ*: အသu:< =ပu?iuင$၏

• now

• of

• ofYearDay

• parse
Creation
// local date time
[Link]("%s : %s%n", "Local DateTime", [Link]());

// local date
[Link]("%s : %s%n", "Local Date", [Link]());

// local time
[Link]("%s : %s%n", "Local Time", [Link]());

// time with specific zone


[Link]("%s : %s%n", "Zone Time", [Link]([Link]("Asia/Tokyo")));

// local date of
[Link]("%s : %s%n", "Date of", [Link](2013, 10, 12));
[Link]("%s : %s%n", "Date of", [Link](2013, [Link], 12));
[Link]("%s : %s%n", "Year of day ", [Link](2013, 150));
[Link]("%s : %s%n", "Parse Date", [Link]("2014-10-11"));

// local time of
[Link]("%s : %s%n", "Time of", [Link](17, 10, 12));
[Link]("%s : %s%n", "Parse Time", [Link]("[Link]"));

// local date time of


[Link]("%s : %s%n", "Date Time of", [Link]([Link](), [Link]()));
Interpretation
• LocalDate, LocalTime ?5င$6 LocalDateTime တiuတ"င$ အ=ခ*:eသ* Data
Type မ/*:သiue=ပ*င$:လ8ရန$ Method မ/*:အ*:=ပင$ဆင$ထ*:၏

• getDayOfWeek • getHour
• getDayOfMonth • getMinute
• getDayOfYear • getSecond
• getYear • getNano
• getMonth
• getMonthValue • isAfter
• isBefore
• isLeapYear
Interpretation
LocalDateTime now = [Link]();
[Link]("%s : %d%n", "DayOfMonth", [Link]());
[Link]("%s : %s%n", "DayOfWeek", [Link]());
[Link]("%s : %d%n", "DayOfYear", [Link]());
[Link]("%s : %d%n", "Hour", [Link]());
[Link]("%s : %d%n", "Minute", [Link]());
[Link]("%s : %s%n", "Month", [Link]());
[Link]("%s : %d%n", "MonthValue", [Link]());
[Link]("%s : %d%n", "Nano", [Link]());
[Link]("%s : %d%n", "Second", [Link]());
[Link]("%s : %d%n", "Year", [Link]());

[Link]("%s : %s%n", "isAfter",


[Link]().isAfter([Link](2014, 10, 19)));
[Link]("%s : %s%n", "isBefore",
[Link]().isBefore([Link](2014, 10, 19)));
[Link]("%s : %s%n", "isLeapYear",
[Link]().isLeapYear());
Changing Value
• LocalDate, LocalTime ?5င$6 LocalDateTime တiu၏ တန$ဖ:iu မ/*
အ*: e=ပ*င$:လ8ရန$ eအ*က$ပ' Method မ/*:အ*: အသu<:=ပu?iuင$၏

• plusYears • minusYears • withDayOfMonth


• plusMonths • minusMonths • withDayOfYear
• plusWeeks • minusWeeks • withYear
• plusDays • minusDays • withMonth
• plusHours • minusHours • withHour
• plusMinutes • minusMinutes • withMinute
• plusSeconds • minusSeconds • WithSecond
• plusNano • minusNano • withNano
• truncateTo • with
Changing Value
LocalDateTime now = [Link]();

[Link]("%s : %s%n", "withDayOfMonth", [Link](10));


[Link]("%s : %s%n", "withMonth", [Link](12));
[Link]("%s : %s%n", "withYear", [Link](2012));

[Link]("%s : %s%n", "plusDays", [Link](20));


[Link]("%s : %s%n", "plusWeeks", [Link](10));
[Link]("%s : %s%n", "ChronoUnit", [Link](1, [Link]));

[Link]("%s : %s%n", "minusYears", [Link](20));


[Link]("%s : %s%n", "minusMonths", [Link](13));

[Link]("%s : %s%n", "firstDayOfNextMonth",


[Link]([Link]()));
[Link]("%s : %s%n", "lastDayOfMonth", [Link]([Link]()));

[Link]("%s : %s%n", "Truncate To", [Link]([Link]));


Formatting and Parsing
• LocalDate, LocalTime ?5င$6 LocalDateTime Object
တiu၏ format method အ*:အသu<:=ပu၍ အခ/န$မ5 Format
ခ/ထ*:eသ* String အ=ဖစ$e=ပ*င$:လည$:?iuင$ပ'သည$

• LocalDate, LocalTime ?5င$6 LocalDateTime တiu၏


static method parse အ*:အသu<:=ပu၍ format က/ eန
eသ* String မ/*:အ*: Local Date Time အ=ဖစ$ e=ပ*င$:
လည$:?iuင$ပ'သည$
Formatting
// 2014-04-01 10:45
LocalDateTime dateTime = [Link](2014, [Link], 1, 10, 45);
[Link]("%s : %s%n", "dateTime", dateTime);

// format as basic ISO date format (20140220)


String asBasicIsoDate = [Link](DateTimeFormatter.BASIC_ISO_DATE);
[Link]("%s : %s%n", "asBasicIsoDate", asBasicIsoDate);

// format as ISO week date (2014-W08-4)


String asIsoWeekDate = [Link](DateTimeFormatter.ISO_WEEK_DATE);
[Link]("%s : %s%n", "asIsoWeekDate", asIsoWeekDate);

// format ISO date time (2014-02-20T[Link].867)


String asIsoDateTime = [Link](DateTimeFormatter.ISO_DATE_TIME);
[Link]("%s : %s%n", "asIsoDateTime", asIsoDateTime);

// using a custom pattern (01/04/2014)


String asCustomPattern = [Link]([Link]("dd/MM/yyyy"));
[Link]("%s : %s%n", "asCustomPattern", asCustomPattern);

// french date formatting (1. avril 2014)


String frenchDate = [Link]([Link]("d. MMMM yyyy", new Locale("fr")));
[Link]("%s : %s%n", "frenchDate", frenchDate);
Parsing

// parsing date strings


LocalDate fromIsoDate = [Link]("2014-01-20");
LocalDate fromIsoWeekDate = [Link]("2014-W14-2",
DateTimeFormatter.ISO_WEEK_DATE);
LocalDate fromCustomPattern = [Link]("20.01.2014",
[Link]("[Link]"));

[Link]("%s : %s%n", "fromIsoDate", fromIsoDate);


[Link]("%s : %s%n", "fromIsoWeekDate", fromIsoWeekDate);
[Link]("%s : %s%n", "fromCustomPattern", fromCustomPattern);
Converting
• Date & Time API အသစ$၏ Object  မ/*:?5င$6 ယခင$ အသu<=ပuခ86eသ* Date
Object မ/*အ*:e=ပ*င$:လ8 လiuပ'ကInstant Object အ*:Hက*:ခ<က* e=ပ*င$:လ8
ရန$ လiuအပ$၏

• LocalDateTime မ5 Instant အ*ရယRလiuပ'က toInstant Method အ*:၎င$:

• Instant မ5 LocalDateTime Object အ*:e=ပ*င$:လ8ရ*တ"င$ Static Method


=ဖစ$eသ* ofInstant method အ*:၎င$:

• Date Object မ5 Instant အ*:e=ပ*င$လ8ရ*တ"င$ toInstant method အ*:၎င$:

• Instant မ5 Date Object အ=ဖစ$e=ပ*င$:လ8ရ*တ"င$ Date ၏ static method


from အ*:အသu<:=ပuရ၏
Converting
// LocalDate/LocalTime <-> LocalDateTime
LocalDateTime now = [Link]();
[Link]("%s : %s%n", "toLocalDate", [Link]());
[Link]("%s : %s%n", "toLocalTime", [Link]());
[Link]("%s : %s%n", "toInstant", [Link]([Link]()).toInstant());
[Link]("%s : %s%n", "toInstant",
[Link]().atStartOfDay([Link]()).toInstant());

// Instant <-> LocalDateTime


Instant instant = [Link]();
[Link]("%s : %s%n", "ofInstant", [Link](instant,
[Link]("America/Los_Angeles")));
[Link]("%s : %s%n", "toInstant", [Link]([Link](-2)));

// convert old date/calendar/timezone classes


[Link]("%s : %s%n", "Date -> Instant", new Date().toInstant());
[Link]("%s : %s%n", "Calendar -> Instant", [Link]().toInstant());
[Link]("%s : %s%n", "TimeZone -> ZoneId", [Link]().toZoneId());
[Link]("%s : %s%n", "GregorianCalendar -> ZonedDateTime",
new GregorianCalendar().toZonedDateTime());

// convert to old classes


[Link]("%s : %s%n", "Instant -> Date", [Link]([Link]()));
[Link]("%s : %s%n", "ZoneId -> TimeZone",
[Link]([Link]("America/Los_Angeles")));
[Link]("%s : %s%n", "ZonedDateTime -> GregorianCalendar",
[Link]([Link]()));
Period & Duration

• အခ/iန$က*လအတiuင$:အတ*အ*: eဖ*$=ပလiueသ*အခ'
Period ?5င$6 Duration အ*:အသu<:=ပuရ၏

• Period အ*:က*လအတiuင$:အတ*အ*: ကiuယ$စ*:=ပuရ*တ"င$


အသu:< =ပuQပ4:၊ Duration အ*: အခ/iန$အတiuင$:အတ*အ*: ကiuယ$
စ*:=ပuရ*တ"င$ အသu<:=ပu၏
Period
// periods
LocalDate firstDate = [Link](2010, 5, 17); // 2010-05-17
LocalDate secondDate = [Link](2015, 3, 7); // 2015-03-07
Period period = [Link](firstDate, secondDate);

int days = [Link](); // 18


int months = [Link](); // 9
int years = [Link](); // 4
boolean isNegative = [Link](); // false
[Link]("%s : %s%n", "days", days);
[Link]("%s : %s%n", "months", months);
[Link]("%s : %s%n", "years", years);
[Link]("%s : %s%n", "isNegative", isNegative);

Period twoMonthsAndFiveDays = [Link](2).plusDays(5);


LocalDate sixthOfJanuary = [Link](2014, 1, 6);
[Link]("%s : %s%n", "twoMonthsAndFiveDays", twoMonthsAndFiveDays);
[Link]("%s : %s%n", "sixthOfJanuary", sixthOfJanuary);

// add two months and five days to 2014-01-06, result is 2014-03-11


LocalDate eleventhOfMarch = [Link](twoMonthsAndFiveDays);
[Link]("%s : %s%n", "eleventhOfMarch", eleventhOfMarch);
Duration
// durations
Instant firstInstant= [Link]( 1294881180 ); // 2011-01-13 01:13
Instant secondInstant = [Link](1294708260); // 2011-01-11 01:11
[Link]("%s : %s%n", "firstInstant", firstInstant);
[Link]("%s : %s%n", "secondInstant", secondInstant);

Duration between = [Link](firstInstant, secondInstant);


[Link]("%s : %s%n", "between", between);

// negative because firstInstant is after secondInstant (-172920)


long seconds = [Link]();
[Link]("%s : %d%n", "seconds", seconds);

// get absolute result in minutes (2882)


long absoluteResult = [Link]().toMinutes();
[Link]("%s : %d%n", "absoluteResult", absoluteResult);

// two hours in seconds (7200)


long twoHoursInSeconds = [Link](2).getSeconds();
[Link]("%s : %d%n", "twoHoursInSeconds", twoHoursInSeconds);

You might also like