samanzamani / PersianDate

Persian date for android
BSD 3-Clause "New" or "Revised" License
326 stars 40 forks source link

problem in Converting persianDateString to Epoch #69

Closed Rezakeshavarz1376 closed 2 years ago

Rezakeshavarz1376 commented 2 years ago

Hi Dear i'm using persianDate Library in my Project i want to convert PersianDate String To epoch but the result is negative Value while the pattern of simpleDateFormat for Converting string to persianDate is same as same pattern of persianDate To String below is my code:

    PersianDateFormat persianDateFormat = new PersianDateFormat("Y/m/d");
    String strDate=persianDateFormat.format(new PersianDate(new Date()));

    try {
        Long aLong=persianDateFormat.parseGrg(strDate).toDate().getTime();
        Log.i("getBirthDate",strDate+" -> "+aLong);
    } catch (ParseException e) {
        e.printStackTrace();
        Log.i("getBirthDate",strDate+" -> "+e.getMessage());

    }

I also used below codes to converting persianDate to epoch but result is also negative

persianDateFormat.parse(strDate).toDate().getTime(); persianDateFormat.parseGrg(strDate).getTime(); persianDateFormat.parse(strDate).getTime();

and i also pass pattern to parse method but result also is negative

note: I also use below pattern for simpledateformat but result is also negative : 'YYYY/MM/dd'

note 2: this is sample of pattern of my persianDateString : 1401/04/13

this is one of result in logcat : getBirthDate: 1401/04/13 -> -17955663960000

How Can I use This library for converting PersianDate String to Java Date and also persianDate String to epoch Thank's for you'r help

samanzamani commented 2 years ago

Hi @Rezakeshavarz1376 If I understand your question correctly you want to convert a String date like 1401/04/17 to PersianDate and Date class and get the timestamps from the desired date. You can use this code:

PersianDateFormat persianDateFormat = new PersianDateFormat("yyyy/MM/dd");
try{
    PersianDate pDate = persianDateFormat.parse("1401/04/17");
    Log.i("PersianDate","" + pDate.toString());//جمعه 17 تیر 1401
    Date jDate = pDate.toDate();
    long timestamps = pDate.getTime();
} catch (ParseException e) {
    e.printStackTrace();
}

Actually, the problem is you used YYYY and y instead yyyy in your code. For more information, you can check the documentation file.

Rezakeshavarz1376 commented 2 years ago

Hi Yes that was my problem exactly And the problem was solved Thank's for you'r help🙏