The Date together with Time API is a really of import 1 for solar daytime to solar daytime Java evolution locomote but many interviewers don't inquire plenty questions on this topic. Most of the questions are based on either Java Collection framework like HashMap, ConcurrentHashMap, or ArrayList or on multi-threading concepts similar volatile, synchronized together with atomic variables. This is non skillful together with I mean value many interviewers bring realized that a skillful agreement of appointment together with fourth dimension API is also of import for a skillful Java developer. That's the mind argue for an increasing number of date, fourth dimension together with calendar based Java interview questions inwards recent time, peculiarly inwards concluding a twain of years.
Since Java evolution task marketplace is really skillful at this 2d together with at that spot are a lot of jobs open, peculiarly inwards investment banking domain inwards the Asia Pacific, its likely the best fourth dimension to alter your job, especially if y'all are stuck alongside no learning, no salary growth together with non a pregnant bonus or incentives inwards concluding a twain of years.
Apart from stability, growth is really of import because if y'all don't movement frontward y'all volition hold out stuck together with eventually movement backward, thence changing task is every bit of import every bit sticking to a task which is working for you.
Btw, if y'all are novel to Java together with don't know much nearly Date together with Time classes together with so I advise y'all to showtime become through a comprehensive course of teaching like The Complete Java Masterclass on Udemy. It's also late updated to encompass the latest Java version together with non really expensive. You tin acquire it inwards precisely $10 inwards several Udemy flash sales which happens all approximately the year.
1. Does SimpleDateFormat hold out prophylactic to usage inwards the multithreaded program? (answer)
No, unfortunately, DateFormat together with all its implementations including SimpleDateFormat is non thread-safe, hence, should non hold out used inwards the multi-threaded plan until external thread-safety measures are applied e.g. confining SimpleDateFormat object into a ThreadLocal variable. I
f y'all don't exercise that, y'all volition acquire an wrong final result piece parsing or formatting dates inwards Java. Though, for all practical appointment fourth dimension purpose, I highly recommend the joda-time library.
2. How exercise y'all format a appointment inwards Java? e.g. inwards the ddMMyyyy format? (answer)
You tin either usage SimpleDateFormat shape or joda-time library to format the appointment inwards Java. DateFormat shape allows y'all to format appointment on many pop formats. Please run across the reply for code samples to format the Date into dissimilar formats e.g. dd-MM-yyyy or ddMMyyyy.
3. Can y'all say some deviation betwixt one-time together with novel Date Time API of Java 8?
Even though novel Date together with Time API are a completely novel API, y'all tin deduce the next deviation betwixt them:
1) In one-time API, Date was mutable but inwards Java 8 all appointment together with fourth dimension classes e.g. LocalDate, LocalTime or LocalDateTime are Immutable.
2) In one-time API, SimpleDateFormat was non thread-safe, but inwards Java 8 Formatter are thread-safe.
3) In one-time Date together with Calendar API, Year starts alongside 1900, Months starts alongside 0, which is corrected inwards Java 8, hither numbers brand to a greater extent than sense.
4) Old Date together with Calendar API has precisely 1 shape Date to correspond appointment together with time, but Java 8 has separated classes for Date together with Time e.g. LocalDate together with LocalTime
4. Which appointment the next Date illustration represent?
Date aDate = novel Date(2015, 12, 25, 20, 40);
This is a actually tricky query for whatever casual Java developers who bring non written appointment together with time-based code for himself because this volition non correspond the Christmas solar daytime or 25th Dec 2015. This code has got ii bugs, first, the yr starts from 1900 to, to correspond 2015, y'all demand to transcend 115 i.e. 2015-1900.
The 2d number alongside this code is that calendar month is non correct. The calendar month starts from 0, so Dec would xi non 12, interesting right? That's why y'all should novel Date together with Time API from Java 8, which solves all these issues. Again, y'all tin check The Complete Java Masterclass to acquire to a greater extent than nearly novel Date together with Time API.
5. How exercise y'all re-create a Date inwards Java? (answer)
It's a uncomplicated query to answer. The Date shape implements the clone() method, so precisely telephone telephone date.clone() to exercise a re-create of the Date object inwards Java.
6. What is the human relationship betwixt java.sql.Date together with java.util.Date? (answer)
This is 1 of the really interesting questions every bit non many developers know this fine but obvious detail. The java.sql.Date extends java.util.Date together with suppress all time-related methods to deed every bit precisely a appointment class. This is inwards straight violation of the Liskov commutation principle.
7. Can y'all format Calendar inwards Java? (answer)
No, Calendar cannot hold out formatted inwards Java. You tin solely format dates.
8. How exercise y'all convert a Calendar to Date together with vice-versa? (answer)
Well, y'all tin usage the Calendar.setTime() together with Calendar.getTime() method to convert the Calendar to Date together with vice-versa.
9. What is the equivalent of Date shape inwards Java 8? (answer)
The Instance shape is the equivalent to java.util.Date inwards Java 8 because it also represents a millisecond value or an illustration inwards the timescale. Java 8 has also added conversion method e.g. toInstant() together with fromDate() to convert illustration to java.util.Date together with vice-versa. See Java SE 8 for Really Impatient By Cay S. Horstmann for to a greater extent than details.
10. How exercise y'all convert a millisecond to Date inwards Java? (answer)
One of the simplest query to answer, y'all tin telephone telephone the getTime() method which returns the millisecond from the Epoch.
12. How exercise y'all acquire a calendar month together with yr from a Date object inwards Java? (answer)
You tin convert a Date to Calendar together with and so usage its get() method alongside diverse fields to acquire the month, year, solar daytime of the calendar week together with other appointment particulars e.g.
int solar daytime = calendar.get(Calendar.DATE); // returns the day
int calendar month = calendar.get(Calendar.MONTH) + 1; // calendar month starts from 0
int yr = calendar.get(Calendar.YEAR) + 1900; // yr starts from 1900
13. How exercise y'all convert XMLGregorianCalendar to Date inwards Java? (answer)
I bring discussed this employment before, delight banking concern jibe the link for the detailed answer.
14. How exercise y'all acquire the electrical flow fourth dimension inwards GMT inwards Java? (answer)
The Java appointment is independent of fourth dimension together with display fourth dimension inwards a electrical flow fourth dimension zone solely if y'all impress them. So, inwards gild to convert the electrical flow fourth dimension inwards GMT, y'all demand to usage Calendar alongside fourth dimension zone every bit shown inwards the given answer.
15. Is Date shape hold out Immutable inwards Java?
No, Date is non immutable. You tin alter it's internal to correspond a dissimilar date. This is the argue that when y'all bring a fellow member correspond Date, y'all must hold out careful non to leak its reference exterior using getDate() method, because together with so the customer tin modify the dry soil of the Immutable object, instead a re-create should hold out returned to the customer every bit suggested in Effective Java book yesteryear none other than Joshua Bloch, writer of several primal classes of JDK API.
16. How exercise y'all add together together with subtract a day, calendar month together with yr inwards a Date object? (answer)
This is 1 of the mutual solar daytime to solar daytime task of a Java developer, y'all tin usage the code shared inwards the reply for adding or subtracting a solar daytime or calendar month from the Date object.
17. How exercise y'all compare ii dates inwards Java to banking concern jibe if they are equal? (answer)
This is 1 of the easier questions y'all volition run across inwards damage of science checking on Date classes. The java.util.Date shape implements the equals() method, which returns truthful if both Dates is same together with imitation otherwise.
18. How exercise y'all uncovering if 1 appointment comes earlier or afterward some other appointment inwards Java? (answer)
This is the follow-up query of the previous question. The interviewer farther checks the candidate's appointment comparing science yesteryear bespeak this question. The reply is simple, the java.util.Date shape provides before() together with after() method which returns truthful together with imitation if the appointment on which method is called comes earlier or afterward the other appointment passed every bit a method argument.
19. How exercise y'all convert java.util.Date to java.sql.Date inwards Java JDBC? (answer)
It's the follow-up query of sixth question. Since java.sql.Date is a subclass of java.util.Date y'all tin only acquire the fourth dimension inwards millisecond together with passed it to SQL appointment every bit shown below:
Date similar a shot = novel Date();
java.sql.Date sqlDate = novel java.sql.Date(now.getTime());
Simple together with tardily right? but at that spot are some interesting follow-up questions when y'all write this code, I advise y'all banking concern jibe the reply link for to a greater extent than detailed discussion, where I bring answered those follow-up questions every bit well. You tin farther see
Since Java evolution task marketplace is really skillful at this 2d together with at that spot are a lot of jobs open, peculiarly inwards investment banking domain inwards the Asia Pacific, its likely the best fourth dimension to alter your job, especially if y'all are stuck alongside no learning, no salary growth together with non a pregnant bonus or incentives inwards concluding a twain of years.
Apart from stability, growth is really of import because if y'all don't movement frontward y'all volition hold out stuck together with eventually movement backward, thence changing task is every bit of import every bit sticking to a task which is working for you.
Btw, if y'all are novel to Java together with don't know much nearly Date together with Time classes together with so I advise y'all to showtime become through a comprehensive course of teaching like The Complete Java Masterclass on Udemy. It's also late updated to encompass the latest Java version together with non really expensive. You tin acquire it inwards precisely $10 inwards several Udemy flash sales which happens all approximately the year.
Top 10 Date, Time together with Calendar Interview Questions together with Answers
I bring been sharing interview questions on diverse Java topics from concluding a twain of years similar multithreading, collections, design patterns, coding problems, etc, but 1 topic which I haven't touched yet was appointment together with fourth dimension together with that's why I idea to portion some of the interesting appointment together with time-based questions from recent Java interviews. In this article, y'all volition uncovering those.1. Does SimpleDateFormat hold out prophylactic to usage inwards the multithreaded program? (answer)
No, unfortunately, DateFormat together with all its implementations including SimpleDateFormat is non thread-safe, hence, should non hold out used inwards the multi-threaded plan until external thread-safety measures are applied e.g. confining SimpleDateFormat object into a ThreadLocal variable. I
f y'all don't exercise that, y'all volition acquire an wrong final result piece parsing or formatting dates inwards Java. Though, for all practical appointment fourth dimension purpose, I highly recommend the joda-time library.
2. How exercise y'all format a appointment inwards Java? e.g. inwards the ddMMyyyy format? (answer)
You tin either usage SimpleDateFormat shape or joda-time library to format the appointment inwards Java. DateFormat shape allows y'all to format appointment on many pop formats. Please run across the reply for code samples to format the Date into dissimilar formats e.g. dd-MM-yyyy or ddMMyyyy.
3. Can y'all say some deviation betwixt one-time together with novel Date Time API of Java 8?
Even though novel Date together with Time API are a completely novel API, y'all tin deduce the next deviation betwixt them:
1) In one-time API, Date was mutable but inwards Java 8 all appointment together with fourth dimension classes e.g. LocalDate, LocalTime or LocalDateTime are Immutable.
2) In one-time API, SimpleDateFormat was non thread-safe, but inwards Java 8 Formatter are thread-safe.
3) In one-time Date together with Calendar API, Year starts alongside 1900, Months starts alongside 0, which is corrected inwards Java 8, hither numbers brand to a greater extent than sense.
4) Old Date together with Calendar API has precisely 1 shape Date to correspond appointment together with time, but Java 8 has separated classes for Date together with Time e.g. LocalDate together with LocalTime
4. Which appointment the next Date illustration represent?
Date aDate = novel Date(2015, 12, 25, 20, 40);
This is a actually tricky query for whatever casual Java developers who bring non written appointment together with time-based code for himself because this volition non correspond the Christmas solar daytime or 25th Dec 2015. This code has got ii bugs, first, the yr starts from 1900 to, to correspond 2015, y'all demand to transcend 115 i.e. 2015-1900.
The 2d number alongside this code is that calendar month is non correct. The calendar month starts from 0, so Dec would xi non 12, interesting right? That's why y'all should novel Date together with Time API from Java 8, which solves all these issues. Again, y'all tin check The Complete Java Masterclass to acquire to a greater extent than nearly novel Date together with Time API.
5. How exercise y'all re-create a Date inwards Java? (answer)
It's a uncomplicated query to answer. The Date shape implements the clone() method, so precisely telephone telephone date.clone() to exercise a re-create of the Date object inwards Java.
6. What is the human relationship betwixt java.sql.Date together with java.util.Date? (answer)
This is 1 of the really interesting questions every bit non many developers know this fine but obvious detail. The java.sql.Date extends java.util.Date together with suppress all time-related methods to deed every bit precisely a appointment class. This is inwards straight violation of the Liskov commutation principle.
7. Can y'all format Calendar inwards Java? (answer)
No, Calendar cannot hold out formatted inwards Java. You tin solely format dates.
8. How exercise y'all convert a Calendar to Date together with vice-versa? (answer)
Well, y'all tin usage the Calendar.setTime() together with Calendar.getTime() method to convert the Calendar to Date together with vice-versa.
9. What is the equivalent of Date shape inwards Java 8? (answer)
The Instance shape is the equivalent to java.util.Date inwards Java 8 because it also represents a millisecond value or an illustration inwards the timescale. Java 8 has also added conversion method e.g. toInstant() together with fromDate() to convert illustration to java.util.Date together with vice-versa. See Java SE 8 for Really Impatient By Cay S. Horstmann for to a greater extent than details.
10. How exercise y'all convert a millisecond to Date inwards Java? (answer)
One of the simplest query to answer, y'all tin telephone telephone the getTime() method which returns the millisecond from the Epoch.
12. How exercise y'all acquire a calendar month together with yr from a Date object inwards Java? (answer)
You tin convert a Date to Calendar together with and so usage its get() method alongside diverse fields to acquire the month, year, solar daytime of the calendar week together with other appointment particulars e.g.
int solar daytime = calendar.get(Calendar.DATE); // returns the day
int calendar month = calendar.get(Calendar.MONTH) + 1; // calendar month starts from 0
int yr = calendar.get(Calendar.YEAR) + 1900; // yr starts from 1900
13. How exercise y'all convert XMLGregorianCalendar to Date inwards Java? (answer)
I bring discussed this employment before, delight banking concern jibe the link for the detailed answer.
14. How exercise y'all acquire the electrical flow fourth dimension inwards GMT inwards Java? (answer)
The Java appointment is independent of fourth dimension together with display fourth dimension inwards a electrical flow fourth dimension zone solely if y'all impress them. So, inwards gild to convert the electrical flow fourth dimension inwards GMT, y'all demand to usage Calendar alongside fourth dimension zone every bit shown inwards the given answer.
15. Is Date shape hold out Immutable inwards Java?
No, Date is non immutable. You tin alter it's internal to correspond a dissimilar date. This is the argue that when y'all bring a fellow member correspond Date, y'all must hold out careful non to leak its reference exterior using getDate() method, because together with so the customer tin modify the dry soil of the Immutable object, instead a re-create should hold out returned to the customer every bit suggested in Effective Java book yesteryear none other than Joshua Bloch, writer of several primal classes of JDK API.
16. How exercise y'all add together together with subtract a day, calendar month together with yr inwards a Date object? (answer)
This is 1 of the mutual solar daytime to solar daytime task of a Java developer, y'all tin usage the code shared inwards the reply for adding or subtracting a solar daytime or calendar month from the Date object.
17. How exercise y'all compare ii dates inwards Java to banking concern jibe if they are equal? (answer)
This is 1 of the easier questions y'all volition run across inwards damage of science checking on Date classes. The java.util.Date shape implements the equals() method, which returns truthful if both Dates is same together with imitation otherwise.
18. How exercise y'all uncovering if 1 appointment comes earlier or afterward some other appointment inwards Java? (answer)
This is the follow-up query of the previous question. The interviewer farther checks the candidate's appointment comparing science yesteryear bespeak this question. The reply is simple, the java.util.Date shape provides before() together with after() method which returns truthful together with imitation if the appointment on which method is called comes earlier or afterward the other appointment passed every bit a method argument.
19. How exercise y'all convert java.util.Date to java.sql.Date inwards Java JDBC? (answer)
It's the follow-up query of sixth question. Since java.sql.Date is a subclass of java.util.Date y'all tin only acquire the fourth dimension inwards millisecond together with passed it to SQL appointment every bit shown below:
Date similar a shot = novel Date();
java.sql.Date sqlDate = novel java.sql.Date(now.getTime());
Simple together with tardily right? but at that spot are some interesting follow-up questions when y'all write this code, I advise y'all banking concern jibe the reply link for to a greater extent than detailed discussion, where I bring answered those follow-up questions every bit well. You tin farther see
Btw, y'all would demand a answer)
This is similar to 1 of the previous questions where nosotros demand to acquire the electrical flow fourth dimension inwards GMT
21. How exercise y'all exhibit timezone inwards formatted appointment inwards Java? (answer)
run across the reply link for a code example
22. What is the deviation betwixt java.util.Date together with java.sql.Date inwards Java? (answer)
There are much deviation betwixt them but the most of import 1 is that java.sql.Date is from JDBC packet together with it acts every bit converted betwixt Date together with Time information types inwards database together with Date inwards Java.
23. How exercise y'all calculate the deviation betwixt ii dates inwards Java? (program)
There are multiple ways to calculate the deviation betwixt ii dates inwards Java e.g. y'all tin acquire the long millisecond values from ii dates together with acquire the deviation but that's non really valuable, y'all by together with large demand the deviation inwards days or hours, etc. See the plan to acquire to a greater extent than nearly those differences.
24 How exercise y'all convert a String(YYYYMMDD) to appointment inwards Java? (answer)
You tin usage SimpleDateFormat prior to JDK 8 together with DateTimeFormatter along alongside format together with parse method to convert a String to appointment inwards Java.
That's all nearly the date, time, together with Calendar based Java Interview Questions. It's really of import for a Java developer to bring a company agreement of Date together with Time API of Java, both one-time together with novel because y'all volition non uncovering Java 8 inwards every projection y'all work. You even so demand to know how to locomote alongside one-time Date together with Calendar API piece maintaining the legacy projection for a twain of to a greater extent than years.
Further Learning
Complete Java Masterclass
see here)
P. S. - If y'all are looking to fill upwards gaps inwards your Java noesis or desire to acquire Java from scratch, hither are some useful free online Java courses to await out.
No comments:
Post a Comment