Skip to content

Instantly share code, notes, and snippets.

@yakatz
Created August 29, 2013 14:29
Show Gist options
  • Save yakatz/6378841 to your computer and use it in GitHub Desktop.
Save yakatz/6378841 to your computer and use it in GitHub Desktop.
Molad Builder
import net.sourceforge.zmanim.*;
import net.sourceforge.zmanim.hebrewcalendar.*;
import net.sourceforge.zmanim.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class MoladBuilder {
public static void main(String[] args) {
DateFormat zmanimFormat = new SimpleDateFormat("MMMMM d");
zmanimFormat.setTimeZone(TimeZone.getDefault());
DateFormat day = new SimpleDateFormat("EEEEE");
day.setTimeZone(TimeZone.getDefault());
DateFormat standardFormatI = new SimpleDateFormat("MMMMM d, h:mm a");
standardFormatI.setTimeZone(TimeZone.getTimeZone("Asia/Jerusalem"));
DateFormat standardFormatA = new SimpleDateFormat("MMMMM d, h:mm a");
standardFormatA.setTimeZone(TimeZone.getTimeZone("America/New_York"));
HebrewDateFormatter hdf = new HebrewDateFormatter();
hdf.setHebrewFormat(true);
int[] months = {JewishDate.CHESHVAN
, JewishDate.KISLEV
, JewishDate.TEVES
, JewishDate.SHEVAT
, JewishDate.ADAR
, JewishDate.ADAR_II
, JewishDate.NISSAN
, JewishDate.IYAR
, JewishDate.SIVAN
, JewishDate.TAMMUZ
, JewishDate.AV
, JewishDate.ELUL};
System.out.println("moladAnnounced" + "\t" + "moladStandard" + "\t" + "moladNewYork" + "\t" + "hdf.formatMonth(jd)" + "\t" + "ShabbatMevorchim");
for (int month : months){
JewishDate jd = new JewishDate(5774, month, 1);
JewishDate molad = jd.getMolad();
int a;
String moladAnnounced = day.format(molad.getTime()) + " " + timeOfDay(molad) + ", " + zmanimFormat.format(molad.getTime()) + ", " + (((a = molad.getMoladHours()%12) == 0?12:a)) + ":" + String.format("%2d", molad.getMoladMinutes()) + " +" + molad.getMoladChalakim() + " " + ((molad.getMoladHours()>11?"PM":"AM"));
Calendar moladS = Calendar.getInstance(TimeZone.getTimeZone("Asia/Jerusalem"));
moladS.setTime(getMoladAsDate(molad));
String moladStandard = standardFormatI.format(moladS.getTime());
String moladNewYork = standardFormatA.format(moladS.getTime());
Calendar shabbatB = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));
shabbatB.setTime(jd.getTime());
do
{
shabbatB.add(Calendar.DATE, -1);
} while (shabbatB.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY);
String shabbatBefore = zmanimFormat.format(shabbatB.getTime());
System.out.println(moladAnnounced + "\t" + moladStandard + "\t" + moladNewYork + "\t" + hdf.formatMonth(jd) + "\t" + shabbatBefore);
}
}
public static String timeOfDay(JewishDate jd)
{
if (jd.getMoladHours() < 12)
{
return "Morning";
}
String locationName = "Jerusalem";
double latitude = 31.778; // Har habayis
double longitude = 35.2354;// Har Habayis
double elevation = 0;
TimeZone timeZone = TimeZone.getTimeZone("Asia/Jerusalem");
GeoLocation location = new GeoLocation(locationName, latitude, longitude, elevation, timeZone);
ZmanimCalendar zc = new ZmanimCalendar(location);
zc.getCalendar().set(jd.getGregorianYear(), jd.getGregorianMonth(), jd.getGregorianDayOfMonth());
Calendar c = Calendar.getInstance(timeZone);
c.set(jd.getGregorianYear(), jd.getGregorianMonth(), jd.getGregorianDayOfMonth(), jd.getMoladHours(), jd.getMoladMinutes());
Calendar s = Calendar.getInstance(timeZone);
s.setTime(zc.getSunset());
if (c.after(s))
{
return "Night";
}
return "Afternoon";
}
/**
* Returns the molad in Standard Time in Yerushalayim as a Date. The traditional calculation uses local time. This
* method subtracts 20.94 minutes (20 minutes and 56.496 seconds) from the local time (Har Habayis with a longitude
* of 35.2354&deg; is 5.2354&deg; away from the %15 timezone longitude) to get to standard time. This method
* intentionally uses standard time and not dailight savings time. Java will implicitly format the time to the
* default (or set) Timezone.
*
* @return the Date representing the moment of the molad in Yerushalayim standard time (GMT + 2)
*/
public static Date getMoladAsDate(JewishDate molad) {
String locationName = "Jerusalem, Israel";
double latitude = 31.778; // Har Habayis latitude
double longitude = 35.2354; // Har Habayis longitude
// The molad calculation always extepcst output in standard time. Using "Asia/Jerusalem" timezone will incorrect
// adjust for DST.
TimeZone yerushalayimStandardTZ = TimeZone.getTimeZone("GMT+2");
GeoLocation geo = new GeoLocation(locationName, latitude, longitude, yerushalayimStandardTZ);
Calendar cal = Calendar.getInstance(geo.getTimeZone());
cal.clear();
double moladSeconds = molad.getMoladChalakim() * 10 / (double)3;
cal.set(molad.getGregorianYear(), molad.getGregorianMonth(), molad.getGregorianDayOfMonth(),
molad.getMoladHours(), molad.getMoladMinutes(), (int) moladSeconds);
cal.set(Calendar.MILLISECOND, (int) (1000 * (moladSeconds - (int) moladSeconds)));
// subtract local time difference of 20.94 minutes (20 minutes and 56.496 seconds) to get to Standard time
cal.add(Calendar.MILLISECOND, -1 * (int) geo.getLocalMeanTimeOffset());
return cal.getTime();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment