7

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;
    }
androidlive
  • 103
  • 2
  • 2
  • 9
  • 4
    There's actually a new set of date classes in the `java.time` package that are a big improvement from the time classes in `java.util`. If you can, use that. – Simon Jun 29 '15 at 13:26
  • 4
    If you're using Java 1.8, there's improved DateTime API. This tutorial shows useful tips: http://javarevisited.blogspot.com/2015/03/20-examples-of-date-and-time-api-from-Java8.html – RichardK Jun 29 '15 at 13:28
  • Thank you for your help. I am looking more into the coding assistance of it. – androidlive Jun 29 '15 at 13:37
  • 1
    Please pay more attention if you edit your question. Your latest edit reverted every change I mad to your question, which reintroduces invalid code and other problems. That is why I rolled it back. Please refresh your browser page and then edit something, if you still like to edit your question. – Tom Jun 29 '15 at 13:43
  • 1
    Ok, thanks for your time. I don't need to edit it any more. – androidlive Jun 29 '15 at 13:48
  • possible duplicate of [How to set time to a date object in java](http://stackoverflow.com/questions/5165428/how-to-set-time-to-a-date-object-in-java) – Basil Bourque Jun 29 '15 at 17:51

4 Answers4

9

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;
    }
mmaynar1
  • 326
  • 4
  • 14
  • Thank you very much @mmaynar1. I was looking for a code suggestion like that. I was not sure how use the new methods to work with my code. I am a newbie android/java developer. – androidlive Jun 29 '15 at 14:24
  • No problem. Is that your full method? I might be able to help out more. – mmaynar1 Jun 29 '15 at 14:26
  • @androidlive If this answer helped you to solve your question/problem, then don't forget to [accept it](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Tom Jun 29 '15 at 14:29
  • 1
    Good catch @Tom. It was irrelevant code that I forgot to remove. My answer has been updated to reflect that. – mmaynar1 Jun 29 '15 at 14:31
  • Thank you @mmaynar1 and Tom. I have updated my question with the code. – androidlive Jun 29 '15 at 14:56
  • I have edited my answer to show what you could use in place of your old method. – mmaynar1 Jun 30 '15 at 13:42
  • Thank you very much. I am going to apply your suggestion and keep debugging if I find something. Thanks again +1 – androidlive Jun 30 '15 at 14:14
  • No problem! Glad I could help – mmaynar1 Jun 30 '15 at 14:17
0

The method setHours for Date is deprecated.

You can check the documentation here: http://docs.oracle.com/javase/7/docs/api/java/util/Date.html

If you look at the set hours method you'll see:

"As of JDK version 1.1, replaced by Calendar.set(Calendar.HOUR_OF_DAY, int hours)."

  • 1
    This is what OP already know. He likes to see an example on how to use these methods. – Tom Jun 29 '15 at 14:00
  • 1
    Thanks Tom, I want to know how I can use the new code `Calendar.set(Calendar.HOUR_OF_DAY, int hours)` in my older code that I have already written. I want to know how to integrate this to my code. For example, `this.startTime.Calendar.set(Calendar.HOUR_OF_DAY, hour)` gives me error. I am not sure about adding it to my code. – androidlive Jun 29 '15 at 14:07
0

You can use Apache Commons Lang3 DateUtils setHours(Date date, int hours)

Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119
0

For example you could write something like this:

Calendar calStartTime = Calendar.getInstance();
calStartTime.set(Calendar.HOUR_OF_DAY, hour);
Date startime = calStartTime.getTime();

Calendar calEndTime = Calendar.getInstance();
calEndTime.set(Calendar.HOUR_OF_DAY, hour);
Date endTime= calEndTime.getTime();
  • Not recommended. The `Calendar` class was cumbersome to work with and is fortunately long outdated too. Use java.time, the modern Java date and time API. – Ole V.V. Sep 26 '22 at 14:58
  • 1
    Thanks for the tip, but the fact is that the Calendar class can be safely used and none of its methods are deprecated: https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/Calendar.html unlike the Date class: https://docs.oracle.com/en/java/javase/18/docs/api/java.sql/java/sql/Date.html Of course, if you prefer you can use the Java Time class, introduced if I'm not mistaken in Java 8 which offers useful functionality for developers. https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html – Paolo Guagliumi Sep 27 '22 at 15:50