Is there any library or algorithm for Persian (Sha

2020-01-25 13:15发布

I want to convert Gregorian (western) date to Persian (Shamsi) date and vice versa for all versions of Android.

Is there any complete and reliable library or algorithm?

11条回答
家丑人穷心不美
2楼-- · 2020-01-25 13:48

Try this

import java.util.Calendar;
import java.util.Date;
public class PersianCalendar {

String[] weekDayNames = {
        "شنبه","یکشنبه","دوشنبه",
        "سه شنبه", "چهارشنبه",
        "پنج شنبه", "جمعه"
};
String[] monthNames ={
        "فروردین","اردیبهشت","خرداد","تیر", "مرداد","شهریور",
         "مهر", "آبان", "آذر","دی", "بهمن","اسفند"
};   
String strWeekDay = "";
String strMonth = "";    
int day;
int month;
int year;    
int ld;     
Calendar calendar = Calendar.getInstance();      
int gregorianYear =calendar.get(Calendar.YEAR);
int gregorianMonth = calendar.get(Calendar.MONTH)+1;
int gregorianDate = calendar.get(Calendar.DATE);
int WeekDay = calendar.get(Calendar.DAY_OF_WEEK);

int[] buf1 = {0,31,59,90,120,151,181,212,243,273,304,334};
int[] buf2 = {0,31,60, 91,121,152,182, 213, 244, 274,305,335};

public PersianCalendar(){
    Date gregorianDate = new Date();
    calendar.setTime(gregorianDate);
    toPersian(gregorianDate);
}

public PersianCalendar(Date gregorianDate){
    calendar.setTime(gregorianDate);
    toPersian(gregorianDate);
}    

private void toPersian(Date gregorianDate) 
{
    if ((gregorianYear % 4) != 0) 
        func1();
    else 
        func2();
    strMonth = monthNames[month-1];
    strWeekDay = weekDayNames[WeekDay];
}

private void func1()
{
    day = buf1[gregorianMonth - 1] + gregorianDate;
    if (day > 79){
        day = day - 79;
        if (day <= 186) {               
            int day2 = day;
            month = (day2 / 31) + 1;
            day = (day2 % 31);
            if(day2 % 31 == 0){
                 month--;
                 day = 31;
            }                
            year = gregorianYear - 621;
        } 
        else {
            int day2 = day - 186;
            month = (day2 / 30) + 7;
            day = (day2 % 30);
            if(day2 % 30 == 0){
                month = (day2 / 30) + 6;
                day = 30;
            }
            year = gregorianYear - 621;
        }
    } 
    else{            
        ld = gregorianYear > 1996 && gregorianYear % 4 == 1 ? 11 : 10 ;            
        int day2 = day + ld;
        month = (day2 / 30) + 10;
        day = (day2 % 30);            
        if(day2 % 30 == 0)            {
            month--;
            day = 30;
        }
        year = gregorianYear - 622;
    }
}

private void func2()
{
    day = buf2[gregorianMonth - 1] + gregorianDate;     
    ld = gregorianYear >= 1996 ? 79 : 80 ;        
    if (day > ld) {
        day = day - ld;
        if (day <= 186) {
            int day2 = day;
            month = (day2 / 31) + 1;
            day = (day2 % 31);
            if(day2 % 31 == 0){
                month--;
                day = 31;
            }
            year = gregorianYear - 621;
        } else {
            int day2 = day - 186; 
            month = (day2 / 30) + 7;
            day = (day2 % 30);
            if(day2 % 30 == 0 ){
                month--;
                day = 30;
            }
            year = gregorianYear - 621;
        }
    }
    else {
        int day2 = day + 10;
        month = (day2 / 30) + 10;
        day = (day2 % 30);
        if(day2 % 30==0){
            month--;
            day = 30;
        }
        year = gregorianYear - 622;
    }
  }
}

create instance

PersianCalendar sc = new PersianCalendar();            
String s= sc.strWeekDay  + " " +sc.day  + " " + 
     sc.strMonth + " " + sc.year;
System.out.print(s);
//setTitle(s);
查看更多
你好瞎i
3楼-- · 2020-01-25 13:50

You may use this stable and tested library with a formatter class, Roozh for Java. It's not deprecated and always getting updated with cool features which Persian date time needs.

查看更多
我只想做你的唯一
4楼-- · 2020-01-25 13:52

Use IBM's International Components for Unicode (icu4j). It is part of Unicode Consortium, is extremely reliable and can be used in any java project (Java EE, Java SE, Android, etc). Use it with Gradle, Maven or simply by downloading the jar.

TL;DR

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import com.ibm.icu.text.DateFormat;
import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.ULocale;

public class DateTimeUtils {
    private static final ULocale PERSIAN_LOCALE = new ULocale("fa_IR@calendar=persian");
    private static final ZoneId IRAN_ZONE_ID = ZoneId.of("Asia/Tehran");

    public static Date fromPersianDateToDate(int year, int month, int day, int hour, int minutes, int seconds) {
        return new Date(fromPersianDate(year, month, day, hour, minutes, seconds));
    }

    public static Calendar fromDateToPersianCalendar(Date date) {
        Calendar persianCalendar = Calendar.getInstance(PERSIAN_LOCALE);
        persianCalendar.clear();
        persianCalendar.setTime(date);
        return persianCalendar;
    }

    /**
     * @param date
     * @param field example: Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, etc
     */
    public static int fromDateToPersianCalendarField(Date date, int field) {
        return fromDateToPersianCalendar(date).get(field);
    }

    public static String fromDateToPersianString(int year, int month, int day, int hour, int minutes, int seconds) {
        return fromDateToPersianString(fromPersianDateToDate(year, month, day, hour, minutes, seconds));
    }

    public static String fromDateToPersianString(Date date) {
        DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, PERSIAN_LOCALE);
        return df.format(date);
    }

    public static LocalDateTime fromPersianDateToLocalDateTime(int year, int month, int day, int hour, int minutes, int seconds) {
        return fromPersianDateToZonedDateTime(year, month, day, hour, minutes, seconds).toLocalDateTime();
    }

    public static ZonedDateTime fromPersianDateToZonedDateTime(int year, int month, int day, int hour, int minutes, int seconds) {
        return toZonedDateTime(fromPersianDate(year, month, day, hour, minutes, seconds));
    }

    public static long fromPersianDate(int year, int month, int day, int hour, int minutes, int seconds) {
        Calendar persianCalendar = Calendar.getInstance(PERSIAN_LOCALE);
        persianCalendar.clear();
        persianCalendar.set(year, month, day, hour, minutes, seconds);
        return persianCalendar.getTimeInMillis();
    }

    public static ZonedDateTime toZonedDateTime(Long epochMilli) {
        if(epochMilli == null) return null;
        return Instant.ofEpochMilli(epochMilli).atZone(IRAN_ZONE_ID);
    }
}

