Gregorian to Hijri (Islamic) date - android

Is there any way or library in Android to convert Gregorian date to Hijri(Islamic) one? I found some Java libraries such as Joda Time but I need something which works in Android.

I found this but I didn't test it yet
public class DateHijri {
static double gmod(double n, double m) {
return ((n % m) + m) % m;
}
static double[] kuwaiticalendar(boolean adjust) {
Calendar today = Calendar.getInstance();
int adj = 0;
if (adjust) {
adj = 0;
} else {
adj = 1;
}
if (adjust) {
int adjustmili = 1000 * 60 * 60 * 24 * adj;
long todaymili = today.getTimeInMillis() + adjustmili;
today.setTimeInMillis(todaymili);
}
double day = today.get(Calendar.DAY_OF_MONTH);
double month = today.get(Calendar.MONTH);
double year = today.get(Calendar.YEAR);
double m = month + 1;
double y = year;
if (m < 3) {
y -= 1;
m += 12;
}
double a = Math.floor(y / 100.);
double b = 2 - a + Math.floor(a / 4.);
if (y < 1583)
b = 0;
if (y == 1582) {
if (m > 10)
b = -10;
if (m == 10) {
b = 0;
if (day > 4)
b = -10;
}
}
double jd = Math.floor(365.25 * (y + 4716))
+ Math.floor(30.6001 * (m + 1)) + day + b - 1524;
b = 0;
if (jd > 2299160) {
a = Math.floor((jd - 1867216.25) / 36524.25);
b = 1 + a - Math.floor(a / 4.);
}
double bb = jd + b + 1524;
double cc = Math.floor((bb - 122.1) / 365.25);
double dd = Math.floor(365.25 * cc);
double ee = Math.floor((bb - dd) / 30.6001);
day = (bb - dd) - Math.floor(30.6001 * ee);
month = ee - 1;
if (ee > 13) {
cc += 1;
month = ee - 13;
}
year = cc - 4716;
double wd = gmod(jd + 1, 7) + 1;
double iyear = 10631. / 30.;
double epochastro = 1948084;
double epochcivil = 1948085;
double shift1 = 8.01 / 60.;
double z = jd - epochastro;
double cyc = Math.floor(z / 10631.);
z = z - 10631 * cyc;
double j = Math.floor((z - shift1) / iyear);
double iy = 30 * cyc + j;
z = z - Math.floor(j * iyear + shift1);
double im = Math.floor((z + 28.5001) / 29.5);
if (im == 13)
im = 12;
double id = z - Math.floor(29.5001 * im - 29);
double[] myRes = new double[8];
myRes[0] = day; // calculated day (CE)
myRes[1] = month - 1; // calculated month (CE)
myRes[2] = year; // calculated year (CE)
myRes[3] = jd - 1; // julian day number
myRes[4] = wd - 1; // weekday number
myRes[5] = id; // islamic date
myRes[6] = im - 1; // islamic month
myRes[7] = iy; // islamic year
return myRes;
}
static String writeIslamicDate() {
String[] wdNames = { "Ahad", "Ithnin", "Thulatha", "Arbaa", "Khams",
"Jumuah", "Sabt" };
String[] iMonthNames = { "Muharram", "Safar", "Rabi'ul Awwal",
"Rabi'ul Akhir", "Jumadal Ula", "Jumadal Akhira", "Rajab",
"Sha'ban", "Ramadan", "Shawwal", "Dhul Qa'ada", "Dhul Hijja" };
// This Value is used to give the correct day +- 1 day
boolean dayTest = true;
double[] iDate = kuwaiticalendar(dayTest);
String outputIslamicDate = wdNames[(int) iDate[4]] + ", " + iDate[5]
+ " " + iMonthNames[(int) iDate[6]] + " " + iDate[7] + " AH";
return outputIslamicDate;
}
}

