Beruflich Dokumente
Kultur Dokumente
Questions
Tags
Users
sign up
Badges
Unanswered
log in
Ask
java
date
gregorian-calendar
date-conversion
hijri
I want to convert from Gregorian to Hijri(Islamic) date and I need a java class for this converting. I want to give it an Gregorian date in format
of "yyyy/mm/dd" as string and it give me the Hijri date in the same format. can anyone help me?
share
Asked
Mar 31 '13 at 11:04
Have you tried something? Have you faced sny particular problem? araknoid Mar 31 '13 at 11:06
I tried some codes found by searching but non of them didn't work probably. anony Apr 4 '13 at 8:03
add a comment
4 Answers
21
order by votes
Firstly, separate out the conversion part from the formatting/parsing part. You can deal with those easily later - and there are lots of
questions on Stack Overflow about that.
Personally I'd use Joda Time, which typically makes life much simpler. For example:
import org.joda.time.Chronology;
import org.joda.time.LocalDate;
import org.joda.time.chrono.IslamicChronology;
import org.joda.time.chrono.ISOChronology;
public class Test {
public static void main(String[] args) throws Exception {
Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
(It feels like there should be a cleaner way of converting dates between chronologies, but I couldn't find one immediately.)
share
6155 7203
Answered
Mar 31 '13 at 11:24
Edited
Nov 25 '14 at 6:46
+1 I like your way better, short code. On the one I posted is just the way how it works knowbody Mar 31 '13 at 11:30
+knowbody I don't know why but this code bring me 1434-05-18 not 1434-05-19? how can I fix this problem? anony Apr 4 '13 at 11:09
@anony: There are two different possiblities here - one is that you're expecting the wrong epoch (there are astronomical and civil epochs) and the other is
that you need to use an IslamicChronology with a different leap year pattern. Look at the docs for IslamicChronology for details. Jon Skeet Apr 4 '13 at
11:13
@Jon Skeet, how to convert from hijri to georgian ? and can we set time to LocalDate object ? MahmoudS Nov 7 '13 at 11:14
@MahmoudSaleh: A LocalDate doesn't have a time - it's just a date. You can add a LocalTime to it to get a LocalDateTime though. Not sure the best way to
convert in Joda Time - probably use the LocalDate(Object, Chronology) constructor. Jon Skeet Nov 7 '13 at 11:20
add a comment
Snippet :
import java.time.*;
import java.time.chrono.HijrahChronology;
Date date=new Date(); ///
Calendar cl=Calendar.getInstance();
cl.setTime(date);
HijrahDate islamyDate=HijrahChronology.INSTANCE.date(LocalDate.of(cl.get(Calendar.YEAR),cl.get(Calendar.MONTH)+1, cl.get(Calendar.DATE)));
//OUTPUT: Hijrah-umalqura AH 1436-02-03
share
Answered
Nov 25 '14 at 7:24
Edited
Feb 11 at 8:50
just use Google for example here copied from the link given:
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){
share
10 41
I think I'd rather use an existing library than have to include all of that code in my own project :) Jon Skeet Mar 31 '13 at 11:28
I would do the same, here you can see how it works ;) knowbody Mar 31 '13 at 11:30
Answered
Mar 31 '13 at 11:10
it don't work right.it bring similar dates for some days. for ex. I have changed it's input to get string and it made same date for "2014/1/29" and "2014/2/1".
however I prefer to use a library than all this code. thank you for your help ;) anony Apr 4 '13 at 8:10
It is not Kuwaiti calendar . it is an islamic Clandar . Abdennour TOUMI Nov 25 '14 at 5:34
it's not correct Muhammad Dyas Yaskur May 10 '15 at 22:44
share
Using this calendar you can convert from Umm Al-Qura to Gregorian and vice versa, and also you can use java.text.SimpleDateFormat to
format dates.
Your Answer
log in
or
Name
Answered
Apr 3 '15 at 22:09
By posting your answer, you agree to the privacy policy and terms of service.
meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc