Geeks With Blogs
Bill Osuch - Random geek notes

The two code blocks below will calculate the Julian date from a standard calendar date/time. Note that they assume you're passing UT time rather than time that's been offset for your local timezone. The formula used is based on the one in Jean Meeus' Astronomical Algorithms.

You may want to add some rounding to the output to limit the number of digits after the decimal point, depending on what precision you can live with. For 5/13/2011 at 14:30:00, the Android version returns 2455695.1041666665; you could probably live with just 2455695.10417.

C# version:

public static double GetJulianDate(DateTime calendarDate)
{
   int year = calendarDate.Year;
   int month = calendarDate.Month;
   int day = calendarDate.Day;
   double hour = calendarDate.Hour;
   double minute = calendarDate.Minute;
   double second = calendarDate.Second;
   int isGregorianCal = 1;
   int A;
   int B;
   int C;
   int D;
   double fraction = day + ((hour + (minute / 60) + (second / 60 / 60)) / 24);

   if (year < 1582)
   {
      isGregorianCal = 0;
   }

   if (month < 3)
   {
      year = year - 1;
      month = month + 12;
   }

   A = year / 100;
   B = (2 - A + (A / 4)) * isGregorianCal;

   if (year < 0)
   {
      C = (int)((365.25 * year) - 0.75);
   }
   else
   {
      C = (int)(365.25 * year);
   }

   D = (int)(30.6001 * (month + 1));
   double JD = B + C + D + 1720994.5 + fraction;

   return JD;
}

Android / Java version:

public static double GetJulianDate(Calendar calendarDate){

   int year = calendarDate.get(Calendar.YEAR);
   int month = calendarDate.get(Calendar.MONTH) + 1;
   int day = calendarDate.get(Calendar.DAY_OF_MONTH);
   double hour = calendarDate.get(Calendar.HOUR_OF_DAY);
   double minute = calendarDate.get(Calendar.MINUTE);
   double second = calendarDate.get(Calendar.SECOND);
   int isGregorianCal = 1;
   int A;
   int B;
   int C;
   int D;
   double fraction = day + ((hour + (minute / 60) + (second / 60 / 60)) / 24);
       
   if (year < 1582)
   {
      isGregorianCal = 0;
   }
       
   if (month < 3)
   {
      year = year - 1;
      month = month + 12;
   }

   A = year / 100;
   B = (2 - A + (A / 4)) * isGregorianCal;
       
   if (year < 0)
   {
      C = (int)((365.25 * year) - 0.75);
   }
   else
   {
      C = (int)(365.25 * year);
   }
       
   D = (int)(30.6001 * (month + 1));
   double JD = B + C + D + 1720994.5 + fraction;

   return JD;
}

Posted on Friday, May 13, 2011 1:26 PM Android , C# , Astronomy | Back to top


Comments on this post: Determining Julian Date in C# or Java / Android

# re: Determining Julian Date in C# or Java / Android
Requesting Gravatar...
In the Java implementation I miss the calendar instace to force the usage of GMT Timezone to compute year, month and/or hms using UTC. Otherwise the fraction value might refer to other time zones rather than Greenwich.
I would add the following line at the top of the method: calendarDate.setTimeZone(TimeZone.getTimeZone("GMT"));
What do you think?
Left by Inaki on Dec 19, 2011 5:15 PM

# re: Determining Julian Date in C# or Java / Android
Requesting Gravatar...
In Java achievement, I used Calendar Institution to calculate the use of GMT Time Zone for year, month or UTC.

Airtel Mobile Signal Booster
Left by Saurabhi on Feb 02, 2018 11:31 AM

# re: Determining Julian Date in C# or Java / Android
Requesting Gravatar...
I remember the date-book instability to force the use of the GMT time zone to use HMS for year, month and UMC usage. Generally the part honors can mark other time zones instead of Greenwich.

mobile signal booster and jammer
Left by ram on Feb 03, 2018 9:34 AM

Your comment:
 (will show your gravatar)


Copyright © Bill Osuch | Powered by: GeeksWithBlogs.net