You can use this Class in your Project (Hope it helps) :
import java.text.SimpleDateFormat;
import java.util.Date;
public class PersianDate {
public String todayShamsi()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String curentDateandTime = sdf.format(new Date());
String year = curentDateandTime.substring(0, 4);
String month = curentDateandTime.substring(4, 6);
String day = curentDateandTime.substring(6, 8);
int Y = Integer.valueOf(year);
int M = Integer.valueOf(month);
int D = Integer.valueOf(day);
return Shamsi(Y, M, D);
}
public static String Shamsi(int Y, int M, int D)
{
if (Y == 0)
Y = 2000;
if (Y < 100)
Y = Y + 1900;
if (Y == 2000)
{
if (M > 2)
{
SimpleDateFormat temp = new SimpleDateFormat("yyyyMMdd");
String curentDateandTime = temp.format(new Date());
String year = curentDateandTime.substring(0, 4);
String month = curentDateandTime.substring(4, 6);
String day = curentDateandTime.substring(6, 8);
Y = Integer.valueOf(year);
M = Integer.valueOf(month);
D = Integer.valueOf(day);
}
}
if (M < 3 || (M == 3 && D < 21))
{
Y -= 622;
}
else Y -= 621;
switch (M)
{
case 1: if (D < 21)
{
M = 10;
D = D + 10;
}
else
{
M = 11;
D -= 20;
}
break;
case 2: if (D < 20)
{
M = 11;
D = D + 11;
}
else
{
M = 12;
D -= 19;
}
break;
case 3:
if (D < 21)
{
M = 12;
D = D + 9;
}
else
{
M = 1;
D -= 20;
}
break;
case 4:
if (D < 21)
{
M = 1;
D = D + 11;
}
else
{
M = 2; D = D - 20;
}
break;
case 5:
if (D < 22)
{
M = M - 3;
D = D + 10;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 6:
if (D < 22)
{
M = M - 3;
D = D + 10;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 7:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 8:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 9:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 10:
if (D < 23)
{
M = 7;
D = D + 8;
}
else
{
M = 8;
D = D - 22;
}
break;
case 11:
if (D < 22)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 12:
if (D < 22)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 21;
}
break;
}
String month = "00";
String day = "00";
//D = Integer.valueOf(D)+1;
if (M < 10)
{
month = "0" + M;
}
else
{
month = String.valueOf(M);
}
if (D < 10)
{
day = "0" + D;
}
else
{
day = String.valueOf(D);
}
return String.valueOf(Y) + "/" + month + "/" + day;
}
}

Related

interpolate a given array to be in new lenght

in order to interpolate 2 values, I can use
lerp(int a, int b) {
return (a + b) / 2;
}
Now imagine I've an array(1, 30, 100, 300) and I want to interpolate it to array in size N (N=10 for example).
If N == 7, then:
1,15,30,65,100,200,300
I've no idea how to interpolate 4 values to be 10. I need a method that looks like:
interpolate(fina int[] input, final int newSize) {
int[] res = new int[newSize];
...
return res;
}
that works even on my example above with newSize of 7, 10 or whatever.
Any idea how to implement it?
SOLVED.
public static double[] interpolate(double[] x, int newLength) {
double[] y = null;
if (newLength > 0) {
int N = x.length;
if (N == 1) {
y = new double[1];
y[0] = x[0];
return y;
} else if (newLength == 1) {
y = new double[1];
int ind = (int) Math.floor(N * 0.5 + 0.5);
ind = Math.max(1, ind);
ind = Math.min(ind, N);
y[0] = x[ind - 1];
return y;
} else {
y = new double[newLength];
double Beta = ((double) newLength) / N;
double newBeta = 1.0;
if (newLength > 2)
newBeta = (N - 2.0) / (newLength - 2.0);
y[0] = x[0];
y[1] = x[1];
y[newLength - 1] = x[N - 1];
double tmp, alpha;
int i, j;
for (i = 2; i <= newLength - 2; i++) {
tmp = 1.0 + (i - 1) * newBeta;
j = (int) Math.floor(tmp);
alpha = tmp - j;
y[i] = (1.0 - alpha) * x[Math.max(0, j)] + alpha * x[Math.min(N - 1, j + 1)];
}
}
}
return y;
}
/**
* Find the maximum of all elements in the array, ignoring elements that are NaN.
* #param data
* #return
*/
public static double max(double[] data) {
double max = Double.NaN;
for (int i = 0; i < data.length; i++) {
if (Double.isNaN(data[i]))
continue;
if (Double.isNaN(max) || data[i] > max)
max = data[i];
}
return max;
}
public static int max(int[] data) {
int max = data[0];
for (int i = 1; i < data.length; i++) {
if (data[i] > max)
max = data[i];
}
return max;
}

