Converting a Gregorian to a Persian date with a Library

I want to use persian date in my android App im using this Library

But i have problem with formating date for example

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd");
        Calendar calendar = new GregorianCalendar(2017,9,14);


        System.out.println(sdf.format(calendar.getTime()));

In the code above my result is : 2017/9/14

How to show the persian date with the library I said

Upvotes: 1

Views: 4662

Answers (2)

Ali Maddi
Ali Maddi

Reputation: 339

PersianDate is a great library for the Persian calendar. It has a lot of facility for format date and time. you can use it by this Gradle dependency

dependencies
{
    implementation 'com.github.samanzamani.persiandate:PersianDate:0.8'
}

for accessing to update dependency check this link

for example :

PersianDate pdate = new PersianDate();
PersianDateFormat pdformater1 = new PersianDateFormat('Y/m/d');
pdformater1.format(pdate);//1396/05/20

Update Feb-2025

Seems version 0.8 got some bugs, you can try newer version:

implementation 'com.github.samanzamani:PersianDate:1.7.1'

Upvotes: 2

masoud vali
masoud vali

Reputation: 1536

call the function like this

PersianDate pdate = new PersianDate(Calendar.getInstance(());
PersianDateFormat pdformater1 = new PersianDateFormat('Y/m/d');
pdformater1.format(pdate);

and if you want to parse a string date use this:

    PersianDateFormat pdformater1 = new PersianDateFormat("yyyy/MM/dd");
    try {
        PersianDate persianDate = pdformater1.parse("1397/06/21");
        Log.e("DATE", "date " + persianDate.getShYear() + "-" + persianDate.getShMonth() + "-" + persianDate.getShDay());
    } catch (ParseException e) {
        e.printStackTrace();
    }

Upvotes: 2

Related Questions