msarhan / ummalqura-calendar

Implementation of java.util.Calendar for the Umm Al-Qura calendar system.
MIT License
154 stars 51 forks source link

formatter outputs Gregorian months on Android #10

Closed flibbertigibbet closed 6 years ago

flibbertigibbet commented 7 years ago

Running the examples in the README outputs Gregorian months from a SimpleDateFormatter. Compiling for Android with target API 23.

Calendar cal = new UmmalquraCalendar(1433, UmmalquraCalendar.RABI_AWWAL, 8, 20, 45, 10);
SimpleDateFormat sdf = new SimpleDateFormat("MMMM", Locale.ENGLISH);
sdf.setCalendar(cal);
Log.d(LOG_LABEL, "calendar month: " + sdf.getCalendar().getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH));
Log.d(LOG_LABEL, "formatted month: " + sdf.format(cal.getTime()));
Log.d(LOG_LABEL, "calendar getTime: " + cal.getTime());
sdf.applyPattern("EEEE d MMMM, y");
Log.d(LOG_LABEL, "formatted full date: " + sdf.format(cal.getTime()));
sdf.applyPattern("EEEE d MMM, y");
Log.d(LOG_LABEL, "with shortened month: " + sdf.format(cal.getTime()));
sdf.applyPattern("y/MM/dd hh:mm a");
Log.d(LOG_LABEL, "short form: " + sdf.format(cal.getTime()));

outputs:

calendar month: Rabi' al-Awwal
formatted month: March
calendar getTime: Tue Jan 31 20:45:10 EST 2012
formatted full date: Tuesday 8 March, 1433
with shortened month: Tuesday 8 Mar, 1433
short form: 1433/03/08 08:45 PM

Initializing instead with the current date and time: Calendar cal = UmmalquraCalendar.getInstance(); outputs in all Gregorian:

calendar month: June
formatted month: June
calendar getTime: Tue Jun 21 11:49:18 EDT 2016
formatted full date: Tuesday 21 June, 2016
with shortened month: Tuesday 21 Jun, 2016
short form: 2016/06/21 11:49 AM

Thanks for building this project!

flibbertigibbet commented 7 years ago

Update: I've gotten this to work on Android by explicitly setting the date format symbols. This required making the class UmmalquraDateFormatSymbols public.

SimpleDateFormat dateFormat = new SimpleDateFormat(formatString, locale);
dateFormat.setCalendar(cal);
UmmalquraDateFormatSymbols ummalquara = new UmmalquraDateFormatSymbols();
DateFormatSymbols symbols = new DateFormatSymbols(locale);
symbols.setMonths(ummalquara.getMonths());
symbols.setShortMonths(ummalquara.getShortMonths());
dateFormat.setDateFormatSymbols(symbols);

outputs:

calendar getTime: Tue Jun 21 14:51:12 EDT 2016
formatted full date: Tuesday 16 Ramadhan, 1437
PHP4PRO commented 7 years ago

Thank you for your workaround but it doesn't work with me until i add " locale " on this line UmmalquraDateFormatSymbols ummalquara = new UmmalquraDateFormatSymbols(locale);

Regards

msarhan commented 6 years ago

I think this is related to Android implementation as it's working fine on JDK. Please check:

com.github.msarhan.ummalqura.calendar.UmmalquraDateFormatTests::formatDate(Calendar cal, Locale l, String fmt)