0% found this document useful (0 votes)
96 views

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

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 အ*: java.time 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သ* java.util.Date ?5င$6


java.util.SimpleDateformat တ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 = Instant.now();


Instant oneHourLater = timeStamp.plus(1,
ChronoUnit.HOURS);

System.out.println(timeStamp);
System.out.println(oneHourLater);

LocalDateTime ldt = LocalDateTime.ofInstant(timeStamp,


ZoneId.systemDefault());
System.out.println(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 = LocalDate.of(1988, 8, 8);

System.out.println(memorialDay);

LocalDate nextWed = memorialDay.with(


TemporalAdjusters.next(DayOfWeek.WEDNESDAY));

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

• လတစ$လ၏ ရက$အeရအတ"က$အ* ရယRရန$ YearMonth.lengthOfMonth Method ကiu အ


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

YearMonth ym1 = YearMonth.of(2010, Month.FEBRUARY);


YearMonth ym2 = YearMonth.of(2012, Month.FEBRUARY);

System.out.format("%s : %d%n", ym1, ym1.lengthOfMonth());


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

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

MonthDay day = MonthDay.of(Month.FEBRUARY, 29);

System.out.println(day.isValidYear(2010));
System.out.println(day.isValidYear(2012));
Year
• ?5စ$အ*:ကiuယ$စ*:=ပuရန$ Year အ*: အသu:< =ပuHက၏

• ?5စ$ထပ$?5စ$ကiu စစ$eဆ:ရန$ Year.isLeap Method ကiu အသu:< =ပu?iuင$၏

Year year = Year.of(2012);


System.out.println(year.isLeap());
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 = LocalTime.now();


LocalTime time2 = LocalTime.of(12, 30);

LocalTime time3 = LocalDateTime.now().toLocalTime();

System.out.println(time1);
System.out.println(time2);
System.out.println(time3);
LocalDateTime
// 2015-03-19T00:52:55.299
LocalDateTime day1 = LocalDateTime.now();

// 2015-01-28T00:00
LocalDateTime day2 = LocalDateTime.of
(LocalDate.of(2015, Month.JANUARY, 28), LocalTime.MIDNIGHT);

// 2015-03-19T00:52:55.301
LocalDateTime day3 = LocalDateTime.
ofInstant(Instant.now(), ZoneId.systemDefault());

// 2010-02-20T18:30
LocalDateTime day4 = LocalDateTime.of(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 = LocalDate.of(2015, Month.MARCH, 15);


LocalTime time = LocalTime.of(10, 30);

LocalDateTime departure = LocalDateTime.of(date, time);

ZonedDateTime yangonDeparture = ZonedDateTime


.of(departure, ZoneId.of("Asia/Rangoon"));
System.out.println(yangonDeparture.format(format));

ZonedDateTime tokyoArraival = yangonDeparture


.withZoneSameInstant(ZoneId.of("Asia/Tokyo")).plusHours(6);
System.out.println(tokyoArraival.format(format));
OffsetDateTime

LocalDateTime date = LocalDateTime


.of(2015, Month.MARCH, 20, 10, 30);

OffsetDateTime tronto = OffsetDateTime


.of(date, ZoneOffset.of("-04:00"));
OffsetDateTime lastMonday = tronto
.with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY));

System.out.format("Last Monday of March is %s%n”,


lastMonday.getDayOfMonth());
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
System.out.format("%s : %s%n", "Local DateTime", LocalDateTime.now());

// local date
System.out.format("%s : %s%n", "Local Date", LocalDate.now());

// local time
System.out.format("%s : %s%n", "Local Time", LocalTime.now());

// time with specific zone


System.out.format("%s : %s%n", "Zone Time", LocalDateTime.now(ZoneId.of("Asia/Tokyo")));

// local date of
System.out.format("%s : %s%n", "Date of", LocalDate.of(2013, 10, 12));
System.out.format("%s : %s%n", "Date of", LocalDate.of(2013, Month.APRIL, 12));
System.out.format("%s : %s%n", "Year of day ", LocalDate.ofYearDay(2013, 150));
System.out.format("%s : %s%n", "Parse Date", LocalDate.parse("2014-10-11"));

