- vừa được xem lúc

Java SE 8 Oracle Certified Associate 1Z0-808 - #15

0 0 6

Người đăng: Tommy Le

Theo Viblo Asia

1. Câu hỏi

2. Phân tích

Đây là một câu hỏi kiểm tra kiến thức của LocalDate và LocalTime classes

 public static void main(String [] args) { LocalDate date = LocalDate.parse("1947-08-14"); LocalTime time = LocalTime.MAX; System.out.println(date.atTime(time)); }

2.1. Phân tích hàm LocalDate.parse() xem docs

public static LocalDate parse(CharSequence text)
Obtains an instance of LocalDate from a text string such as 2007-12-03.
The string must represent a valid date and is parsed using DateTimeFormatter.ISO_LOCAL_DATE. Parameters:
text - the text to parse such as "2007-12-03", not null
Returns:
the parsed local date, not null
Throws:
DateTimeParseException - if the text cannot be parsed

Hàm này không truyền format vào nên nó lấy theo format mặc định DateTimeFormatter.ISO_LOCAL_DATE Do đó text truyền vào 1947-08-14 là hợp lệ year=1947, month-of_year=08 và day-of-month=14

public static final DateTimeFormatter ISO_LOCAL_DATE
The ISO date formatter that formats or parses a date without an offset, such as '2011-12-03'.
This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended local date format. The format consists of: Four digits or more for the year. Years in the range 0000 to 9999 will be pre-padded by zero to ensure four digits. Years outside that range will have a prefixed positive or negative symbol.
A dash
Two digits for the month-of-year. This is pre-padded by zero to ensure two digits.
A dash
Two digits for the day-of-month. This is pre-padded by zero to ensure two digits.
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the STRICT resolver style.

2.2. Phân tích hàm tạo biến LocalTime

LocalTime time = LocalTime.MAX;

Đọc docs ta sẽ thấy giá trị của time = '23:59:59.999999999'

public static final LocalTime MAX
The maximum supported LocalTime, '23:59:59.999999999'. This is the time just before midnight at the end of the day.

2.3. Check code hàm atTime của LocalDate

System.out.println(date.atTime(time));

Đọc docs , như vậy hàm này sẽ trả về LocalDateTime với giá trị được tạo bỏi LocalDate và LocalTime.

public LocalDateTime atTime(LocalTime time)
Combines this date with a time to create a LocalDateTime.
This returns a LocalDateTime formed from this date at the specified time. All possible combinations of date and time are valid. Specified by:
atTime in interface ChronoLocalDate
Parameters:
time - the time to combine with, not null
Returns:
the local date-time formed from this date and the specified time, not null

Giá trị localDateTime là 1947-08-14T23:59:59.999999999

Đáp án: image.png

3. Kết luận

Bạn có thể gõ code vào IDE để chạy lại và kiểm tra Source cho câu hỏi

Bình luận

Bài viết tương tự

- vừa được xem lúc

Tổng hợp các bài hướng dẫn về Design Pattern - 23 mẫu cơ bản của GoF

Link bài viết gốc: https://gpcoder.com/4164-gioi-thieu-design-patterns/. Design Patterns là gì. Design Patterns không phải là ngôn ngữ cụ thể nào cả.

0 0 282

- vừa được xem lúc

Học Spring Boot bắt đầu từ đâu?

1. Giới thiệu Spring Boot. 1.1.

0 0 258

- vừa được xem lúc

Cần chuẩn bị gì để bắt đầu học Java

Cần chuẩn bị những gì để bắt đầu lập trình Java. 1.1. Cài JDK hay JRE.

0 0 37

- vừa được xem lúc

Sử dụng ModelMapper trong Spring Boot

Bài hôm nay sẽ là cách sử dụng thư viện ModelMapper để mapping qua lại giữa các object trong Spring nhé. Trang chủ của ModelMapper đây http://modelmapper.org/, đọc rất dễ hiểu dành cho các bạn muốn tìm hiểu sâu hơn. 1.

0 0 180

- vừa được xem lúc

[Java] 1 vài tip nhỏ khi sử dụng String hoặc Collection part 1

. Hello các bạn, hôm nay mình sẽ chia sẻ về mẹo check String null hay full space một cách tiện lợi. Mình sẽ sử dụng thư viện Lớp StringUtils download file jar để import vào thư viện tại (link).

0 0 55

- vừa được xem lúc

Deep Learning với Java - Tại sao không?

Muốn tìm hiểu về Machine Learning / Deep Learning nhưng với background là Java thì sẽ như thế nào và bắt đầu từ đâu? Để tìm được câu trả lời, hãy đọc bài viết này - có thể kỹ năng Java vốn có sẽ giúp bạn có những chuyến phiêu lưu thú vị. DJL là tên viết tắt của Deep Java Library - một thư viện mã ng

0 0 124