2

I am developing a java app in which I need to get a date and number of days from the user and then add this numbers to the date and show the final date which in this case would be the deadline date , to the user. The problem here is that I'm using shamsi(jalali) calendar and in order to the calculation , first I have to convert the shamsi date to java standar calendar then add the day numbers and again convert it back to shamsi date.

Date miladiDate = new Date(DateConvertor.shamsi2miladi(entity.getDoDate()));

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(miladiDate);
c.add(Calendar.DATE, baseEvaluate.getDeadLineDays());
String output = sdf.format(c.getTime());
System.out.println(output);
entityDetail.setDeadLineDate(DateConvertor.miladi2date(c.getTime()));

I wanted to know if there is a way in which I could do this without converting the shamsi date , and just add the day number to the shamsi date. P.S : I used joda time , it does not support the shamsi calendar.

Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67
  • I think you have found the best way regarding the current missing support of libraries for shamsi calendar in Java. – Meno Hochschild Sep 29 '14 at 17:47
  • My previous comment is outdated. Meanwhile I have implemented the Jalali calendar in Java - with support for adding days etc., see demo in my other [SO-post](http://stackoverflow.com/a/33348458/2491410). Just use Time4J instead of Time4A with at least the modules core+i18n+calendar. – Meno Hochschild Feb 08 '16 at 10:02

2 Answers2

0

You can use this Jalali calendar available on github and jcenter which has a builtin method to calculate date based on day difference :

https://github.com/razeghi71/JalaliCalendar

Mohammad Razeghi
  • 1,574
  • 2
  • 14
  • 33
0

This similar Question, Is there any library or algorithm for Persian (Shamsi or Jalali) calendar in Android?, has this Answer which provides the source code for what it claims to be an accurate solar calendar for converting to a Persian (Shamsi) date.

Some other answers there may help.


Pardon my ignorance as I know nothing about Persian calendars in particular… but I can tell you that it is possible to plug a calendar system into Java’s modern date-time architecture (java.time).

java.time

The old java.util.Date/.Calendar classes are a mess, and should be avoided. They have been supplanted by the java.time framework built into Java 8 and later.

Plug in an implementation

I suggest looking for, or even making, a Jalali chronology that can plug into the java.time framework. It would be an implementation of the java.time.chrono.Chronology, and most likely an extension of java.time.chrono.AbstractChronology.

Below I list ten implementations because they are all open-source and may provide help to anyone attempting an implementation. And this list shows that plugging an implementation into java.time has proven to be practicable.

The java.time framework currently includes these calendar systems:

The ThreeTen-Extra project is an extension of java.time. It serves as the proving ground for possible future additions to java.time. If someone did have an open-source implementation of a Jalali chronology, this project might accept it.

Currently ThreeTen-Extra includes these calendar systems:

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • "plugging an implementation into java.time has proven to be practicable..." hm, rather not. For evaluating the quality of the design, it is not enough to just count the offered calendars. The details matter, for example start-of-day deviating from midnight, no i18n for Ethiopian etc, broken thaibuddhist before 1941 and so on! For your information: The overall pluggable chronology design was pushed by Oracle, against the resistance of S. Colbourne. Final result of the conflict was to enhance the `ChronoLocalDate`-API with so many warnings that its javadoc reads like."don't use me". – Meno Hochschild May 02 '16 at 13:55
  • Pluggable chronologies are evil design IMHO, they cannot take into account oddities and deviations of various calendars because they just try to define a "standard" scheme only. See also [Colebournes self-criticism about pluggable chronos](http://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html). This concept has been de facto revived in `java.time` by Oracle - just in a different form. – Meno Hochschild May 02 '16 at 14:03