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? 2. How to convert Date object to String? b) c) d) Answer: b 3. How to convert a String to a Date object? b) c) d) Answer: a 4. Is SimpleDateFormat thread safe? Answer: b 5. How to identify if a timezone is eligible for DayLight Saving? Answer: c 6. What is the replacement of joda time library in java 8? Answer: a 7. How is Date stored in database? Answer: a 8. What does LocalTime represent? Answer: b 9. How to get difference between two dates? Answer: a 10. How to get UTC time? Answer: c
a) SimpleDateFormat
b) DateFormat
c) SimpleFormat
d) DateConverter
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: " + );
a) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.parse(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.format(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().parse();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().format();
Clarification: SimpleDateFormat takes a string containing pattern. sdf.format converts the Date object to String.
a) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.parse(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf.format(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().parse();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date().format();
Clarification: SimpleDateFormat takes a string containing pattern. sdf.parse converts the String to Date object.
a) True
b) False
Clarification: SimpleDateFormat is not thread safe. In the multithreaded environment, we need to manage threads explicitly.
a) useDaylightTime() of Time class
b) useDaylightTime() of Date class
c) useDaylightTime() of TimeZone class
d) useDaylightTime() of DateTime class
Clarification: public abstract boolean useDaylightTime() is provided in TimeZone class.
a) java.time (JSR-310)
b) java.date (JSR-310)
c) java.joda
d) java.jodaTime
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.
a) java.sql.Date
b) java.util.Date
c) java.sql.DateTime
d) java.util.DateTime
Clarification: java.sql.Date is the datatype of Date stored in database.
a) Date without time
b) Time without Date
c) Date and Time
d) Date and Time with timezone
Clarification: LocalTime of joda library represents time without date.
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();
Clarification: Java 8 provides a method called between which provides Duration between two times.
a) Time.getUTC();
b) Date.getUTC();
c) Instant.now();
d) TimeZone.getUTC();
Clarification: In java 8, Instant.now() provides current time in UTC/GMT.