5 Reasons Why Java's Former Appointment As Well As Calendar Api Was Bad
Monday, December 10, 2018
Add Comment
If y'all direct keep been programming inward Java for a duad of years so y'all may know that how bad was Java's Date in addition to Calendar API was, I hateful the java.util.Date and java.utill.Calendar was badly designed. Though Java has corrected it's error yesteryear introducing a construct new, shiny Date in addition to Time API, non many Java developers are using it yet. In a chip to motivate y'all to exercise novel Date in addition to Time API from Java 8, I am going to listing downward a duad of reasons why Java's Date in addition to Calendar bird has been criticized so much inward the past. These points are likewise of import to know from the Interview signal of sentiment because every bit an experienced Java programmer, it is expected from y'all to know the shortcomings of existing API in addition to how novel API solves those problems.
1) It's non intuitive
Look at the next example, hither a user only wants to practise a Date object for 25th Dec 2017, 8.30 at night, practise y'all think this is the right way to stand upwards for that engagement inward Java?
Date engagement = novel Date(2017, 12, 25, 20, 30);
Well, fifty-fifty though it is looking alright, it is non correct. The higher upwards code contains 2 bugs, which is non at all intuitive. If y'all are using the java.util.Date bird so y'all must know that yr starts from 1900 in addition to calendar month starts from null i.e. Jan is the zeroth month, non the first.
Here is the right way to declare same engagement inward Java:
int yr = 2017 - 1900;
int calendar month = 12 - 1;
Date engagement = novel Date(year, month, 25, 20, 30);
You must recall that Jan is 0, Dec is xi in addition to Date exercise years from 1900.
2) Timezones
Prior to JDK 8, Java uses String to stand upwards for TimeZone, a really bad idea. Ideally, they should direct keep defined a constant or Enum instead of allowing the user to exceed String. Since String lacks compiler checks, it opens a door for spelling mistakes in addition to featherbrained human errors. Java's naming convention alongside timezones likewise 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 featherbrained but difficult to discovery a mistake, the time zone string is incorrect here. Correct timezone String is "America/New_York" every bit shown inward the next example
TimeZone zone = TimeZone.getInstance("America/New_York");
Had they direct keep used Enum, these would direct keep been flagged yesteryear the compiler or your IDE every bit presently every bit y'all typed. Another instance of this form of gotcah is "Asia/Hong_Kong", so hold out careful spell using those timezones inward Java.
3) Calendars
The Calendar bird is likewise non really 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 practise a Calendar object for a engagement inward a especial timezone, It volition non demo whatever compile fourth dimension error but it volition non piece of job every bit y'all expect.
Calendar cal = novel GregorianCalendar(date, zone);
Right way of using Calandar object alongside engagement in addition to timezone is every bit next :
Calendar cal = novel GregorianCalendar(zone);
cal.setTime(date);
So, if y'all haven't used Calendar API from a long time, at that topographic point is no peril y'all tin mail away acquire it right subsequently that.
Here is some other instance of how intuitive novel Date in addition to Time API has every bit compared to sometime Date in addition to Calendar API:
If y'all are interested on to a greater extent than examples of sometime in addition to novel Date in addition to Time API similar this, I propose exploring a expert Java 8 mass e.g. Java SE 8 for Impatient yesteryear Cay S. Horstmann. One of my favorite author, who explains the details which affair most.
4) Formatting Dates
Formatting dates are 1 of the nigh mutual tasks spell using Dates inward Java. Given multi-threading is the marrow pull of Java, 1 should hold back that their marrow classes e.g. String, Integer or Date should piece of job seamlessly inward concurrent applications. Java designers made a expert undertaking alongside String in addition to Integer but did a actually pathetic undertaking alongside Date, formatting engagement was really tricky inward Java. One of the nigh 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 duad of bugs, first, y'all direct keep to ready Timezone for DateFormat bird in addition to second, y'all cannot exceed a Calendar object to format method, y'all tin mail away alone exceed a java.util.Date object, thence y'all demand to starting fourth dimension convert Calendar to Date inward Java in addition to so exceed it to the format() method every bit shown below:
Here is the right way of using Calendar bird 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 recall that Calendar cannot hold out formatted inward Java in addition to DateFormat bird is non thread-safe so it cannot hold out used inward multi-threaded Java application without proper synchronization. This effect has been addressed inward Java 8 yesteryear making DateFormatter immutable in addition to thread-safe. If y'all desire to acquire more, y'all tin mail away likewise read Java 8 inward Action yesteryear Roul-Gabrial Urma in addition to 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 y'all to exceed a customer to acquire reference of Date in addition to modify it without possessor of that Date bird knowing, every bit shown inward below example
Persons p = novel Person();
Date birthDay = p.getBirthDay();
birthDay.setTime();
Now that soul volition direct keep a dissimilar nascency date, so bad. That's why whenever y'all direct keep to furnish Date from your class, y'all must furnish a novel Date object or clone, y'all should never furnish the master Date object, it breaks the Encapsulation of class.
If these reasons were non enough, the SQL Date/Time/Timestamp likewise extends the java.util.Date bird in addition to violates the Liskov Substitution Principle, which way if a method expects the java.util.Date object so y'all cannot exceed the java.sql.Date, java.sql.Time, or java.sql.Timestamp class, fifty-fifty though they are a subclass of java.util.Date, thence y'all demand to convert util engagement to SQL engagement in addition to vice-versa spell passing roughly your application's DAO layer in addition to Business layer.
That's all on why Java's Date, Calendar, in addition to Time classes suck earlier Java 8. It's of import to acquire from sense in addition to that's what Java designers direct keep done that. For you, a Java developer likewise its of import to know shortcomings of Java's sometime Date in addition to Calendar API to appreciate in addition to acclaim greatness of Java 8's novel Date in addition to Time API.
If y'all desire to acquire novel features of Java 8 in addition to novel Date in addition to Time API, I propose reading Java SE 8 for Impatient yesteryear Cay S. Horstmann, 1 of the best books to acquire 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 practise a Date object for 25th Dec 2017, 8.30 at night, practise y'all think this is the right way to stand upwards for that engagement inward Java?
Date engagement = novel Date(2017, 12, 25, 20, 30);
Well, fifty-fifty though it is looking alright, it is non correct. The higher upwards code contains 2 bugs, which is non at all intuitive. If y'all are using the java.util.Date bird so y'all must know that yr starts from 1900 in addition to calendar month starts from null i.e. Jan is the zeroth month, non the first.
Here is the right way to declare same engagement inward Java:
int yr = 2017 - 1900;
int calendar month = 12 - 1;
Date engagement = novel Date(year, month, 25, 20, 30);
You must recall that Jan is 0, Dec is xi in addition to Date exercise years from 1900.
2) Timezones
Prior to JDK 8, Java uses String to stand upwards for TimeZone, a really bad idea. Ideally, they should direct keep defined a constant or Enum instead of allowing the user to exceed String. Since String lacks compiler checks, it opens a door for spelling mistakes in addition to featherbrained human errors. Java's naming convention alongside timezones likewise 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 featherbrained but difficult to discovery a mistake, the time zone string is incorrect here. Correct timezone String is "America/New_York" every bit shown inward the next example
TimeZone zone = TimeZone.getInstance("America/New_York");
Had they direct keep used Enum, these would direct keep been flagged yesteryear the compiler or your IDE every bit presently every bit y'all typed. Another instance of this form of gotcah is "Asia/Hong_Kong", so hold out careful spell using those timezones inward Java.
3) Calendars
The Calendar bird is likewise non really 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 practise a Calendar object for a engagement inward a especial timezone, It volition non demo whatever compile fourth dimension error but it volition non piece of job every bit y'all expect.
Calendar cal = novel GregorianCalendar(date, zone);
Right way of using Calandar object alongside engagement in addition to timezone is every bit next :
Calendar cal = novel GregorianCalendar(zone);
cal.setTime(date);
So, if y'all haven't used Calendar API from a long time, at that topographic point is no peril y'all tin mail away acquire it right subsequently that.
Here is some other instance of how intuitive novel Date in addition to Time API has every bit compared to sometime Date in addition to Calendar API:
If y'all are interested on to a greater extent than examples of sometime in addition to novel Date in addition to Time API similar this, I propose exploring a expert Java 8 mass e.g. Java SE 8 for Impatient yesteryear Cay S. Horstmann. One of my favorite author, who explains the details which affair most.
4) Formatting Dates
Formatting dates are 1 of the nigh mutual tasks spell using Dates inward Java. Given multi-threading is the marrow pull of Java, 1 should hold back that their marrow classes e.g. String, Integer or Date should piece of job seamlessly inward concurrent applications. Java designers made a expert undertaking alongside String in addition to Integer but did a actually pathetic undertaking alongside Date, formatting engagement was really tricky inward Java. One of the nigh 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 duad of bugs, first, y'all direct keep to ready Timezone for DateFormat bird in addition to second, y'all cannot exceed a Calendar object to format method, y'all tin mail away alone exceed a java.util.Date object, thence y'all demand to starting fourth dimension convert Calendar to Date inward Java in addition to so exceed it to the format() method every bit shown below:
Here is the right way of using Calendar bird 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 recall that Calendar cannot hold out formatted inward Java in addition to DateFormat bird is non thread-safe so it cannot hold out used inward multi-threaded Java application without proper synchronization. This effect has been addressed inward Java 8 yesteryear making DateFormatter immutable in addition to thread-safe. If y'all desire to acquire more, y'all tin mail away likewise read Java 8 inward Action yesteryear Roul-Gabrial Urma in addition to 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 y'all to exceed a customer to acquire reference of Date in addition to modify it without possessor of that Date bird knowing, every bit shown inward below example
Persons p = novel Person();
Date birthDay = p.getBirthDay();
birthDay.setTime();
Now that soul volition direct keep a dissimilar nascency date, so bad. That's why whenever y'all direct keep to furnish Date from your class, y'all must furnish a novel Date object or clone, y'all should never furnish the master Date object, it breaks the Encapsulation of class.
If these reasons were non enough, the SQL Date/Time/Timestamp likewise extends the java.util.Date bird in addition to violates the Liskov Substitution Principle, which way if a method expects the java.util.Date object so y'all cannot exceed the java.sql.Date, java.sql.Time, or java.sql.Timestamp class, fifty-fifty though they are a subclass of java.util.Date, thence y'all demand to convert util engagement to SQL engagement in addition to vice-versa spell passing roughly your application's DAO layer in addition to Business layer.
That's all on why Java's Date, Calendar, in addition to Time classes suck earlier Java 8. It's of import to acquire from sense in addition to that's what Java designers direct keep done that. For you, a Java developer likewise its of import to know shortcomings of Java's sometime Date in addition to Calendar API to appreciate in addition to acclaim greatness of Java 8's novel Date in addition to Time API.
If y'all desire to acquire novel features of Java 8 in addition to novel Date in addition to Time API, I propose reading Java SE 8 for Impatient yesteryear Cay S. Horstmann, 1 of the best books to acquire Java 8 quickly.
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
Sumber https://javarevisited.blogspot.com/
0 Response to "5 Reasons Why Java's Former Appointment As Well As Calendar Api Was Bad"
Post a Comment