content: package com.sardari.daterangepicker.utils;
  
  import java.util.GregorianCalendar;
  import java.util.TimeZone;
  
  public class PersianCalendar extends GregorianCalendar {
      private static final long serialVersionUID = 5541422440580682494L;
  
      private int persianYear;
      private int persianMonth;
      private int persianDay;
      private String delimiter = "/";
  
      private long convertToMillis(long julianDate) {
          return PersianCalendarConstants.MILLIS_JULIAN_EPOCH + julianDate * PersianCalendarConstants.MILLIS_OF_A_DAY
                  + PersianCalendarUtils.ceil(getTimeInMillis() - PersianCalendarConstants.MILLIS_JULIAN_EPOCH, PersianCalendarConstants.MILLIS_OF_A_DAY);
      }
  
      public PersianCalendar(long millis) {
          setTimeInMillis(millis);
      }
  
      public PersianCalendar() {
          setTimeZone(TimeZone.getTimeZone("GMT"));
      }
  
      private void calculatePersianDate() {
          long julianDate = ((long) Math.floor((getTimeInMillis() - PersianCalendarConstants.MILLIS_JULIAN_EPOCH)) / PersianCalendarConstants.MILLIS_OF_A_DAY);
          long PersianRowDate = PersianCalendarUtils.julianToPersian(julianDate);
          long year = PersianRowDate >> 16;
          int month = (int) (PersianRowDate & 0xff00) >> 8;
          int day = (int) (PersianRowDate & 0xff);
          this.persianYear = (int) (year > 0 ? year : year - 1);
          this.persianMonth = month;
          this.persianDay = day;
      }
  
      public boolean isPersianLeapYear() {
          // calculatePersianDate();
          return PersianCalendarUtils.isPersianLeapYear(this.persianYear);
      }
  
      public void setPersianDate(int persianYear, int persianMonth, int persianDay) {
          persianMonth += 1;
          this.persianYear = persianYear;
          this.persianMonth = persianMonth;
          this.persianDay = persianDay;
          setTimeInMillis(convertToMillis(PersianCalendarUtils.persianToJulian(this.persianYear > 0 ? this.persianYear : this.persianYear + 1, this.persianMonth - 1, this.persianDay)));
      }
  
      public void setPersianYear(int persianYear) {
          this.persianMonth += 1;
          this.persianYear = persianYear;
          setTimeInMillis(convertToMillis(PersianCalendarUtils.persianToJulian(this.persianYear > 0 ? this.persianYear : this.persianYear + 1, this.persianMonth - 1, this.persianDay)));
      }
  
      public void setPersianMonth(int persianMonth) {
          persianMonth += 1;
          this.persianMonth = persianMonth;
          setTimeInMillis(convertToMillis(PersianCalendarUtils.persianToJulian(this.persianYear > 0 ? this.persianYear : this.persianYear + 1, this.persianMonth - 1, 1)));
      }
  
      public void setPersianDay(int persianDay) {
          this.persianMonth += 1;
          this.persianDay = persianDay;
          setTimeInMillis(convertToMillis(PersianCalendarUtils.persianToJulian(this.persianYear > 0 ? this.persianYear : this.persianYear + 1, this.persianMonth - 1, this.persianDay)));
      }
  
      public int getPersianYear() {
          // calculatePersianDate();
          return this.persianYear;
      }
  
      public int getPersianMonth() {
          // calculatePersianDate();
          return this.persianMonth;
      }
  
      public String getPersianMonthName() {
          // calculatePersianDate();
          return PersianCalendarConstants.persianMonthNames[this.persianMonth];
      }
  
      public int getPersianDay() {
          // calculatePersianDate();
          return this.persianDay;
      }
  
      public String getPersianWeekDayName() {
          switch (get(DAY_OF_WEEK)) {
              case SATURDAY:
                  return PersianCalendarConstants.persianWeekDays[0];
              case SUNDAY:
                  return PersianCalendarConstants.persianWeekDays[1];
              case MONDAY:
                  return PersianCalendarConstants.persianWeekDays[2];
              case TUESDAY:
                  return PersianCalendarConstants.persianWeekDays[3];
              case WEDNESDAY:
                  return PersianCalendarConstants.persianWeekDays[4];
              case THURSDAY:
                  return PersianCalendarConstants.persianWeekDays[5];
              default:
                  return PersianCalendarConstants.persianWeekDays[6];
          }
      }
  
      public String getPersianLongDate() {
          return getPersianWeekDayName() + "  " + this.persianDay + "  " + getPersianMonthName() + "  " + this.persianYear;
      }
  
      public String getPersianLongDateAndTime() {
          return getPersianLongDate() + " ساعت " + get(HOUR_OF_DAY) + ":" + get(MINUTE) + ":" + get(SECOND);
      }
  
      public String getPersianShortDate() {
          // calculatePersianDate();
          return "" + formatToMilitary(this.persianYear) + delimiter + formatToMilitary(getPersianMonth() + 1) + delimiter + formatToMilitary(this.persianDay);
      }
  
      public String getPersianShortDateTime() {
          return "" + formatToMilitary(this.persianYear) + delimiter + formatToMilitary(getPersianMonth() + 1) + delimiter + formatToMilitary(this.persianDay) + " " + formatToMilitary(this.get(HOUR_OF_DAY)) + ":" + formatToMilitary(get(MINUTE))
                  + ":" + formatToMilitary(get(SECOND));
      }
  
      private String formatToMilitary(int i) {
          return (i <= 9) ? "0" + i : String.valueOf(i);
      }
  
      public void addPersianDate(int field, int amount) {
          if (amount == 0) {
              return; // Do nothing!
          }
  
          if (field < 0 || field >= ZONE_OFFSET) {
              throw new IllegalArgumentException();
          }
  
          if (field == YEAR) {
              setPersianDate(this.persianYear + amount, getPersianMonth() + 1, this.persianDay);
              return;
          } else if (field == MONTH) {
              setPersianDate(this.persianYear + ((getPersianMonth() + 1 + amount) / 12), (getPersianMonth() + 1 + amount) % 12, this.persianDay);
              return;
          }
          add(field, amount);
          calculatePersianDate();
      }
  
      public void addPersianDate1(int field, int amount) {
          if (amount == 0) {
              return; // Do nothing!
          }
  
          if (field < 0 || field >= ZONE_OFFSET) {
              throw new IllegalArgumentException();
          }
  
          if (field == YEAR) {
              setPersianDate(this.persianYear + amount, getPersianMonth() + 1, 1);
              return;
          } else if (field == MONTH) {
              setPersianDate(this.persianYear + ((getPersianMonth() + 1 + amount) / 12), (getPersianMonth() + 1 + amount) % 12, this.persianDay);
  
  //            setPersianDate(this.persianYear + ((getPersianMonth() + amount) / 12), (getPersianMonth() + 1 + amount) % 12, 1);
  
              return;
          }
  
  //        add(field, amount);
  
          calculatePersianDate();
      }
  
      public void parse(String dateString) {
          PersianCalendar p = new PersianDateParser(dateString, delimiter).getPersianDate();
          setPersianDate(p.getPersianYear(), p.getPersianMonth(), p.getPersianDay());
      }
  
      public String getDelimiter() {
          return delimiter;
      }
  
      public void setDelimiter(String delimiter) {
          this.delimiter = delimiter;
      }
  
      @Override
      public String toString() {
          String str = super.toString();
          return str.substring(0, str.length() - 1) + ",PersianDate=" + getPersianShortDate() + "]";
      }
  
      @Override
      public boolean equals(Object obj) {
          return super.equals(obj);
  
      }
  
      @Override
      public int hashCode() {
          return super.hashCode();
      }
  
      @Override
      public void set(int field, int value) {
          super.set(field, value);
          calculatePersianDate();
      }
  
      @Override
      public void setTimeInMillis(long millis) {
          super.setTimeInMillis(millis);
          calculatePersianDate();
      }
  
      @Override
      public void setTimeZone(TimeZone zone) {
          super.setTimeZone(zone);
          calculatePersianDate();
      }
  }
  