Usage:

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Date;
import com.ibm.icu.util.Calendar;

public class DateTimeUtilsTest {
    public static void main(String[] args) {
        System.out.println("Java 7 and before:");
        Date date = new Date(1467262800000L);
        System.out.println(DateTimeUtils.fromDateToPersianCalendarField(date, Calendar.YEAR));
        System.out.println(DateTimeUtils.fromDateToPersianCalendarField(date, Calendar.MONTH));
        System.out.println(DateTimeUtils.fromDateToPersianCalendarField(date, Calendar.DAY_OF_MONTH));
        Date gregorianDate = DateTimeUtils.fromPersianDateToDate(1395, 3, 10, 9, 30, 0);
        System.out.println(gregorianDate);
        System.out.println(DateTimeUtils.fromDateToPersianString(gregorianDate));

        System.out.println("\n"+"Java 8 onward:");
        ZonedDateTime gregorianZonedDateTime = DateTimeUtils.fromPersianDateToZonedDateTime(1395, 3, 10, 9, 30, 0);
        System.out.println(gregorianZonedDateTime);
        LocalDateTime gregorianLocalDateTime = DateTimeUtils.fromPersianDateToLocalDateTime(1395, 3, 10, 9, 30, 0);
        System.out.println(gregorianLocalDateTime);
    }
}

