Java Questions & Answers – Data Type-Date, TimeZone

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Data Type – Date and TimeZone”.

1. How to format date from one form to another?
a) SimpleDateFormat
b) DateFormat
c) SimpleFormat
d) DateConverter
View Answer

Answer: a
Explanation: SimpleDateFormat can be used as

Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-mm-dd'T'hh:MM:ss");
String nowStr = sdf.format(now);
System.out.println("Current Date: " + );

2. How to convert Date object to String?
a)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.parse(new Date());
advertisement
advertisement

b)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.format(new Date());

c)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().parse();
Note: Join free Sanfoundry classes at Telegram or Youtube

d)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().format();
View Answer
Answer: b
Explanation: SimpleDateFormat takes a string containing pattern. sdf.format converts the Date object to String.
 
 

3. How to convert a String to a Date object?
a)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.parse(new Date());
advertisement

b)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.format(new Date());

c)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().parse();
advertisement

d)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().format();
View Answer
Answer: a
Explanation: SimpleDateFormat takes a string containing pattern. sdf.parse converts the String to Date object.
 
 

4. Is SimpleDateFormat thread safe?
a) True
b) False
View Answer

Answer: b
Explanation: SimpleDateFormat is not thread safe. In the multithreaded environment, we need to manage threads explicitly.

5. How to identify if a timezone is eligible for DayLight Saving?
a) useDaylightTime() of Time class
b) useDaylightTime() of Date class
c) useDaylightTime() of TimeZone class
d) useDaylightTime() of DateTime class
View Answer

Answer: c
Explanation: public abstract boolean useDaylightTime() is provided in TimeZone class.

6. What is the replacement of joda time library in java 8?
a) java.time (JSR-310)
b) java.date (JSR-310)
c) java.joda
d) java.jodaTime
View Answer

Answer: a
Explanation: In java 8, we are asked to migrate to java.time (JSR-310) which is a core part of the JDK which replaces joda library project.

7. How is Date stored in database?
a) java.sql.Date
b) java.util.Date
c) java.sql.DateTime
d) java.util.DateTime
View Answer

Answer: a
Explanation: java.sql.Date is the datatype of Date stored in database.

8. What does LocalTime represent?
a) Date without time
b) Time without Date
c) Date and Time
d) Date and Time with timezone
View Answer

Answer: b
Explanation: LocalTime of joda library represents time without date.

9. How to get difference between two dates?
a) long diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();
b) long diffInMilli = java.time.difference(dateTime1, dateTime2).toMillis();
c) Date diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();
d) Time diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();
View Answer

Answer: a
Explanation: Java 8 provides a method called between which provides Duration between two times.

10. How to get UTC time?
a) Time.getUTC();
b) Date.getUTC();
c) Instant.now();
d) TimeZone.getUTC();
View Answer

Answer: c
Explanation: In java 8, Instant.now() provides current time in UTC/GMT.

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.