// local time of
System.out.format("%s : %s%n", "Time of", LocalTime.of(17, 10, 12));
System.out.format("%s : %s%n", "Parse Time", LocalTime.parse("15:30:00"));

// local date time of


System.out.format("%s : %s%n", "Date Time of", LocalDateTime.of(LocalDate.now(), LocalTime.now()));
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 = LocalDateTime.now();
System.out.format("%s : %d%n", "DayOfMonth", now.getDayOfMonth());
System.out.format("%s : %s%n", "DayOfWeek", now.getDayOfWeek());
System.out.format("%s : %d%n", "DayOfYear", now.getDayOfYear());
System.out.format("%s : %d%n", "Hour", now.getHour());
System.out.format("%s : %d%n", "Minute", now.getMinute());
System.out.format("%s : %s%n", "Month", now.getMonth());
System.out.format("%s : %d%n", "MonthValue", now.getMonthValue());
System.out.format("%s : %d%n", "Nano", now.getNano());
System.out.format("%s : %d%n", "Second", now.getSecond());
System.out.format("%s : %d%n", "Year", now.getYear());

System.out.format("%s : %s%n", "isAfter",


LocalDate.now().isAfter(LocalDate.of(2014, 10, 19)));
System.out.format("%s : %s%n", "isBefore",
LocalDate.now().isBefore(LocalDate.of(2014, 10, 19)));
System.out.format("%s : %s%n", "isLeapYear",
LocalDate.now().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 = LocalDateTime.now();

System.out.format("%s : %s%n", "withDayOfMonth", now.withDayOfMonth(10));


System.out.format("%s : %s%n", "withMonth", now.withMonth(12));
System.out.format("%s : %s%n", "withYear", now.withYear(2012));

System.out.format("%s : %s%n", "plusDays", now.plusDays(20));


System.out.format("%s : %s%n", "plusWeeks", now.plusWeeks(10));
System.out.format("%s : %s%n", "ChronoUnit", now.plus(1, ChronoUnit.CENTURIES));

System.out.format("%s : %s%n", "minusYears", now.minusYears(20));


System.out.format("%s : %s%n", "minusMonths", now.minusMonths(13));

System.out.format("%s : %s%n", "firstDayOfNextMonth",


now.with(TemporalAdjusters.firstDayOfNextMonth()));
System.out.format("%s : %s%n", "lastDayOfMonth", now.with(TemporalAdjusters.lastDayOfMonth()));

System.out.format("%s : %s%n", "Truncate To", now.truncatedTo(ChronoUnit.MINUTES));


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 = LocalDateTime.of(2014, Month.APRIL, 1, 10, 45);
System.out.format("%s : %s%n", "dateTime", dateTime);

// format as basic ISO date format (20140220)


String asBasicIsoDate = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.format("%s : %s%n", "asBasicIsoDate", asBasicIsoDate);

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


String asIsoWeekDate = dateTime.format(DateTimeFormatter.ISO_WEEK_DATE);
System.out.format("%s : %s%n", "asIsoWeekDate", asIsoWeekDate);

// format ISO date time (2014-02-20T20:04:05.867)


String asIsoDateTime = dateTime.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.format("%s : %s%n", "asIsoDateTime", asIsoDateTime);

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


String asCustomPattern = dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
System.out.format("%s : %s%n", "asCustomPattern", asCustomPattern);

// french date formatting (1. avril 2014)


String frenchDate = dateTime.format(DateTimeFormatter.ofPattern("d. MMMM yyyy", new Locale("fr")));
System.out.format("%s : %s%n", "frenchDate", frenchDate);
Parsing

// parsing date strings


LocalDate fromIsoDate = LocalDate.parse("2014-01-20");
LocalDate fromIsoWeekDate = LocalDate.parse("2014-W14-2",
DateTimeFormatter.ISO_WEEK_DATE);
LocalDate fromCustomPattern = LocalDate.parse("20.01.2014",
DateTimeFormatter.ofPattern("dd.MM.yyyy"));

System.out.format("%s : %s%n", "fromIsoDate", fromIsoDate);


System.out.format("%s : %s%n", "fromIsoWeekDate", fromIsoWeekDate);
System.out.format("%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 = LocalDateTime.now();
System.out.format("%s : %s%n", "toLocalDate", now.toLocalDate());
System.out.format("%s : %s%n", "toLocalTime", now.toLocalTime());
System.out.format("%s : %s%n", "toInstant", now.atZone(ZoneId.systemDefault()).toInstant());
System.out.format("%s : %s%n", "toInstant",
LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());

// Instant <-> LocalDateTime


Instant instant = Instant.now();
System.out.format("%s : %s%n", "ofInstant", LocalDateTime.ofInstant(instant,
ZoneId.of("America/Los_Angeles")));
System.out.format("%s : %s%n", "toInstant", now.toInstant(ZoneOffset.ofHours(-2)));

// convert old date/calendar/timezone classes


System.out.format("%s : %s%n", "Date -> Instant", new Date().toInstant());
System.out.format("%s : %s%n", "Calendar -> Instant", Calendar.getInstance().toInstant());
System.out.format("%s : %s%n", "TimeZone -> ZoneId", TimeZone.getDefault().toZoneId());
System.out.format("%s : %s%n", "GregorianCalendar -> ZonedDateTime",
new GregorianCalendar().toZonedDateTime());

// convert to old classes


System.out.format("%s : %s%n", "Instant -> Date", Date.from(Instant.now()));
System.out.format("%s : %s%n", "ZoneId -> TimeZone",
TimeZone.getTimeZone(ZoneId.of("America/Los_Angeles")));
System.out.format("%s : %s%n", "ZonedDateTime -> GregorianCalendar",
GregorianCalendar.from(ZonedDateTime.now()));
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 = LocalDate.of(2010, 5, 17); // 2010-05-17
LocalDate secondDate = LocalDate.of(2015, 3, 7); // 2015-03-07
Period period = Period.between(firstDate, secondDate);

int days = period.getDays(); // 18


int months = period.getMonths(); // 9
int years = period.getYears(); // 4
boolean isNegative = period.isNegative(); // false
System.out.format("%s : %s%n", "days", days);
System.out.format("%s : %s%n", "months", months);
System.out.format("%s : %s%n", "years", years);
System.out.format("%s : %s%n", "isNegative", isNegative);

Period twoMonthsAndFiveDays = Period.ofMonths(2).plusDays(5);


LocalDate sixthOfJanuary = LocalDate.of(2014, 1, 6);
System.out.format("%s : %s%n", "twoMonthsAndFiveDays", twoMonthsAndFiveDays);
System.out.format("%s : %s%n", "sixthOfJanuary", sixthOfJanuary);

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


LocalDate eleventhOfMarch = sixthOfJanuary.plus(twoMonthsAndFiveDays);
System.out.format("%s : %s%n", "eleventhOfMarch", eleventhOfMarch);
Duration
// durations
Instant firstInstant= Instant.ofEpochSecond( 1294881180 ); // 2011-01-13 01:13
Instant secondInstant = Instant.ofEpochSecond(1294708260); // 2011-01-11 01:11
System.out.format("%s : %s%n", "firstInstant", firstInstant);
System.out.format("%s : %s%n", "secondInstant", secondInstant);

Duration between = Duration.between(firstInstant, secondInstant);


System.out.format("%s : %s%n", "between", between);

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


long seconds = between.getSeconds();
System.out.format("%s : %d%n", "seconds", seconds);

// get absolute result in minutes (2882)


long absoluteResult = between.abs().toMinutes();
System.out.format("%s : %d%n", "absoluteResult", absoluteResult);

// two hours in seconds (7200)


long twoHoursInSeconds = Duration.ofHours(2).getSeconds();
System.out.format("%s : %d%n", "twoHoursInSeconds", twoHoursInSeconds);

You might also like