Output:

Java 7 and before:
1395
3
10
Thu Jun 30 09:30:00 IRDT 2016
۱۳۹۵ تیر ۱۰, پنجشنبه

Java 8 onward:
2016-06-30T09:30+04:30[Asia/Tehran]
2016-06-30T09:30

More detailed:

Java 7 and before:

You can expect all of functionalities of java.util.Calendar in addition to some other ones:

import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.ULocale;

...

ULocale locale = new ULocale("fa_IR@calendar=persian");
Calendar persianCalendar = Calendar.getInstance(locale);
persianCalendar.clear();
persianCalendar.set(1395, 3, 10); // Tir(4th month) 10th 1395 equivalent to June 30th 2016 

java.util.Date gregorianDate = persianCalendar.getTime();
System.out.println(gregorianDate); // Thu Jun 30 00:00:00 IDT 2016

// Gregorian to Persian
java.util.Calendar gregorianCal = java.util.GregorianCalendar.getInstance();
gregorianCal.set(2016, java.util.Calendar.JUNE, 30);

persianCalendar.setTime(gregorianCal.getTime());
System.out.println(persianCalendar.get(Calendar.YEAR));         // 1395
System.out.println(persianCalendar.get(Calendar.MONTH));        // 3
System.out.println(persianCalendar.get(Calendar.DAY_OF_MONTH)); // 10

WARNING: Note that month field is zero based in Java calendar so by calendar.set(1395, 3, 10) calendar will represent 4th month of 1395, not 3rd!

If you need text outputs in persian:

import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.SimpleDateFormat;

...

// full date output in persian
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale);
System.out.println(df.format(persianCalendar.getTime()));

// year output in persian
SimpleDateFormat sdf1 = new SimpleDateFormat(SimpleDateFormat.YEAR, locale);
System.out.println(sdf1.format(persianCalendar.getTime()));

// month name output in persian
SimpleDateFormat sdf2 = new SimpleDateFormat(SimpleDateFormat.MONTH, locale);
System.out.println(sdf2.format(persianCalendar.getTime()));

// weekday name output in persian
SimpleDateFormat sdf3 = new SimpleDateFormat(SimpleDateFormat.WEEKDAY, locale);
System.out.println(sdf3.format(persianCalendar.getTime()));

// full date output in YY/MM/dd form
SimpleDateFormat sdf4 = new SimpleDateFormat("YY/MM/dd", locale);
System.out.println(sdf4.format(persianCalendar.getTime()));

Output:

ه‍.ش. ۱۳۹۵ تیر ۱۰, پنجشنبه  
۱۳۹۵  
تیر  
پنجشنبه
۹۵/۰۴/۱۰

If you need output to be in english, change new ULocale("fa_IR@calendar=persian") to new ULocale("@calendar=persian").

Output:

AP 1395 Tir 10, Thu
1395
Tir
Thu
95/04/10

Other nice things:

