5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad
5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul 5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan
Artikel core java,
Artikel date and time tutorial, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.
Judul : 5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad
link : 5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad
Anda sekarang membaca artikel 5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad dengan alamat link https://bestlearningjava.blogspot.com/2020/03/5-reasons-why-javas-onetime-appointment.html
Judul : 5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad
link : 5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad
5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad
If you lot lead maintain been programming inward Java for a brace of years so 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 fault 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 utilization novel Date as well as Time API from Java 8, I am going to listing downward a brace of reasons why Java's Date as well as Calendar course of report has been criticized so much inward the past. These points are besides of import to know from the Interview betoken of sentiment because every bit 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 exercise a Date object for 25th Dec 2017, 8.30 at night, exercise you lot think this is the right way to correspond 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 higher upward code contains 2 bugs, which is non at all intuitive. If you lot are using the java.util.Date course of report so 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 recall that Jan is 0, Dec is eleven as well as Date utilization years from 1900.
2) Timezones
Prior to JDK 8, Java uses String to correspond TimeZone, a really bad idea. Ideally, they should lead maintain defined a constant or Enum instead of allowing the user to overstep String. Since String lacks compiler checks, it opens a door for spelling mistakes as well as empty-headed human errors. Java's naming convention amongst timezones besides doesn't help. Look at the next example, hither I am trying to acquire TimeZone object for NewYork, doesn't it facial expression all right?
TimeZone zone = TimeZone.getInstance("America/NewYork");
Unfortunately, it's non correct. It contains a empty-headed but difficult to detect 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 lead maintain used Enum, these would lead maintain been flagged past times the compiler or your IDE every bit presently every bit you lot typed. Another trial of this variety of gotcah is "Asia/Hong_Kong", so move careful spell using those timezones inward Java.
3) Calendars
The Calendar course of report is besides non really user-friendly or intuitive inward Java. You precisely cannot apply mutual sense or predictive knowledge. For trial inward the next code, nosotros are trying to exercise a Calendar object for a appointment inward a especial timezone, It volition non present whatever compile fourth dimension error but it volition non piece of occupation every bit you lot expect.
Calendar cal = novel GregorianCalendar(date, zone);
Right way of using Calandar object amongst appointment as well as timezone is every bit next :
Calendar cal = novel GregorianCalendar(zone);
cal.setTime(date);
So, if you lot haven't used Calendar API from a long time, at that spot is no peril you lot tin acquire it right later that.
Here is some other trial of how intuitive novel Date as well as Time API has every bit compared to former Date as well as Calendar API:
If you lot are interested on to a greater extent than examples of former as well as novel Date as well as Time API similar this, I propose exploring a proficient 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 thing most.
4) Formatting Dates
Formatting dates are ane of the almost mutual tasks spell using Dates inward Java. Given multi-threading is the marrow describe of Java, ane should facial expression that their marrow classes e.g. String, Integer or Date should piece of occupation seamlessly inward concurrent applications. Java designers made a proficient undertaking amongst String as well as Integer but did a actually pitiable undertaking amongst Date, formatting appointment was really tricky inward Java. One of the almost useful utility to format dates, SimpleDateFormat was non thread-safe.
DateFormat fm = novel SimpleDateFormat("HH:mm Z");
String str = fm.format(cal);
This trial contains a brace of bugs, first, you lot lead maintain to laid upward Timezone for DateFormat course of report as well as second, you lot cannot overstep a Calendar object to format method, you lot tin solely overstep a java.util.Date object, thence you lot demand to starting fourth dimension convert Calendar to Date inward Java as well as so overstep it to the format() method every bit shown below:
Here is the right way of using Calendar course of report amongst 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 move formatted inward Java as well as DateFormat course of report is non thread-safe so it cannot move used inward multi-threaded Java application without proper synchronization. This trial 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 besides read Java 8 inward Action past times Roul-Gabrial Urma as well as team.
5) Mutable
This is IMHO biggest fault Java designers has made inward the JDK. Unlike String or Integer, Date is mutable. It's possible for you lot to overstep a customer to acquire reference of Date as well as alter it without possessor of that Date course of report knowing, every bit shown inward below example
Persons p = novel Person();
Date birthDay = p.getBirthDay();
birthDay.setTime();
Now that someone volition lead maintain a dissimilar nativity date, so bad. That's why whenever you lot lead maintain to provide Date from your class, you lot must provide a novel Date object or clone, you lot should never provide the master Date object, it breaks the Encapsulation of class.
If these reasons were non enough, the SQL Date/Time/Timestamp besides extends the java.util.Date course of report as well as violates the Liskov Substitution Principle, which agency if a method expects the java.util.Date object so you lot cannot overstep 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 approximately 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 lead maintain done that. For you, a Java developer besides its of import to know shortcomings of Java's former 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, ane 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 exercise a Date object for 25th Dec 2017, 8.30 at night, exercise you lot think this is the right way to correspond 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 higher upward code contains 2 bugs, which is non at all intuitive. If you lot are using the java.util.Date course of report so 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 recall that Jan is 0, Dec is eleven as well as Date utilization years from 1900.
2) Timezones
Prior to JDK 8, Java uses String to correspond TimeZone, a really bad idea. Ideally, they should lead maintain defined a constant or Enum instead of allowing the user to overstep String. Since String lacks compiler checks, it opens a door for spelling mistakes as well as empty-headed human errors. Java's naming convention amongst timezones besides doesn't help. Look at the next example, hither I am trying to acquire TimeZone object for NewYork, doesn't it facial expression all right?
TimeZone zone = TimeZone.getInstance("America/NewYork");
Unfortunately, it's non correct. It contains a empty-headed but difficult to detect 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 lead maintain used Enum, these would lead maintain been flagged past times the compiler or your IDE every bit presently every bit you lot typed. Another trial of this variety of gotcah is "Asia/Hong_Kong", so move careful spell using those timezones inward Java.
3) Calendars
The Calendar course of report is besides non really user-friendly or intuitive inward Java. You precisely cannot apply mutual sense or predictive knowledge. For trial inward the next code, nosotros are trying to exercise a Calendar object for a appointment inward a especial timezone, It volition non present whatever compile fourth dimension error but it volition non piece of occupation every bit you lot expect.
Calendar cal = novel GregorianCalendar(date, zone);
Right way of using Calandar object amongst appointment as well as timezone is every bit next :
Calendar cal = novel GregorianCalendar(zone);
cal.setTime(date);
So, if you lot haven't used Calendar API from a long time, at that spot is no peril you lot tin acquire it right later that.
Here is some other trial of how intuitive novel Date as well as Time API has every bit compared to former Date as well as Calendar API:
If you lot are interested on to a greater extent than examples of former as well as novel Date as well as Time API similar this, I propose exploring a proficient 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 thing most.
4) Formatting Dates
Formatting dates are ane of the almost mutual tasks spell using Dates inward Java. Given multi-threading is the marrow describe of Java, ane should facial expression that their marrow classes e.g. String, Integer or Date should piece of occupation seamlessly inward concurrent applications. Java designers made a proficient undertaking amongst String as well as Integer but did a actually pitiable undertaking amongst Date, formatting appointment was really tricky inward Java. One of the almost useful utility to format dates, SimpleDateFormat was non thread-safe.
DateFormat fm = novel SimpleDateFormat("HH:mm Z");
String str = fm.format(cal);
This trial contains a brace of bugs, first, you lot lead maintain to laid upward Timezone for DateFormat course of report as well as second, you lot cannot overstep a Calendar object to format method, you lot tin solely overstep a java.util.Date object, thence you lot demand to starting fourth dimension convert Calendar to Date inward Java as well as so overstep it to the format() method every bit shown below:
Here is the right way of using Calendar course of report amongst 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 move formatted inward Java as well as DateFormat course of report is non thread-safe so it cannot move used inward multi-threaded Java application without proper synchronization. This trial 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 besides read Java 8 inward Action past times Roul-Gabrial Urma as well as team.
5) Mutable
This is IMHO biggest fault Java designers has made inward the JDK. Unlike String or Integer, Date is mutable. It's possible for you lot to overstep a customer to acquire reference of Date as well as alter it without possessor of that Date course of report knowing, every bit shown inward below example
Persons p = novel Person();
Date birthDay = p.getBirthDay();
birthDay.setTime();
Now that someone volition lead maintain a dissimilar nativity date, so bad. That's why whenever you lot lead maintain to provide Date from your class, you lot must provide a novel Date object or clone, you lot should never provide the master Date object, it breaks the Encapsulation of class.
If these reasons were non enough, the SQL Date/Time/Timestamp besides extends the java.util.Date course of report as well as violates the Liskov Substitution Principle, which agency if a method expects the java.util.Date object so you lot cannot overstep 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 approximately 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 lead maintain done that. For you, a Java developer besides its of import to know shortcomings of Java's former 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, ane 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!
Demikianlah Artikel 5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad
Sekianlah artikel 5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
Anda sekarang membaca artikel 5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad dengan alamat link https://bestlearningjava.blogspot.com/2020/03/5-reasons-why-javas-onetime-appointment.html
Belum ada Komentar untuk "5 Reasons Why Java's Onetime Appointment As Well As Calendar Api Was Bad"
Posting Komentar