0

I want to get the week number from the date picker that user select even if the date was past date;

String dateofweek= dateselect.getText().toString();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;



try {
    date=format.parse(dateofweek);
} catch (ParseException e) {
    e.printStackTrace();
}


Calendar cal2 = Calendar.getInstance();
cal2.setTime(date);

int weekofdate= cal2.get(Calendar.WEEK_OF_YEAR);

I tried this but it is showing same week number for every week.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • I strongly recommend you don’t use `SimpleDateFormat`, `Date` and `Calendar`. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). And find a date picker that supports java.time. – Ole V.V. Nov 12 '22 at 17:44
  • I can’t reproduce. When I set `dateofweek` to `11/11/2022`. I get 45. When instead I set it to `01/11/2022`, I get only 44. How come you are getting the date as a string in a non-standard format from the date picker? Shouldn’t you be getting a `LocalDate` object (or some other date object)? – Ole V.V. Nov 12 '22 at 17:49

0 Answers0