// Get number of days in month
System.out.println(persianCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 31

// Get first day of week
System.out.println(persianCalendar.getFirstDayOfWeek()); // 7 (Saturday according to docs)

// Add some amount of time
persianCalendar.add(Calendar.MONTH, 2);
System.out.println(persianCalendar.get(Calendar.YEAR));         //1395
System.out.println(persianCalendar.get(Calendar.MONTH));        // 5
System.out.println(persianCalendar.get(Calendar.DAY_OF_MONTH)); // 10

For other functionalities see icu4j demos, specially:

Also see Calendar and PersianCalendar API.

Java 8 onward:

In order to use java.time classes like ZonedDateTime or LocalDateTime, you could simply use this methods to convert a persian date to preferred classes:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.ULocale;

...

public static LocalDateTime fromPersianDateToLocalDateTime(int year, int month, int day, int hour, int minutes, int seconds) {
    return fromPersianDateToZonedDateTime(year, month, day, hour, minutes, seconds).toLocalDateTime();
}

public static ZonedDateTime fromPersianDateToZonedDateTime(int year, int month, int day, int hour, int minutes, int seconds) {
    return toZonedDateTime(fromPersianDate(year, month, day, hour, minutes, seconds));
}

public static long fromPersianDate(int year, int month, int day, int hour, int minutes, int seconds) {
    Calendar persianCalendar = Calendar.getInstance(new ULocale("fa_IR@calendar=persian"));
    persianCalendar.clear();
    persianCalendar.set(year, month, day, hour, minutes, seconds);
    return persianCalendar.getTimeInMillis();
}

public static ZonedDateTime toZonedDateTime(Long epochMilli) {
    if(epochMilli == null) return null;
    return Instant.ofEpochMilli(epochMilli).atZone(ZoneId.of("Asia/Tehran"));
}

Remarks About Jar Size

If you're concerned about icu4j's jar size , then you may rebuild it and just use the Calendar module (2,176KB). More Info: ver. 57 or earlier , ver. 58 or later using ICU Data Build Tool.

查看更多
唯我独甜
5楼-- · 2020-01-25 13:52

There is persianutils project which includes a bi-directional DateConverter; Gregorian <-> Persian (Jalali). It is written in Scala, so I suppose using it in a Java project would be quite easy.

The algorithm used is valid for Gregorian years up to ~3790 and Persian years up to ~3170.

DISCLAIMER: I am the author of PersianUtils

查看更多
Melony?
6楼-- · 2020-01-25 13:55

Beside Time4A, icu4j, which are too heavy libraries, I written a class to handle Persian calendar accurately and you can find it here: https://github.com/hadilq/java-persian-calendar/blob/master/persian/src/main/java/ir/hadilq/PersianCalendar.java

As you can find it in its tests, this class supports from year 3000 before hijra to 3000 after hijra.

查看更多
别忘想泡老子
7楼-- · 2020-01-25 13:58

I'm using this algorithm for years and it is very accurate between 1901 and 2099.

Use it and Enjoy! :)

public class Utilities {

    private class SolarCalendar {

        public String strWeekDay = "";
        public String strMonth = "";

        int date;
        int month;
        int year;

        public SolarCalendar()
        {
            Date MiladiDate = new Date();
            calcSolarCalendar(MiladiDate);
        }

        public SolarCalendar(Date MiladiDate)
        {
            calcSolarCalendar(MiladiDate);
        }