How can i repeat for-loop after a fixed limit over?

I have a for-loop code:
try
{
outputStream = new FileOutputStream("/sdcard/output1.txt");
Writer out = new OutputStreamWriter(outputStream);
Point point;
for (int i = 48; i <= 66; i++)
{
point = landmarks.get(i);
int pointX = (int) (point.x * resizeRatio);
int pointY = (int) (point.y * resizeRatio);
Log.d(TAG, "My points:(" + pointX + "," + pointY + ")");
point = landmarks.get(i + 1);
int pointXX = (int) (point.x * resizeRatio);
int pointYY = (int) (point.y * resizeRatio);
canvas.drawLine(pointX, pointY, pointXX, pointYY,mFaceLandmardkPaint);
out.write(Integer.toString(pointX));
String h = ",";
out.write(h);
out.write(Integer.toString(pointY));
String j = ",";
out.write(j);
}
out.write("\n");
out.close();
}
In this i need to start from 48 limit after the first 66 limit gets over.How can i again starts from 48th limit???
use recursion.
create a method to execute your forloop and at the end of loop call the method again.
like
if(i==65){
callMethodAgain();
}
and to break the loop store a temp variable to do the count ex.
if(i==65&& temp<2){
temp++;
callMethodAgain();
}
Try following code :
for(int j=0; j<2; j++){
for (int i = 48; i <= 66; i++)
{
if(j == 0){
point = landmarks.get(i);
int pointX = (int) (point.x * resizeRatio);
int pointY = (int) (point.y * resizeRatio);
Log.d(TAG, "My points:(" + pointX + "," + pointY + ")");
point = landmarks.get(i + 1);
int pointXX = (int) (point.x * resizeRatio);
int pointYY = (int) (point.y * resizeRatio);
canvas.drawLine(pointX, pointY, pointXX, pointYY,mFaceLandmardkPaint);
out.write(Integer.toString(pointX));
String h = ",";
out.write(h);
out.write(Integer.toString(pointY));
String j = ",";
out.write(j);
}
else{
// do your stuff
}
}
}

how to convert string timestamp from gregorian to shamsi

