...Nội dung I II Giới thiệu Interoperating with Legacy Code Câu hỏi trắc nghiệm I.Giới thiệu Interoperating with Legacy Code Ngày thời gian API Java phải tương thích với... thiệu Interoperating with Legacy Code Kết Date: Mon Sep 21 20:09:49 ICT 2015 Instant: 2015-09-21T13:09:49.937Z Date: Mon Sep 21 20:09:49 ICT 2015 13 I.Giới thiệu Interoperating with Legacy Code. .. hướng khác I.Giới thiệu Interoperating with Legacy Code Ví dụ :Instant & Date Date date = Date.from(instant); // API -> legacy Instant instant = date.toInstant(); // legacy -> new API Date.from
Trang 1CHƯƠNG 5: THE NEW DATE
AND TIME API
5.7 Interoperating with Legacy Code
Lớp : CNTT1.02- k57
Giáo viên hướng dẫn : Nguyễn Hồng Quang
1
Trang 2Nội dung
I Giới thiệu về Interoperating with
Legacy Code
II Câu hỏi trắc nghiệm
2
Trang 3I.Giới thiệu về Interoperating with Legacy Code
Ngày và thời gian API trong Java sẽ phải tương thích với các lớp hiện có, chẳng hạn như java.util.Date,
java.util.GregorianCalender, và
java.sql.Date/Time/Timestamp
3
Trang 4I Giới thiệu về Interoperating with Legacy Code
Lớp Thời gian hiện tại(Instant) là tương tự như lớp java.util.Date Trong Java 8, lớp này có hai phương thức bổ sung: phương thức toInstant có thể chuyển đổi một ngày(Date) sang thời gian hiện
tại(Instant), và giữ nguyên phương thức có thể chuyển đổi theo hướng khác
4
Trang 5I.Giới thiệu về Interoperating with Legacy Code
Ví dụ :Instant & Date
Date date = Date.from(instant); // API -> legacy
Instant instant = date.toInstant(); // legacy -> new API
Date.from (Instant) : tạo ra một đối tượng ngày(Date) từ một thời gian hiện tại(Instant)
Date.toInstant() : chuyển đổi một đối tượng ngày sang một thời gian hiện tại (Instant)
Trang 6I.Giới thiệu về Interoperating with Legacy Code
Tương tự như vậy, ZonedDateTime cũng tương tự
java.util.GregorianCalendar, và lớp này cũng có các phương thức
có thể chuyển đổi trong Java 8 Phương thức chuyển đổi
toZonedDateTime từ GregorianCalendar sang ZonedDateTime, và
có thể chuyển đổi ngược lại
Trang 7I.Giới thiệu về Interoperating with Legacy Code
Ví dụ: ZonedDateTime & GregorianCalendar
GregorianCalendar gc = GregorianCalendar.from(zdt) ;
ZonedDateTime zdt = cal.toZonedDateTime();
GregorianCalendar.from (ZonedDateTime) : tạo ra một đối tượng GregorianCalendar bằng cách sử dụng ngôn ngữ
mặc định từ một ZonedDateTime
cal.toZonedDateTime () : chuyển đổi
một GregorianCalendar sang một ZonedDateTime
Trang 8I.Giới thiệu về Interoperating with Legacy Code
Một bộ chuyển đổi có sẵn cho các lớp ngày và thời gian trong gói java.sql Bạn cũng có thể sử dụng một DateTimeFormatter
từ mã kế thừa để sử dụng java.text.Format
Ví dụ
Date today = new Date();
System.out.println("Today is : " + today);
//formatting date in Java using SimpleDateFormat
SimpleDateFormat DATE_FORMAT = new
SimpleDateFormat("dd-MM-yyyy");
String date = DATE_FORMAT.format(today);
System.out.println("Today in dd-MM-yyyy format : " + date);
Trang 9I.Giới thiệu về Interoperating with Legacy Code
Bảng chuyển đổi giữa các lớp java.time và lớp kế thừa
Lớp Các phương thức chuyển đổi
Date & Instant
+ Date.from(instant): tạo ra một đối tượng ngày(Date)
từ một thời gian hiện tại(Instant).
+ date.toInstant(): chuyển đổi một đối tượng ngày(Date) sang một thời gian hiện tại (Instant).
ZonedDatetime &
GregorianCalendar
+ GregorianCalendar.from(zonedDateTime): tạo ra một đối tượng GregorianCalendar từ ZonedDateTime + cal.toZonedDateTime() : chuyển đổi
một GregorianCalendar sang một ZonedDateTime.
Instant & TimeStamp
+ TimeStamp.from(instant) : tạo ra một đối tượng nhãn thời gian(TimeStamp) từ thời gian hiện
tại(Instant).
+ timestamp.toInstant() : chuyển đổi từ một nhãn thời gian(TimeStamp) sang thời gian hiện tại(Instant).
Trang 10I.Giới thiệu về Interoperating with Legacy Code
LocalDateTime
& TimeStamp
+Timestamp.valueOf(localDateTime) :tạo ra java.sql.TimeStamp từ LocalDateTime.
+ timestamp.toLocalDateTime() : chuyển đổi từ TimeStamp sang LocalDateTime
LocalDate &
Date
+ Date.valueOf(localDate) : tạo java.sql.Date từ LocalDate + date.toLocalDate() : chuyển đổi từ Date sang LocalDate
LocalTime &
Time
+ Time.valueOf(localTime) : tạo java.sql.Time từ LocalTime + time.toLocalTime() : chuyển đổi từ Time sang LocalTime
DateTimeForma
tter &
DateFormat
+ Formatter.toFormat() : chuyển đổi từ DateTimeFormatter sang DateFormat
10
Trang 11I.Giới thiệu về Interoperating with Legacy Code
TimeZone &
ZoneId
+TimeZone.getTimeZone(id) : tạo TimeZone từ ZoneId
+ timeZone.toZoneId() :chuyển đổi một đối tượng TimeZone sang một đối tượng ZoneId
FileTime &
Instant
+ FileTime.from(instant) : tạo FileTime từ Instant + fileTime.toInstant() : chuyển đổi từ FileTime sang Instant
11
Trang 12I.Giới thiệu về Interoperating with Legacy Code
Ví dụ 1:
public class Main {
public static void main(String[] args) {
Date dt = new Date();
System.out.println("Date: " + dt);
Instant in = dt.toInstant();
System.out.println("Instant: " + in);
Date dt2 = Date.from(in);
System.out.println("Date: " + dt2);
}
}
Trang 13I.Giới thiệu về Interoperating with Legacy Code
Kết quả
Date: Mon Sep 21 20:09:49 ICT 2015
Instant: 2015-09-21T13:09:49.937Z
Date: Mon Sep 21 20:09:49 ICT 2015
Trang 14I.Giới thiệu về Interoperating with Legacy Code
Ví dụ 2:
public class Main {
public static void main(String[] args) {
GregorianCalendar gc = new GregorianCalendar(2014, 2, 11, 15, 45,
50);
LocalDate ld = gc.toZonedDateTime().toLocalDate();
System.out.println("Local Date: " + ld);
LocalTime lt = gc.toZonedDateTime().toLocalTime();
System.out.println("Local Time: " + lt);
LocalDateTime ldt = gc.toZonedDateTime().toLocalDateTime();
System.out.println("Local DateTime: " + ldt);
Trang 15I.Giới thiệu về Interoperating with Legacy Code
Kết quả:
Local Date: 2014-03-11
Local Time: 15:45:50
Local DateTime: 2014-03-11T15:45:50
15
Trang 16 Câu 1:Phương thức nào dùng để chuyển đổi giữa
2 lớp java.util.Date và java.time.Instant
A Date.from(instant)
B Instant.valueOfDate();
C date.getInstant();
D Date.toLocalTime();
II Câu hỏi trắc nghiệm
Trang 17DateTimeFormatter f = DateTimeFormatter.ofPattern("MMMM dd, yyyy, hh:mm:ss");
System.out.println(dateTime.format(f));
A Jan 20, 2020 , 11:12:34
B January 20, 2020, 11:12:34
C January 20, 20, 11:12:34
D Jan 20, 20, 11:12:34
II Câu hỏi trắc nghiệm
Trang 18giữa 2 lớp java.util.GregorianCalendar và lớp
java.time.ZonedDateTime
A Instant.fromZonedDateTime();
B date.toZonedDateTime();
C zdt.toLocalTime();
D cal.toZonedDateTime();
II Câu hỏi trắc nghiệm
Trang 19Cảm ơn thầy giáo và các bạn
đã lắng nghe
19