1. Trang chủ
  2. » Công Nghệ Thông Tin

Interoperating with Legacy code

19 344 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 19
Dung lượng 336,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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

CHƯƠ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 2

Nội dung

I Giới thiệu về Interoperating with

Legacy Code

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

2

Trang 3

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

Trang 4

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

Trang 5

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)

Trang 6

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

Trang 7

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

Trang 8

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-MM-yyyy");

String date = DATE_FORMAT.format(today);

System.out.println("Today in dd-MM-yyyy format : " + date);

Trang 9

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

+ 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 10

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

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

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);

}

}

Trang 13

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

Trang 14

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);

Trang 15

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

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 17

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

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

Trang 18

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();

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

Trang 19

Cảm ơn thầy giáo và các bạn

đã lắng nghe

19

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

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

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

TÀI LIỆU LIÊN QUAN

w