Why does Java make it not obvious how to get the day of the month from a Date object?
.getDay() was deprecated, it is recommended to use Calendar.get(Calendar.MONTH)
This makes little sense to me and I was curious what the rationale behind this deprecation is.
I have a Date object and I just want the day. It's the most natural thing ever and I can't invoke it. This design is wrong and therefore my code is not working the way it should:
private String getIngivningsDag() {
return ""+ingivningsDatum.getDate();
}
private String getIngivningsMonth() {
return ""+ingivningsDatum.getmonth();
}
private String getIngivningsYear() {
return ""+ingivningsDatum.getYear();
}
here's the "solution":
public String getIngivningsDag() {
Calendar cal = Calendar.getInstance();
cal.setTime(ingivningsDatum);
return cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)+"";
}
Here's how it should look simple and good without the design errors of Java and using method parameters instead of strange Class methods and factories:
public String getIngivningsDag() {
return ingivningsDatum.getDay(Calendar.GREGORIAN, "SE");
}
The Date means a moment of time, but exact day/month/time/etc is different in different calendars and timezones. So, to know what day is a particular timestamp, you should use Calendar Timezone and DateFormat. E.g. jdk has at least two calendars implementation - BuddistCalendar and GregorianCalendar. Default calendar depends on user's preferences.
Also, have a look at Jodatime library - it is has more features and makes more sense to manage dates in Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With