public class TechSol { public static void main(String[] args) { Date startUtilDate = new Date(); // Assume that the obtained date is Thu Nov 03 20:40:45 IST 2016 Date tempUtilDate = startUtilDate; startUtilDate.setDate(26); System.out.println(tempUtilDate); LocalDate startLocalDate = LocalDate.of(2016, Month.JUNE, 01); LocalDate tempLocalDate = startLocalDate; startLocalDate = startLocalDate.plusDays(10); System.out.println(tempLocalDate); }}What will get consoled out during execution?Sat Nov 26 20:40:45 IST 2016 2016-06-01Sat Nov 03 20:40:45 IST 2016 2016-06-01Sat Nov 26 20:40:45 IST 2016 2016-06-10Sat Nov 03 20:40:45 IST 2016 2016-06-10Submit
Question
public class TechSol { public static void main(String[] args) { Date startUtilDate = new Date(); // Assume that the obtained date is Thu Nov 03 20:40:45 IST 2016 Date tempUtilDate = startUtilDate; startUtilDate.setDate(26); System.out.println(tempUtilDate); LocalDate startLocalDate = LocalDate.of(2016, Month.JUNE, 01); LocalDate tempLocalDate = startLocalDate; startLocalDate = startLocalDate.plusDays(10); System.out.println(tempLocalDate); }}What will get consoled out during execution?Sat Nov 26 20:40:45 IST 2016 2016-06-01Sat Nov 03 20:40:45 IST 2016 2016-06-01Sat Nov 26 20:40:45 IST 2016 2016-06-10Sat Nov 03 20:40:45 IST 2016 2016-06-10Submit
Solution
The output of the execution will be:
Sat Nov 26 20:40:45 IST 2016 2016-06-01
Explanation:
In the first part of the code, we are dealing with java.util.Date. When we create a new Date object, it is initialized with the current date and time. Then we create a reference (tempUtilDate) to the same Date object. When we change the date of the original object (startUtilDate), the change is reflected when we print tempUtilDate because they are pointing to the same object.
In the second part of the code, we are dealing with java.time.LocalDate which is immutable. When we create a new LocalDate object (startLocalDate), it is initialized with the date we provided. Then we create a reference (tempLocalDate) to the same LocalDate object. When we add 10 days to the original object (startLocalDate), it does not affect tempLocalDate because LocalDate is immutable and any changes will result in a new object. Therefore, tempLocalDate still has the original date when we print it.
Similar Questions
What will be the output when the following code is executed?public class Product {public static void main(String[] args) { LocalDate startLocalDate = LocalDate.of(2016, Month.JUNE, 01);LocalDate tempLocalDate = startLocalDate;startLocalDate.plusDays(10);System.out.println(tempLocalDate);}} 2016-06-0101-06-20162016-06-1111-06-2016
Consider the following programimport java.time.*;public class TestParseMethod{ public static void main(String[] args){ String dateString = "2/4/2019"; LocalDate myDate = LocalDate.parse(dateString); System.out.println("You entered " + myDate);}}What will be an output of this program ?Group of answer choicesYou entered 2 Apr 2019It will not compileYou entered 2/4/2019It will create exception
public class DateTimeSample { public static void main(String[] args) { Period period = Period.of(1, 5, 2001); System.out.println(period.getDays() + "\t" + period.getMonths()); }}What is expected to be consoled out? (Choose any one option)1 55 12001 55 2001
Write a JAVA program in which take date(DDMMYYYY) from user and displaynext day date(DDMMYYYY) as output.Example:Input: date=09, month=-06, year=1992Output: date=10, month=-06, year=1992Note:-1. Consider condition for leap year2. Consider number of days in month of February based on leap year ( if leapyear then February days =29, else days = 28 ) )3. Consider number of days either 30 or 31 based on month entered by user
Which of the following will print only the date without time?*System.out.println(new Date());System.out.println(new LocalTime());System.out.println(LocalDate.today());System.out.println(LocalDate.now());
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.