5 Reasons Why Java's Quondam Appointment Together With Calendar Api Was Bad
If you lot receive got been programming inward Java for a twosome of years as well as then you lot may know that how bad was Java's Date as well as Calendar API was, I hateful the java.util.Date and java.utill.Calendar was badly designed. Though Java has corrected it's error past times introducing a build new, shiny Date as well as Time API, non many Java developers are using it yet. In a fight to motivate you lot to job novel Date as well as Time API from Java 8, I am going to listing downwards a twosome of reasons why Java's Date as well as Calendar shape has been criticized so much inward the past. These points are too of import to know from the Interview indicate of stance because equally an experienced Java programmer, it is expected from you lot to know the shortcomings of existing API as well as how novel API solves those problems.
1) It's non intuitive
Look at the next example, hither a user only wants to practice a Date object for 25th Dec 2017, 8.30 at night, practice you lot think this is the right way to stand upward for that appointment inward Java?
Date appointment = novel Date(2017, 12, 25, 20, 30);
Well, fifty-fifty though it is looking alright, it is non correct. The inward a higher house code contains ii bugs, which is non at all intuitive. If you lot are using the java.util.Date shape as well as then you lot must know that twelvemonth starts from 1900 as well as calendar month starts from null i.e. Jan is the zeroth month, non the first.
Here is the right way to declare same appointment inward Java:
int twelvemonth = 2017 - 1900;
int calendar month = 12 - 1;
Date appointment = novel Date(year, month, 25, 20, 30);
You must recollect that Jan is 0, Dec is xi as well as Date job years from 1900.
2) Timezones
Prior to JDK 8, Java uses String to stand upward for TimeZone, a real bad idea. Ideally, they should receive got defined a constant or Enum instead of allowing the user to locomote past times String. Since String lacks compiler checks, it opens a door for spelling mistakes as well as empty-headed human errors. Java's naming convention alongside timezones too doesn't help. Look at the next example, hither I am trying to acquire TimeZone object for NewYork, doesn't it aspect all right?
TimeZone zone = TimeZone.getInstance("America/NewYork");
Unfortunately, it's non correct. It contains a empty-headed but difficult to uncovering a mistake, the time zone string is incorrect here. Correct timezone String is "America/New_York" equally shown inward the next example
TimeZone zone = TimeZone.getInstance("America/New_York");
Had they receive got used Enum, these would receive got been flagged past times the compiler or your IDE equally before long equally you lot typed. Another instance of this variety of gotcah is "Asia/Hong_Kong", so last careful spell using those timezones inward Java.
3) Calendars
The Calendar shape is too non real user-friendly or intuitive inward Java. You precisely cannot apply mutual sense or predictive knowledge. For instance inward the next code, nosotros are trying to practice a Calendar object for a appointment inward a special timezone, It volition non present whatsoever compile fourth dimension error but it volition non piece of work equally you lot expect.
Calendar cal = novel GregorianCalendar(date, zone);
Right way of using Calandar object alongside appointment as well as timezone is equally next :
Calendar cal = novel GregorianCalendar(zone);
cal.setTime(date);
So, if you lot haven't used Calendar API from a long time, in that place is no conduct chances you lot tin acquire it right afterwards that.
Here is some other instance of how intuitive novel Date as well as Time API has equally compared to quondam Date as well as Calendar API:
If you lot are interested on to a greater extent than examples of quondam as well as novel Date as well as Time API similar this, I propose exploring a skilful Java 8 mass e.g. Java SE 8 for Impatient past times Cay S. Horstmann. One of my favorite author, who explains the details which affair most.
4) Formatting Dates
Formatting dates are 1 of the virtually mutual tasks spell using Dates inward Java. Given multi-threading is the nub forcefulness of Java, 1 should aspect that their nub classes e.g. String, Integer or Date should piece of work seamlessly inward concurrent applications. Java designers made a skilful chore alongside String as well as Integer but did a actually pitiable chore alongside Date, formatting appointment was real tricky inward Java. One of the virtually useful utility to format dates, SimpleDateFormat was non thread-safe.
DateFormat fm = novel SimpleDateFormat("HH:mm Z");
String str = fm.format(cal);
This instance contains a twosome of bugs, first, you lot receive got to gear upward Timezone for DateFormat shape as well as second, you lot cannot locomote past times a Calendar object to format method, you lot tin alone locomote past times a java.util.Date object, thence you lot demand to start convert Calendar to Date inward Java as well as and then locomote past times it to the format() method equally shown below:
Here is the right way of using Calendar shape alongside SimpleDateFormat inward Java.
DateFormat fm = novel SimpleDateFormat("HH:mm Z");
fm.setTimeZone(zone);
Date calDate = cal.getTime();
String str = fm.format(calDate);
You must recollect that Calendar cannot last formatted inward Java as well as DateFormat shape is non thread-safe so it cannot last used inward multi-threaded Java application without proper synchronization. This result has been addressed inward Java 8 past times making DateFormatter immutable as well as thread-safe. If you lot desire to larn more, you lot tin too read Java 8 inward Action past times Roul-Gabrial Urma as well as team.
5) Mutable
This is IMHO biggest error Java designers has made inward the JDK. Unlike String or Integer, Date is mutable. It's possible for you lot to locomote past times a customer to acquire reference of Date as well as alter it without possessor of that Date shape knowing, equally shown inward below example
Persons p = novel Person();
Date birthDay = p.getBirthDay();
birthDay.setTime();
Now that soul volition receive got a unlike nascence date, so bad. That's why whenever you lot receive got to furnish Date from your class, you lot must furnish a novel Date object or clone, you lot should never furnish the master copy Date object, it breaks the Encapsulation of class.
If these reasons were non enough, the SQL Date/Time/Timestamp too extends the java.util.Date shape as well as violates the Liskov Substitution Principle, which agency if a method expects the java.util.Date object as well as then you lot cannot locomote past times the java.sql.Date, java.sql.Time, or java.sql.Timestamp class, fifty-fifty though they are a subclass of java.util.Date, thence you lot demand to convert util appointment to SQL appointment as well as vice-versa spell passing unopen to your application's DAO layer as well as Business layer.
That's all on why Java's Date, Calendar, as well as Time classes suck earlier Java 8. It's of import to larn from sense as well as that's what Java designers receive got done that. For you, a Java developer too its of import to know shortcomings of Java's quondam Date as well as Calendar API to appreciate as well as acclaim greatness of Java 8's novel Date as well as Time API.
If you lot desire to larn novel features of Java 8 as well as novel Date as well as Time API, I propose reading Java SE 8 for Impatient past times Cay S. Horstmann, 1 of the best books to larn Java 8 quickly.
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
1) It's non intuitive
Look at the next example, hither a user only wants to practice a Date object for 25th Dec 2017, 8.30 at night, practice you lot think this is the right way to stand upward for that appointment inward Java?
Date appointment = novel Date(2017, 12, 25, 20, 30);
Well, fifty-fifty though it is looking alright, it is non correct. The inward a higher house code contains ii bugs, which is non at all intuitive. If you lot are using the java.util.Date shape as well as then you lot must know that twelvemonth starts from 1900 as well as calendar month starts from null i.e. Jan is the zeroth month, non the first.
Here is the right way to declare same appointment inward Java:
int twelvemonth = 2017 - 1900;
int calendar month = 12 - 1;
Date appointment = novel Date(year, month, 25, 20, 30);
You must recollect that Jan is 0, Dec is xi as well as Date job years from 1900.
2) Timezones
Prior to JDK 8, Java uses String to stand upward for TimeZone, a real bad idea. Ideally, they should receive got defined a constant or Enum instead of allowing the user to locomote past times String. Since String lacks compiler checks, it opens a door for spelling mistakes as well as empty-headed human errors. Java's naming convention alongside timezones too doesn't help. Look at the next example, hither I am trying to acquire TimeZone object for NewYork, doesn't it aspect all right?
TimeZone zone = TimeZone.getInstance("America/NewYork");
Unfortunately, it's non correct. It contains a empty-headed but difficult to uncovering a mistake, the time zone string is incorrect here. Correct timezone String is "America/New_York" equally shown inward the next example
TimeZone zone = TimeZone.getInstance("America/New_York");
Had they receive got used Enum, these would receive got been flagged past times the compiler or your IDE equally before long equally you lot typed. Another instance of this variety of gotcah is "Asia/Hong_Kong", so last careful spell using those timezones inward Java.
3) Calendars
The Calendar shape is too non real user-friendly or intuitive inward Java. You precisely cannot apply mutual sense or predictive knowledge. For instance inward the next code, nosotros are trying to practice a Calendar object for a appointment inward a special timezone, It volition non present whatsoever compile fourth dimension error but it volition non piece of work equally you lot expect.
Calendar cal = novel GregorianCalendar(date, zone);
Right way of using Calandar object alongside appointment as well as timezone is equally next :
Calendar cal = novel GregorianCalendar(zone);
cal.setTime(date);
So, if you lot haven't used Calendar API from a long time, in that place is no conduct chances you lot tin acquire it right afterwards that.
Here is some other instance of how intuitive novel Date as well as Time API has equally compared to quondam Date as well as Calendar API:
If you lot are interested on to a greater extent than examples of quondam as well as novel Date as well as Time API similar this, I propose exploring a skilful Java 8 mass e.g. Java SE 8 for Impatient past times Cay S. Horstmann. One of my favorite author, who explains the details which affair most.
4) Formatting Dates
Formatting dates are 1 of the virtually mutual tasks spell using Dates inward Java. Given multi-threading is the nub forcefulness of Java, 1 should aspect that their nub classes e.g. String, Integer or Date should piece of work seamlessly inward concurrent applications. Java designers made a skilful chore alongside String as well as Integer but did a actually pitiable chore alongside Date, formatting appointment was real tricky inward Java. One of the virtually useful utility to format dates, SimpleDateFormat was non thread-safe.
DateFormat fm = novel SimpleDateFormat("HH:mm Z");
String str = fm.format(cal);
This instance contains a twosome of bugs, first, you lot receive got to gear upward Timezone for DateFormat shape as well as second, you lot cannot locomote past times a Calendar object to format method, you lot tin alone locomote past times a java.util.Date object, thence you lot demand to start convert Calendar to Date inward Java as well as and then locomote past times it to the format() method equally shown below:
Here is the right way of using Calendar shape alongside SimpleDateFormat inward Java.
DateFormat fm = novel SimpleDateFormat("HH:mm Z");
fm.setTimeZone(zone);
Date calDate = cal.getTime();
String str = fm.format(calDate);
You must recollect that Calendar cannot last formatted inward Java as well as DateFormat shape is non thread-safe so it cannot last used inward multi-threaded Java application without proper synchronization. This result has been addressed inward Java 8 past times making DateFormatter immutable as well as thread-safe. If you lot desire to larn more, you lot tin too read Java 8 inward Action past times Roul-Gabrial Urma as well as team.
5) Mutable
This is IMHO biggest error Java designers has made inward the JDK. Unlike String or Integer, Date is mutable. It's possible for you lot to locomote past times a customer to acquire reference of Date as well as alter it without possessor of that Date shape knowing, equally shown inward below example
Persons p = novel Person();
Date birthDay = p.getBirthDay();
birthDay.setTime();
Now that soul volition receive got a unlike nascence date, so bad. That's why whenever you lot receive got to furnish Date from your class, you lot must furnish a novel Date object or clone, you lot should never furnish the master copy Date object, it breaks the Encapsulation of class.
If these reasons were non enough, the SQL Date/Time/Timestamp too extends the java.util.Date shape as well as violates the Liskov Substitution Principle, which agency if a method expects the java.util.Date object as well as then you lot cannot locomote past times the java.sql.Date, java.sql.Time, or java.sql.Timestamp class, fifty-fifty though they are a subclass of java.util.Date, thence you lot demand to convert util appointment to SQL appointment as well as vice-versa spell passing unopen to your application's DAO layer as well as Business layer.
That's all on why Java's Date, Calendar, as well as Time classes suck earlier Java 8. It's of import to larn from sense as well as that's what Java designers receive got done that. For you, a Java developer too its of import to know shortcomings of Java's quondam Date as well as Calendar API to appreciate as well as acclaim greatness of Java 8's novel Date as well as Time API.
If you lot desire to larn novel features of Java 8 as well as novel Date as well as Time API, I propose reading Java SE 8 for Impatient past times Cay S. Horstmann, 1 of the best books to larn Java 8 quickly.
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!


Komentar
Posting Komentar