250+ TOP MCQs on Data Type-Date, TimeZone and Answers

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

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

Answer: a
Clarification: 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());

b)

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

c)

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

d)

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

Answer: b
Clarification: 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());

b)

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

c)

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

d)

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

Answer: a
Clarification: SimpleDateFormat takes a string containing pattern. sdf.parse converts the String to Date object.

 
 

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

Answer: b
Clarification: 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

Answer: c
Clarification: 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

Answer: a
Clarification: 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

Answer: a
Clarification: 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

Answer: b
Clarification: 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();

Answer: a
Clarification: 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();

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

Leave a Reply

Your email address will not be published. Required fields are marked *