        private void calcSolarCalendar(Date MiladiDate) {

            int ld;

            int miladiYear = MiladiDate.getYear() + 1900;
            int miladiMonth = MiladiDate.getMonth() + 1;
            int miladiDate = MiladiDate.getDate();
            int WeekDay = MiladiDate.getDay();

            int[] buf1 = new int[12];
            int[] buf2 = new int[12];

            buf1[0] = 0;
            buf1[1] = 31;
            buf1[2] = 59;
            buf1[3] = 90;
            buf1[4] = 120;
            buf1[5] = 151;
            buf1[6] = 181;
            buf1[7] = 212;
            buf1[8] = 243;
            buf1[9] = 273;
            buf1[10] = 304;
            buf1[11] = 334;

            buf2[0] = 0;
            buf2[1] = 31;
            buf2[2] = 60;
            buf2[3] = 91;
            buf2[4] = 121;
            buf2[5] = 152;
            buf2[6] = 182;
            buf2[7] = 213;
            buf2[8] = 244;
            buf2[9] = 274;
            buf2[10] = 305;
            buf2[11] = 335;

            if ((miladiYear % 4) != 0) {
                date = buf1[miladiMonth - 1] + miladiDate;

                if (date > 79) {
                    date = date - 79;
                    if (date <= 186) {
                        switch (date % 31) {
                        case 0:
                            month = date / 31;
                            date = 31;
                            break;
                        default:
                            month = (date / 31) + 1;
                            date = (date % 31);
                            break;
                        }
                        year = miladiYear - 621;
                    } else {
                        date = date - 186;

                        switch (date % 30) {
                        case 0:
                            month = (date / 30) + 6;
                            date = 30;
                            break;
                        default:
                            month = (date / 30) + 7;
                            date = (date % 30);
                            break;
                        }
                        year = miladiYear - 621;
                    }
                } else {
                    if ((miladiYear > 1996) && (miladiYear % 4) == 1) {
                        ld = 11;
                    } else {
                        ld = 10;
                    }
                    date = date + ld;

                    switch (date % 30) {
                    case 0:
                        month = (date / 30) + 9;
                        date = 30;
                        break;
                    default:
                        month = (date / 30) + 10;
                        date = (date % 30);
                        break;
                    }
                    year = miladiYear - 622;
                }
            } else {
                date = buf2[miladiMonth - 1] + miladiDate;

                if (miladiYear >= 1996) {
                    ld = 79;
                } else {
                    ld = 80;
                }
                if (date > ld) {
                    date = date - ld;

                    if (date <= 186) {
                        switch (date % 31) {
                        case 0:
                            month = (date / 31);
                            date = 31;
                            break;
                        default:
                            month = (date / 31) + 1;
                            date = (date % 31);
                            break;
                        }
                        year = miladiYear - 621;
                    } else {
                        date = date - 186;

                        switch (date % 30) {
                        case 0:
                            month = (date / 30) + 6;
                            date = 30;
                            break;
                        default:
                            month = (date / 30) + 7;
                            date = (date % 30);
                            break;
                        }
                        year = miladiYear - 621;
                    }
                }

                else {
                    date = date + 10;

                    switch (date % 30) {
                    case 0:
                        month = (date / 30) + 9;
                        date = 30;
                        break;
                    default:
                        month = (date / 30) + 10;
                        date = (date % 30);
                        break;
                    }
                    year = miladiYear - 622;
                }

            }

            switch (month) {
            case 1:
                strMonth = "فروردين";
                break;
            case 2:
                strMonth = "ارديبهشت";
                break;
            case 3:
                strMonth = "خرداد";
                break;
            case 4:
                strMonth = "تير";
                break;
            case 5:
                strMonth = "مرداد";
                break;
            case 6:
                strMonth = "شهريور";
                break;
            case 7:
                strMonth = "مهر";
                break;
            case 8:
                strMonth = "آبان";
                break;
            case 9:
                strMonth = "آذر";
                break;
            case 10:
                strMonth = "دي";
                break;
            case 11:
                strMonth = "بهمن";
                break;
            case 12:
                strMonth = "اسفند";
                break;
            }

            switch (WeekDay) {

            case 0:
                strWeekDay = "يکشنبه";
                break;
            case 1:
                strWeekDay = "دوشنبه";
                break;
            case 2:
                strWeekDay = "سه شنبه";
                break;
            case 3:
                strWeekDay = "چهارشنبه";
                break;
            case 4:
                strWeekDay = "پنج شنبه";
                break;
            case 5:
                strWeekDay = "جمعه";
                break;
            case 6:
                strWeekDay = "شنبه";
                break;
            }

        }

    }

    public static String getCurrentShamsidate() {
        Locale loc = new Locale("en_US");
        Utilities util = new Utilities();
        SolarCalendar sc = util.new SolarCalendar();
        return String.valueOf(sc.year) + "/" + String.format(loc, "%02d",
                sc.month) + "/" + String.format(loc, "%02d", sc.date);
    }
}
查看更多
登录 后发表回答