Search code examples
androidandroid-adapterandroid-jsonandroid-date

How to convert Date in Android


I want show date into my application, and i receive this data from server with below json :

"date": "2016-08-01 19:55:16"

and i set into textview with below code :

((DataViewHolder) holder).main_dateTime.setText(Html.fromHtml(mDateSet.get(position).getDate()));

I want convert this date to jalali/shamsi . but i don't know how to convert this date and set into textview !

Can you help me for this issue?


Solution

  • Use this https://github.com/amirmehdizadeh/JalaliCalendar to get the Jalali date

    first get the year, month and day from your date like

    String date = "2016-08-01 19:55:16";
    
    String[] parts = date.split(" ");
    String datePart = parts[0];
    String timePart = parts[1];
    
    int year;
    int month;
    int day;
    
    String[] dateParts = datePart.split("-");
    year = Integer.parseInt( dateParts[0]);
    month = Integer.parseInt( dateParts[1]);
    day = Integer.parseInt( dateParts[2]);
    

    then create the Object to pass to that library

    JalaliCalendar.YearMonthDate  georgianDate = new JalaliCalendar.YearMonthDate(year,month,day);
    

    and then call its method that convert from Georgian date to Jalali Date

     JalaliCalendar.YearMonthDate JalaliDate = JalaliCalendar.gregorianToJalali(georgianDate);
    

    And Finally append the date with time to show in text view

    String jalaliDateTime =  JalaliDate.toString() + " " + timePart;
    textView.setText(jalaliDateTime);