In this post we will learn about one of very tricky java programming interview question where interviewer will give you code snippet and you will have to find the output of given code snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.kkjavatutorials.client; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; /** * @author KK JavaTutorials * What will be the output of this program? */ public class DateTimeInterviewQuestion_1 { public static void main(String[] args) { //Get instance of Calendar with Asia time zone Calendar instance = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta")); Date date1 = instance.getTime(); System.out.println("date1 = " + date1); //Now setting Europe time zone instance.setTimeZone(TimeZone.getTimeZone("Europe/London")); Date date2 = instance.getTime(); System.out.println("date2 = " + date2); } } |
Sample output of program:
date1 = Mon Aug 12 14:19:09 IST 2019
date2 = Mon Aug 12 14:19:09 IST 2019
Explanation of above output:
Output of both the System.out.println will be same date value because Date class object is always printed in local TimeZone and changing the TimeZone on Calendar class does not alter the underlying milliseconds value from the epoch time (Since January 1, 1970 00:00:00.000 GMT).
You may also like:
What are the standard ways for handling TimeZone in database transactions?
How to convert time from One Time Zone to another in Java?
Thanks for visiting this blog.
If you have any feedback or suggestion please feel free to drop in blow comment box.