-1

I have a calendar picker that allows user to pick a start date and end dates.

I want to extract the day from a month, the old way to use getDay produces a wrong day from a month. Is there any other methods that I can use to get the date from a month and put it into an int type?

      Date date_from = HolidayForm.pickerFrom.getDate();
      Date date_to = HolidayForm.pickerTo.getDate();

      //getDay is deprecated 
      int from = date_from.getDay();
      int to = date_to.getDay();

      //so i can do to find difference. 
      int diff = to-from;
user1851359
  • 139
  • 2
  • 14
  • 1
    Use a [`Calendar`](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) or – even better – [Joda Time](http://joda-time.sourceforge.net/). – Matt Ball Feb 28 '13 at 00:02
  • See http://stackoverflow.com/questions/7226156/how-to-get-day-of-the-month – Mihai8 Feb 28 '13 at 00:04
  • Just a heads up, as I didn't understand initially. This is being downvoted as getDate() is deprecated, and I believe was deprecated when this post was made. – James Nurse Jan 25 '16 at 15:06

2 Answers2

6

Using Calendar API:

Methods in java.util.Date class are mostly deprecated. you have to use java.util.calendar class in order to do manipulation on dates.

Date d = new Date(); 
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        System.out.println(cal.get(Calendar.DAY_OF_MONTH));
PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

Use Calendar object :

Try this :

Date date_from = HolidayForm.pickerFrom.getDate();
Date date_to = HolidayForm.pickerTo.getDate();

Calendar calFrom = Calendar.getInstance();
calFrom.setTime(date_from);
int from = calFrom.get(Calendar.DAY_OF_MONTH);

Calendar calTo = Calendar.getInstance();
calTo.setTime(date_to);
int to = calTo.get(Calendar.DAY_OF_MONTH);

int diff = to-from;
Iswanto San
  • 18,263
  • 13
  • 58
  • 79
  • I wrote the same code as this before, doesnt work,but then i copied this code, it works. Programming is like magic. :)) thanks – user1851359 Feb 28 '13 at 00:08