hello i'am getting a timestamp from server in gregorian what look likes this
{
"error": false,
"users": [
{
"id": 1,
"username": "dfsg",
"active": 1,
"created_at": "2017-02-14 00:13:25"
},
{
"id": 2,
"username": "asf",
"active": 1,
"created_at": "2017-02-14 13:42:26"
},
{
"id": 3,
"username": "test2",
"active": 1,
"created_at": "2017-02-15 13:57:31"
}
]
}
and i want to change it to shamsi what should look like this
1395-11-26 13:42:26
is there any class to do it ? thanks
you could add this class in your project, source
import java.text.SimpleDateFormat;
import java.util.Date;
public class PersianDate {
public String todayShamsi()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String curentDateandTime = sdf.format(new Date());
String year = curentDateandTime.substring(0, 4);
String month = curentDateandTime.substring(4, 6);
String day = curentDateandTime.substring(6, 8);
int Y = Integer.valueOf(year);
int M = Integer.valueOf(month);
int D = Integer.valueOf(day);
return Shamsi(Y, M, D);
}
public static String Shamsi(int Y, int M, int D)
{
if (Y == 0)
Y = 2000;
if (Y < 100)
Y = Y + 1900;
if (Y == 2000)
{
if (M > 2)
{
SimpleDateFormat temp = new SimpleDateFormat("yyyyMMdd");
String curentDateandTime = temp.format(new Date());
String year = curentDateandTime.substring(0, 4);
String month = curentDateandTime.substring(4, 6);
String day = curentDateandTime.substring(6, 8);
Y = Integer.valueOf(year);
M = Integer.valueOf(month);
D = Integer.valueOf(day);
}
}
if (M < 3 || (M == 3 && D < 21))
{
Y -= 622;
}
else Y -= 621;
switch (M)
{
case 1: if (D < 21)
{
M = 10;
D = D + 10;
}
else
{
M = 11;
D -= 20;
}
break;
case 2: if (D < 20)
{
M = 11;
D = D + 11;
}
else
{
M = 12;
D -= 19;
}
break;
case 3:
if (D < 21)
{
M = 12;
D = D + 9;
}
else
{
M = 1;
D -= 20;
}
break;
case 4:
if (D < 21)
{
M = 1;
D = D + 11;
}
else
{
M = 2; D = D - 20;
}
break;
case 5:
if (D < 22)
{
M = M - 3;
D = D + 10;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 6:
if (D < 22)
{
M = M - 3;
D = D + 10;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 7:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 8:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 9:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 10:
if (D < 23)
{
M = 7;
D = D + 8;
}
else
{
M = 8;
D = D - 22;
}
break;
case 11:
if (D < 22)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 12:
if (D < 22)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 21;
}
break;
}
String month = "00";
String day = "00";
//D = Integer.valueOf(D)+1;
if (M < 10)
{
month = "0" + M;
}
else
{
month = String.valueOf(M);
}
if (D < 10)
{
day = "0" + D;
}
else
{
day = String.valueOf(D);
}
return String.valueOf(Y) + "/" + month + "/" + day;
}
}
Use Joda Time
As such:
Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
LocalDate todayIso = new LocalDate(2013, 3, 31, iso);
LocalDate todayHijri = new LocalDate(todayIso.toDateTimeAtStartOfDay(),
hijri);
System.out.println(todayHijri); // 1434-05-19
To get the year, month and day from your string use the following:
String startDateString = "2017-02-14 00:13:25";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = df.parse(startDateString);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
//Add one to month {0 - 11}
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);

convert gregorian Date to hijri date is back wrong days

