Interoperating with Legacy code

19 344 0
Interoperating with Legacy code

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

...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

CHƯƠNG 5: THE NEW DATE AND TIME API 5.7. Interoperating with Legacy Code Họ tên MSSV Lớp Giáo viên hướng dẫn : Nguyễn Đức Hậu : 20124977 : CNTT1.02- k57 : Nguyễn Hồng Quang 1 Nội dung I. II. Giới thiệu về Interoperating with Legacy Code Câu hỏi trắc nghiệm 2 I.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 I.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 I.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).  5 I.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 6 I.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  7 I.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-MMyyyy"); String date = DATE_FORMAT.format(today); System.out.println("Today in dd-MM-yyyy format : " + date); 8 I.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     ZonedDatetime &  GregorianCalendar + 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).   + 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.   + TimeStamp.from(instant) : tạo ra một đối tượng  nhãn thời gian(TimeStamp) từ thời gian hiện  Instant & TimeStamp 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). 9 I.Giới thiệu về Interoperating with Legacy Code   +Timestamp.valueOf(localDateTime) :tạo ra  LocalDateTime  java.sql.TimeStamp từ LocalDateTime. & TimeStamp + 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   + Formatter.toFormat() : chuyển đổi từ DateTimeFormatter  DateTimeForma sang DateFormat tter &  DateFormat 10 I.Giới thiệu về Interoperating with Legacy Code   TimeZone &  ZoneId    FileTime &  Instant  +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.from(instant) : tạo FileTime từ Instant + fileTime.toInstant() : chuyển đổi từ FileTime sang  Instant 11 I.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); } 12 I.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  13 I.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); 14 I.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 II. Câu hỏi trắc nghiệm  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. B. C. D. Date.from(instant) Instant.valueOfDate(); date.getInstant(); Date.toLocalTime(); 16 II. Câu hỏi trắc nghiệm  Câu 2: Kết quả đoạn code sau là : DateTimeFormatter 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 17 II. Câu hỏi trắc nghiệm  Câu 3: Phương thức nào dùng để chuyển đổi giữ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(); 18 Cảm ơn thầy giáo và các bạn đã lắng nghe 19 [...]...I.Giới thiệu về Interoperating with Legacy Code   TimeZone &  ZoneId    FileTime &  Instant  +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.from(instant) : tạo FileTime từ Instant + fileTime.toInstant() : chuyển đổi từ FileTime sang  Instant 11 I.Giới thiệu về Interoperating with Legacy Code Ví dụ 1: public... System.out.println("Instant: " + in); Date dt2 = Date.from(in); System.out.println("Date: " + dt2); } 12 I.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  13 I.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,... I.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 II Câu hỏi trắc nghiệm  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 B C D Date.from(instant) Instant.valueOfDate(); date.getInstant(); Date.toLocalTime(); 16 II Câu hỏi trắc nghiệm  Câu 2: Kết quả đoạn code sau là :

Ngày đăng: 29/09/2015, 08:15

Mục lục

  • CHƯƠNG 5: THE NEW DATE AND TIME API 5.7. Interoperating with Legacy Code

  • I.Giới thiệu về Interoperating with Legacy Code

  • II. Câu hỏi trắc nghiệm

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan