Java Date vs Calendar

Java Date vs Calendar

Asked on December 1, 2018 in Java.
Add Comment


  • 4 Answer(s)

    Try to do this , this works fine.

    In order to get set particular dates or do date arithmetic, use a Calendar. Because date is a simpler class and is mainly there for backward compatibility reasons. If you need to . Calendars also handle localization. The previous date manipulation functions of Date have since been deprecated.

    individualistically we tend to use either time in milliseconds as a long or Long, as appropriate or Calendar when there is a choice.

    Here, both Date and Calendar are mutable.They tends to present issues when using either in an API.

    Answered on December 1, 2018.
    Add Comment

    Here i’m going to share with a solution for this question very simple terms.  The best way for new code is to use the Joda Time library. if your policy allows third-party code.

    Here, both, Date and Calendar , have itself so many design problems that neither are good solutions for new codings.

    Answered on December 1, 2018.
    Add Comment

    The concept of the Java versus calender is following, Remember one thing date is best for storing a date object. It is the persisted one, the Serialized one …

    chronology or Calendar is best for operating  Dates.

    Date is mutable and therefore not thread-safe. so we also sometimes favor java.lang.Long over Date.. On a Date object, use setTime() and getTime() to switch between the two. For example, a constant Date in the application.

    Here is an examples: the zero 1970/01/01, or an applicative END_OF_TIME that you set to 2099/12/31 ; those are very useful to replace null values as start time and end time, especially when you persist them in the database, as SQL is so peculiar with nulls.

    Answered on December 1, 2018.
    Add Comment
    • Date and Calendar are really the same fundamental concept (both represent an instant in timeand are wrappers around an underlying long value).
    • One could argue that Calendar is actually even more broken than Date is, as it seems to offer concrete facts about things like day of the week and time of day, whereas if you change its timeZone property, the concrete turns into blancmange! Neither objects are really useful as a store of year-month-day or time-of-day for this reason.
    • Use Calendar only as a calculator which, when given Date and TimeZone objects, will do calculations for you. Avoid its use for property typing in an application.
    • Use SimpleDateFormat together with TimeZone and Date to generate display Strings.
    • If you’re feeling adventurous use Joda-Time, although it is unnecessarily complicated IMHO and is soon to be superceded by the JSR-310 date API in any event.
    • I have answered before that it is not difficult to roll your own YearMonthDay class, which uses Calendar under the hood for date calculations. I was downvoted for the suggestion but I still believe it is a valid one because Joda-Time (and JSR-310) are really so over-complicated for most use-cases.
    Answered on January 18, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.