0

I have a string of date which is

String dateOfOrder = "02/06/2019";

and I want to find out the week of the year based on my dateOfOrder. I set my string into the Calender class and I tried using this method which is

int WeekOfYear = c.get(Calendar.WEEK_OF_YEAR);

But when I checked online with http://www.whatweekisit.org/

The expected Week of year I should be getting is Week Number 22 but I end up getting Week Number 23. Anyway to resolve this issue?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
jun
  • 33
  • 5
  • Can you share your code for how you are setting the calender – Anupam Jul 01 '19 at 02:26
  • In programming counting starts from 0 so you get a number more than the correct one, so you should decrease one when you get the result and then show the correct one – Gourav Jul 01 '19 at 04:03
  • Weeks are numbered differently in different places. While it may not be a big issue in 2019, you should decide: do you want US week numbers? – Ole V.V. Jul 08 '19 at 17:08
  • 2 June 2019 was a Sunday. In the US and some other places the new week starts on Sunday, so this is already in week 23. According to the international standard (ISO 8601) the new week begins on Monday, so Sunday in in the old week, therefore week 22. – Ole V.V. Jul 08 '19 at 17:13
  • I recommend you don’t use `Calendar`. That class is poorly designed and long outdated. Instead use `LocalDate` and other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 09 '19 at 16:56

2 Answers2

0

Try using like this -

Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,2019);
c.set(Calendar.MONTH,06);
c.set(Calendar.DATE, 02);

Int week = c.get(Calendar.WEEK_OF_YEAR));
Anupam
  • 2,845
  • 2
  • 16
  • 30
0

Maybe you can try to add 1 to your WeekOfYear since in programming the counting starts in 0.

int WeekOfYear = c.get(Calendar.WEEK_OF_YEAR) + 1;

KikX
  • 217
  • 2
  • 17