-1

The following sql is to calculate the week number of 20811231, the result is 1, which means 20181231's year week is 201901

select to_number(to_char(to_date('20181231','YYYYMMDD'), 'iw')) from dual

What I am doing now is to write a java method that does the same thing as the above sql: input 20181231, the method will output 1,and for other inputs, the output should also be the same.

I want to ask how I can achieve this using java.util.Calendar class

Tom
  • 5,848
  • 12
  • 44
  • 104
  • See [SimpleDateFormat](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). Note: this class is NOT thread-safe – lance-java Feb 28 '18 at 09:23
  • 2
    Being very picky... the title refers to `to_date()`, but `iw` isn't a valid format model for that function; you're really asking about `to_char()`. – Alex Poole Feb 28 '18 at 09:52
  • `LocalDate.of(2018, 12, 31).get(WeekFields.ISO.weekOfYear())` will give you `53` and `LocalDate.of(2018, 12, 31).get(WeekFields.ISO.weekOfWeekBasedYear())` will give you `1`. So it depends, whether you're looking for the "week of week-based year" or "week of year". – Mick Mnemonic Feb 28 '18 at 10:11

1 Answers1

1

Format IW means week number according ISO-8601

However I strongly assume there is a build-in function in Java, you don't have to code it by yourself.

Have a look at What is a good, simple way to compute ISO 8601 week number?

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110