-2

I need to get the week of year and week of the month in Jalali (Shamsi) calendar. Is there a library or snippet of code to do that?

I used JalaliCalendar, but it only has the week of the year

System.out.println(JalaliCalendar.weekOfYear(32,1397));

I want to have the week of the month too.

Update:

I use below library: https://github.com/razeghi71/JalaliCalendar/

Kian
  • 211
  • 1
  • 3
  • 17
  • What is your `JalaliCalender` library address? There are plenty of them on Github. – Hamid Ghasemi Jan 01 '19 at 13:46
  • 1
    Possible duplicate of [A good date converter for Jalali Calendar in Java?](https://stackoverflow.com/questions/23385434/a-good-date-converter-for-jalali-calendar-in-java) – Oleg Cherednik Jan 01 '19 at 14:09
  • @oleg.cherednik : it's not. I want to get a specific feature, not just a date convertor. – Kian Jan 01 '19 at 14:39

1 Answers1

2

I suggest you com.ibm.icu. It contains an awesome library for Jalali calendar.

If your project is a maven based, you can use the following dependency:

<dependency>
    <groupId>com.ibm.icu</groupId>
    <artifactId>icu4j</artifactId>
    <version>59.1</version>
</dependency>

And this is a sample use of Persian calendar:

//configuration
ULocale locale = new ULocale("@calendar=persian");
Calendar calendar = Calendar.getInstance(locale);
calendar.setFirstDayOfWeek(7); //Make Saturdays first day of the week.

//usage
calendar.setTime(new Date());
int year = calendar.get(Calendar.YEAR);
int weekOfYear = (calendar.get(Calendar.YEAR_WOY) == year)? calendar.get(Calendar.WEEK_OF_YEAR) : 53;
int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);

It is important to know Calendar and ULocale are from com.ibm.icu.util.Calendar and com.ibm.icu.util.ULocale. NOT java.util.

If you have any problem in understanding the code, do not hesitate to ask.

Hamid Ghasemi
  • 880
  • 3
  • 13
  • 32