Given the code fragment:
What is the result?
Given the code fragment:
What is the result?
The method truncatedTo(ChronoUnit.MINUTES) truncates the Instant object to the nearest minute. Given the loginTime of 2021-01-12T21:58:18.817Z and the logoutTime of 2021-01-12T21:58:19.880Z, both will be truncated to 2021-01-12T21:58:00Z. After truncation, loginTime and logoutTime are equal, so logoutTime.isAfter(loginTime) will return false, and the else statement will execute, printing 'Can't logout'.
A is correct: truncatedTo() coverts the logoutTime=LoginTime => else is reached.
package q04; import java.time.Instant; import java.time.temporal.ChronoUnit; public class Q04 { public static void main(String[] args) throws InterruptedException { // Login time: 2021-01-12T21:58:18.817Z Instant loginTime = Instant.now(); Thread.sleep(1000); // Logout time: 2021-01-12T21:58:19.880Z Instant logoutTime = Instant.now(); loginTime = loginTime.truncatedTo(ChronoUnit.MINUTES); // line n1 logoutTime = logoutTime.truncatedTo(ChronoUnit.MINUTES); if (logoutTime.isAfter(loginTime)) { System.out.println("Logged out at: " + logoutTime); } else { System.out.println("Can't logout"); } } } // Result: // Can't logout
A is correct
Outputs Can't logout
A is correct