Is it possible to localize Java calendar class to convert current system date to for example Jalali (Persian) date?
-
2this [answer](http://stackoverflow.com/questions/10378764/is-there-any-library-or-algorithm-for-persian-shamsi-or-jalali-calendar-in-and) may help you – Subhrajyoti Majumder Mar 18 '14 at 04:16
-
1you can use https://github.com/razeghi71/JalaliCalendar as an alternative. – Mohammad Razeghi Apr 21 '16 at 10:12
-
3See related Question: [How to implement Persian Calandar](https://stackoverflow.com/q/39357121/642706) – Basil Bourque Jan 14 '18 at 06:03
2 Answers
When you create instance you pass the Locale
, for example
Calendar.getInstance(Locale.US);

- 237,923
- 42
- 401
- 438
-
2FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 16 '18 at 08:42
-
2While the new API is better, You did not support "Terribly troublesome" with some valid proofs :) – jmj Aug 16 '18 at 16:36
Localize
How to localize Java Calendar class?
You don’t. Instead, you localize a ZonedDateTime
object.
The terrible Calendar
class, actually GregorianCalendar
, was supplanted years by the ZonedDateTime
class. All the awful old date-time classes from the earliest versions of Java (Date
, Calendar
, SimpleDateFormat
, java.sql.Timestamp
, java.sql.Date
, etc.) were all outmoded by the modern java.time classes.
You can easily convert from the legacy classes to the modern by calling new methods added to the old classes.
ZonedDateTime zdt = ( ( GregorianCalendar ) myCalendar ).toZonedDateTime() ;
Now let DateTimeFormatter
automatically localize while generating a String
representing textually the date-time value.
To localize, specify:
FormatStyle
to determine how long or abbreviated should the string be.Locale
to determine:- The human language for translation of name of day, name of month, and such.
- The cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
Example:
Locale l = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL )
.withLocale( l ) ;
String output = zdt.format( f ) ;
Jalali calendar
convert current system date to for example Jalali (Persian) date?
Java does come bundled with an implementation of Chronology
for the Hijrah calendar system: HijrahChronology
. Perhaps that might meet your needs (I know next-to-nothing about chronologies other than ISO 8601).
You might learn more about this by searching Stack Overflow.
If that implementation does not meet your needs, you might find one elsewhere or develop your own. Besides the several open-source chronologies bundled with Java, you will find several more in the ThreeTen-Extra project, all open-source. You might find others from other sources.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 303,325
- 100
- 852
- 1,154