Java 中 Jalali 日历的良好日期转换器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23385434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A good date converter for Jalali Calendar in Java?
提问by Saeed
I'm developing a Java App and I have a timeStamp
(in long
). I can easily use this code to change it to a Gregorian date:
我正在开发一个 Java 应用程序,我有一个timeStamp
(in long
)。我可以轻松地使用此代码将其更改为公历日期:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);
But I need to have the date in Jalali Calendar. I searched but didn't found any good library. Do you know a reliable and good library for converting (or creating dates in Jalali format from timeStamp
)? I don't need an implementation or an algorithm, cause this issue is too buggy and has a lot of rules, I need a reliable solution
但我需要在 Jalali Calendar 中有日期。我搜索但没有找到任何好的图书馆。您是否知道一个可靠且良好的转换库(或从 中创建 Jalali 格式的日期timeStamp
)?我不需要实现或算法,因为这个问题太有问题并且有很多规则,我需要一个可靠的解决方案
采纳答案by AlexR
Take a look on this: https://github.com/amirmehdizadeh/JalaliCalendar
看看这个:https: //github.com/amirmehdizadeh/JalaliCalendar
The code looks nice, maven based project, a lot of unit tests.
代码看起来不错,基于 maven 的项目,很多单元测试。
回答by jarnbjo
For better localization and language support, it is often convenient to use the ICU(International Components for Unicode) library from IBM.
为了更好的本地化和语言支持,使用IBM的ICU(Unicode 国际组件)库通常很方便。
The APIs are similar to the standard Java APIs, but add additional support for localization and internationalization (e.g. time and calendar issues, sorting, formatting rules and a regex implementation with proper Unicode support).
这些 API 类似于标准的 Java API,但增加了对本地化和国际化的额外支持(例如时间和日历问题、排序、格式化规则和具有适当 Unicode 支持的正则表达式实现)。
To create a Persian calendar and output the formatted date in Farsi, you would e.g. do something like this:
要创建波斯日历并以波斯语输出格式化日期,您可以执行以下操作:
import com.ibm.icu.text.DateFormat;
import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.ULocale;
...
ULocale locale = new ULocale("fa_IR@calendar=persian");
Calendar calendar = Calendar.getInstance(locale);
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale);
System.out.println(df.format(calendar));
This will output:
这将输出:
???????? ?? ???????? ???? ??.?.
????????? ?? ????????? ?????.?.
回答by Meno Hochschild
I have developed my own Persian (jalali) calendar in Java within my library Time4J. The implementation deploys the algorithm of Borkowski(valid at least until gregorian year 2129 - no 2025-bug).
我在我的库Time4J 中用Java 开发了我自己的波斯(jalali)日历。该实现部署了Borkowski算法(至少在公历 2129 年之前有效 - 没有 2025-bug)。
Solution for the concrete problem of OP:
OP具体问题的解决方案:
// conversion from jalali to gregorian by constructed input
PersianCalendar jalali = PersianCalendar.of(1394, 11, 5);
// or use a safe enum instead of the month number:
// PersianCalendar jalali = PersianCalendar.of(1394, PersianMonth.BAHMAN, 5);
PlainDate gregorian = jalali.transform(PlainDate.class);
System.out.println(gregorian); // 2016-01-25
// conversion to millis-since-unix (timezone-dependent)
Moment moment1 = gregorian.atStartOfDay().inTimezone(ASIA.TEHRAN);
long millisSinceUnix = TemporalType.MILLIS_SINCE_UNIX.from(moment1);
System.out.println(millisSinceUnix); // 1453667400000L
// conversion of millis-since-unix to jalali (timezone-dependent)
Moment moment2 = TemporalType.MILLIS_SINCE_UNIX.translate(millisSinceUnix);
PlainDate gregorian2 = moment2.toZonalTimestamp(ASIA.TEHRAN).toDate();
System.out.println(gregorian2.transform(PersianCalendar.class)); // AP-1394-11-05
// formatting and parsing in Farsi language using full style
ChronoFormatter<PersianCalendar> f1 =
ChronoFormatter.ofStyle(DisplayMode.FULL, new Locale("fa"), PersianCalendar.axis());
System.out.println(f1.format(jalali)); // ??.?. ???? ???? ?, ??????
System.out.println(f1.parse("??.?. ???? ???? ?, ??????")); // AP-1394-11-05
// formatting in English language using custom pattern
ChronoFormatter<PersianCalendar> f2 =
ChronoFormatter.ofPattern(
"d. MMMM yyyy", PatternType.CLDR, Locale.ENGLISH, PersianCalendar.axis());
System.out.println(f2.format(jalali)); // 5. Bahman 1394
Of course, the calendar offers more features like date arithmetic (adding days or months, calculating the delta in days, months etc.) or field/element-manipulations (easy going to the last day of month etc).
当然,日历提供了更多的功能,比如日期算术(添加天数或月数,计算天数、月数等的增量)或字段/元素操作(容易到月份的最后一天等)。
Side notes about other libraries proposed here so far:
到目前为止,这里提出的关于其他库的旁注:
The libraries amirmehdizadeh/JalaliCalendar as well as ICU4Jboth use zero-based months. This can be extremely confusing. Non-intuitive example using amirmehdizadeh's library:
库amirmehdizadeh/JalaliCalendar 以及ICU4J都使用从零开始的月份。这可能非常令人困惑。使用 amirmehdizadeh 库的非直观示例:
YearMonthDate jalali = new YearMonthDate(1394, 10, 5); // really month 11 (Bahman)
YearMonthDate gregorian = JalaliCalendar.jalaliToGregorian(jalali);
System.out.println(gregorian); // 2016/0/25 => really month 1 (January)
About internationalization, I don't think that ICU4J offers more than Time4J on the area of Persian calendar since latter one is based on newest CLDR-version v28, too. Time4J actually supports about 25 languages for Persian months and eras (including Farsi and Pashto).
关于国际化,我认为 ICU4J 在波斯历领域提供的不仅仅是 Time4J,因为后者也是基于最新的 CLDR 版本 v28。Time4J 实际上支持大约 25 种波斯语月份和时代的语言(包括波斯语和普什图语)。
回答by Mohammad Razeghi
There is a good library on github which has a very simple API and it has a lot of capabilities and it also it available on mavenCentral :
github 上有一个很好的库,它有一个非常简单的 API,它有很多功能,它也可以在 mavenCentral 上使用:
回答by Sajad Bahmani
You can use JalCalthat Jalali(Persian) Calender Convertor in Java:
您可以在 Java 中使用JalCal,即 Jalali(波斯语)日历转换器:
<dependency>
<groupId>com.github.sbahmani</groupId>
<artifactId>jalcal</artifactId>
<version>1.0</version>
</dependency>
1. Jalali to Gregorian
1. 贾拉里到格里高利
Calendar expected4 = Calendar.getInstance(TimeZone.getDefault());
expected4.set(2014, 7, 5, 1, 23, 1);
assertThat(JalCal.jalaliToGregorian(1393, 5, 14, 1, 23, 1).toString()).isEqualTo(expected4.getTime().toString());
2. Gregorian to Jalali
2. 格里高利到贾拉利
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 5);
cal.set(Calendar.MONTH, 6);
cal.set(Calendar.YEAR, 2014);
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 25);
cal.set(Calendar.SECOND, 1);
cal.set(Calendar.MILLISECOND, 0);
assertThat(JalCal.gregorianToJalali(cal.getTime(), true)).isEqualTo("14/04/1393 10:25:01");
assertThat(JalCal.gregorianToJalali(new Date(1426883400000l), true)).isEqualTo("01/01/1394 00:00:00");