Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace the deprecated method Date.setHours(int)?

I have some deprecated Date methods in my Java code and I would appreciate if someone can guide me here please. I have a private Date variable:

private Date startime;
private Date endTime;

and in my method I have declared:

Calendar calender = Calendar.getInstance();
this.startTime = calender.getTime();
this.startTime.setHours(0); // ----> is depreacted

this.endTime.setHours(startTime.getHours()); // -->deprecated line as well

Other methods such as setMinutes() and getMinutes() are also deprecated.

I know that I have to use Calendar.set(Calendar.HOUR_OF_DAY, hour). How can use the new code here? all the setHours, getMinutes, etc are all over-lined.

        if (query.getCount() > 0 && query.moveToFirst()) {
          Calendar calender = Calendar.getInstance();
          this.startTime = calender.getTime();
          this.startTime.setHours(0);
          this.startTime.setMinutes(query.getInt("startTimeOfDayMins"));

          this.daysOfWeek = (query.getString("daysOfWeek")).toLowerCase();

          this.endTime = calender.getTime();
          this.endTime.setHours(startTime.getHours());
          this.endTime.setMinutes(startTime.getMinutes() + query.getInt("durationMins"));

        this.context = null;
    }
like image 582
androidlive Avatar asked Jun 29 '15 13:06

androidlive


People also ask

What does deprecated date mean?

In several fields, especially computing, deprecation is the discouragement of use of some terminology, feature, design, or practice, typically because it has been superseded or is no longer considered efficient or safe, without completely removing it or prohibiting its use.

What should I use instead of date in Java?

Joda-Time provides a quality replacement for the Java date and time classes. Joda-Time is the de facto standard date and time library for Java prior to Java SE 8.

Why is date class deprecated?

SimpleDateFormat classes were rushed too quickly when Java first launched and evolved. The classes were not well designed or implemented. Improvements were attempted, thus the deprecations you've found. Unfortunately the attempts at improvement largely failed.


1 Answers

If I understand your question correctly, this should work:

int hours = 0;
Calendar calendar = Calendar.getInstance();
calendar.set( Calendar.HOUR_OF_DAY, hours );
this.startTime = calendar.getTime();

this.endTime = calendar.getTime();

If not, can you show us the full method where you want to replace the date code?

EDIT: Here is the updated version for your full method. Basically how it works is that once you get an instance of the Calendar object it maintains its state. Since you are not planning on changing the hours it only has to be set once. Since you are updating the minutes from your query you will have to set it again before calling calendar.getTime().

    if (query.getCount() > 0 && query.moveToFirst())
    {
        int hours = 0;
        int minutes = query.getInt( "startTimeOfDayMins" );

        Calendar calendar = Calendar.getInstance();
        calendar.set( Calendar.HOUR, hours );
        calendar.set( Calendar.MINUTE, minutes );
        this.startTime = calendar.getTime();

        this.daysOfWeek = ( query.getString( "daysOfWeek" ) ).toLowerCase();

        calendar.set( Calendar.MINUTE, minutes + query.getInt( "durationMins" ) );
        this.endTime = calendar.getTime();

        this.context = null;
    }
like image 126
mmaynar1 Avatar answered Oct 03 '22 07:10

mmaynar1