Last active
August 11, 2017 18:39
-
-
Save akmalxxx/9542675 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Makkah Calendar | |
http://www.makkahcalendar.org/en/hijriCalendar.php | |
Usage: String today = MakkahCalendar.getSimpleDate(null); | |
*/ | |
import java.util.Calendar; | |
public final class MakkahCalendar { | |
public final static String[] WEEKDAYS = { 0, "Ahad", "Ithnain", "Thulatha", "Arba'a", "Khamsa", "Jumu'ah", "Sabt" }; | |
public final static String[] MONTHS = { 0, "Muharram", "Safar", "Rabi'ul awwal", "Rabi'ul akhir", | |
"Jumadal ula", "Jumadal akhira", "Rajab", "Sha'ban", "Ramadhan", "Shawwal", "Dhul qa'ada", "Dhul hijja" }; | |
private final static int START_YEAR = 2009; //1 muharram 1431 = 18 dec 2009 | |
private final static int START_MONTH = Calendar.DECEMBER; | |
private final static int START_DAY = 18; | |
private final static int START_HIJRIYEAR = 1431; | |
private final static int[] data = { | |
30,29,30,30,29,30,29,29,30,29,29,30, //1431 | |
30,29,30,30,30,29,30,29,29,30,29,30, //1432 | |
29,29,30,30,30,29,30,30,29,29,30,29, //1433 | |
29,30,29,30,30,29,30,30,29,30,29,30, //1434 | |
29,29,30,29,30,30,29,30,29,30,30,29 //1435 | |
}; | |
public static int[] getDate(Calendar cal) { | |
//get startdate calendar | |
Calendar calS = Calendar.getInstance(); | |
calS.clear(); | |
calS.set(START_YEAR, START_MONTH, START_DAY); | |
//today | |
if (cal == null) cal = Calendar.getInstance(); | |
int weekday = cal.get(Calendar.DAY_OF_WEEK); | |
//total days since startdate | |
long diff = cal.getTimeInMillis() - calS.getTimeInMillis(); | |
int elapse = (int)(diff / (24 * 60 * 60 * 1000)); //includes start day | |
if (elapse < 0) return fallback(cal); | |
int year = START_HIJRIYEAR; | |
int month = 1; | |
int day = 1; | |
int total = 0; | |
boolean inRange = false; | |
for (int i=0; i<data.length; i++) { | |
if (elapse < total+data[i]) { | |
day = elapse - total; | |
inRange = true; | |
break; | |
} | |
total += data[i]; | |
month++; | |
if (month > 12) { month = 1; year++; } | |
} | |
if (!inRange) return fallback(cal); | |
return new int[]{ weekday, day, month, year }; | |
} | |
public static int[] fallback(Calendar cal) { | |
return null; //or fallback to other methods | |
} | |
public static String getSimpleDate(Calendar cal) { | |
int[] dt = getDate(cal); | |
if (dt == null) return "Date not in range"; | |
return WEEKDAYS[dt[0]] + ", " + dt[1] + " " + MONTHS[dt[2]] + " " + dt[3] + "H"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment