Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem in Converting persianDateString to Epoch #69

Closed
Rezakeshavarz1376 opened this issue Jul 4, 2022 · 2 comments
Closed

problem in Converting persianDateString to Epoch #69

Rezakeshavarz1376 opened this issue Jul 4, 2022 · 2 comments
Labels

Comments

@Rezakeshavarz1376
Copy link

Rezakeshavarz1376 commented Jul 4, 2022

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
Copy link
Owner

samanzamani commented Jul 8, 2022

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.

samanzamani added a commit that referenced this issue Jul 8, 2022
@Rezakeshavarz1376
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants