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
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());
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();View Answer
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());
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();View Answer
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
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
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
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
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
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
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
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.
- Practice Information Technology MCQs
- Practice Programming MCQs
- Practice BCA MCQs
- Check Java Books
- Check Programming Books