I want to convert from Gregorian to Hijri(Islamic) date and I use this class
the problem is islamic month's days is <29 , 30 , 29 , 30 else
but this class month's days is < 30, 29, 30, 29 else
any body can help fix this?
import java.util.Calendar;
/**
* Gregorian-Hijri Dates Converter
*
*
* This Code is used to convert Gregorian dates to Hijri Dates
*
*
*/
public class DateHigri {
static double gmod(double n,double m) {
return ((n % m) + m) % m;
}
static double[] kuwaiticalendar(boolean adjust) {
Calendar today = Calendar.getInstance();
int adj=0;
if(adjust){
adj=0;
}else{
adj=1;
}
if (adjust) {
int adjustmili = 1000 * 60 * 60 * 24 * adj;
long todaymili = today.getTimeInMillis() + adjustmili;
today.setTimeInMillis(todaymili);
}
double day = today.get(Calendar.DAY_OF_MONTH);
double month = today.get(Calendar.MONTH);
double year = today.get(Calendar.YEAR);
double m = month + 1;
double y = year;
if (m < 3) {
y -= 1;
m += 12;
}
double a = Math.floor(y / 100.);
double b = 2 - a + Math.floor(a / 4.);
if (y < 1583)
b = 0;
if (y == 1582) {
if (m > 10)
b = -10;
if (m == 10) {
b = 0;
if (day > 4)
b = -10;
}
}
double jd = Math.floor(365.25 * (y + 4716)) + Math.floor(30.6001 * (m + 1)) + day
+ b - 1524;
b = 0;
if (jd > 2299160) {
a = Math.floor((jd - 1867216.25) / 36524.25);
b = 1 + a - Math.floor(a / 4.);
}
double bb = jd + b + 1524;
double cc = Math.floor((bb - 122.1) / 365.25);
double dd = Math.floor(365.25 * cc);
double ee = Math.floor((bb - dd) / 30.6001);
day = (bb - dd) - Math.floor(30.6001 * ee);
month = ee - 1;
if (ee > 13) {
cc += 1;
month = ee - 13;
}
year = cc - 4716;
double wd = gmod(jd + 1, 7) + 1;
double iyear = 10631. / 30.;
double epochastro = 1948084;
double epochcivil = 1948085;
double shift1 = 8.01 / 60.;
double z = jd - epochastro;
double cyc = Math.floor(z / 10631.);
z = z - 10631 * cyc;
double j = Math.floor((z - shift1) / iyear);
double iy = 30 * cyc + j;
z = z - Math.floor(j * iyear + shift1);
double im = Math.floor((z + 28.5001) / 29.5);
if (im == 13)
im = 12;
double id = z - Math.floor(29.5001 * im - 29);
double[] myRes = new double[8];
myRes[0] = day; // calculated day (CE)
myRes[1] = month - 1; // calculated month (CE)
myRes[2] = year; // calculated year (CE)
myRes[3] = jd - 1; // julian day number
myRes[4] = wd - 1; // weekday number
myRes[5] = id; // islamic date
myRes[6] = im - 1; // islamic month
myRes[7] = iy; // islamic year
return myRes;
}
static String writeIslamicDate() {
String[] wdNames = {"Ahad", "Ithnin", "Thulatha", "Arbaa", "Khams",
"Jumuah", "Sabt"};
String[] iMonthNames = {"Muharram", "Safar", "Rabi'ul Awwal",
"Rabi'ul Akhir", "Jumadal Ula", "Jumadal Akhira", "Rajab",
"Sha'ban", "Ramadan", "Shawwal", "Dhul Qa'ada", "Dhul Hijja"};
// This Value is used to give the correct day +- 1 day
boolean dayTest=true;
double[] iDate = kuwaiticalendar(dayTest);
String outputIslamicDate = wdNames[(int) iDate[4]] + ", " + iDate[5] + " "
+ iMonthNames[(int) iDate[6]] + " " + iDate[7] + " AH";
return outputIslamicDate;
}
}

Android Renderscript String Functions?

Are there any string functions in Renderscript? Like vsprintf, for example?
Specifically, I'd like to convert a float to a string. Do I have to write that from scratch?
Thanks!
Sorry, here's a better one. It'll work for integers as well but they have ".000" added on.
char stringBuffer[50];
static const int MAX_STRING_LENGTH = sizeof(stringBuffer) - 1;
void drawFloat(float value, int x, int y) {
int index = 0;
int scaledValue = (int)(value * 1000);
index = MAX_STRING_LENGTH;
stringBuffer[index] = 0;
while(scaledValue > 0 || index > MAX_STRING_LENGTH - 4) {
index--;
if(index == MAX_STRING_LENGTH - 4) {
stringBuffer[index--] = '.';
}
int digitValue = scaledValue % 10;
stringBuffer[index] = '0' + digitValue;
scaledValue /= 10;
}
if(value < 0) {
stringBuffer[index--] = '-';
}
rsgDrawText(&stringBuffer[index], x - 10, y + 5);
}
Couldn't find a simple way so...
void drawInteger(int value, int x, int y) {
char text[50] = "0";
int index = 0;
if(value != 0) {
index = 49;
text[index] = 0;
while(value > 0) {
index--;
int digitValue = value % 10;
text[index] = '0' + digitValue;
value /= 10;
}
if(value < 0) {
text[index--] = '-';
}
}
rsgDrawText(&text[index], x - 10, y + 5);
}
void drawFloat(float value, int x, int y) {
char text[50] = "0.000";
int index = 0;
if(value != 0) {
int integerPart = (int)(value * 1000);
index = 49;
text[index] = 0;
while(integerPart > 0) {
index--;
if(index == 45) {
text[index--] = '.';
}
int digitValue = integerPart % 10;
text[index] = '0' + digitValue;
integerPart /= 10;
}
if(value < 0) {
text[index--] = '-';
}
}
rsgDrawText(&text[index], x - 10, y + 5);
}

Categories

Resources