Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I convert between a Java Date and Julian day number?
    primarykey
    data
    text
    <p>How can a Java <code>Date</code> be converted into a <code>double</code> that represents a Julian day?<br> How can a Julian day number be converted into a Java <code>Date</code>?</p> <hr> <pre><code>public static double dateToJulian(Date date) { GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(date); int year; int month; float day; int a; int b; double d; double frac; frac = (calendar.get(Calendar.HOUR_OF_DAY) / 0.000024 + calendar.get(Calendar.MINUTE) / 0.001440); b = 0; year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH) + 1; DecimalFormat ceroPlaces = new DecimalFormat("0"); day = calendar.get(Calendar.DAY_OF_MONTH); day = Float.parseFloat(ceroPlaces.format(day) + "." + ceroPlaces.format(Math.round(frac))); if (month &lt; 3) { year--; month += 12; } if (FuncionSoporte.compararFechas(calendar.getTime(), calendar.getGregorianChange()) &gt; 0) { a = year / 100; b = 2 - a + a / 4; } d = Math.floor(365.25 * year) + Math.floor(30.6001 * (month + 1)) + day + 1720994.5 + b; return (d); } </code></pre> <hr> <pre><code>public static Date julianToDate(double jd) { double z, f, a, b, c, d, e, m, aux; Date date = new Date(); jd += 0.5; z = Math.floor(jd); f = jd - z; if (z &gt;= 2299161.0) { a = Math.floor((z - 1867216.25) / 36524.25); a = z + 1 + a - Math.floor(a / 4); } else { a = z; } b = a + 1524; c = Math.floor((b - 122.1) / 365.25); d = Math.floor(365.25 * c); e = Math.floor((b - d) / 30.6001); aux = b - d - Math.floor(30.6001 * e) + f; Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, (int) aux); aux = ((aux - calendar.get(Calendar.DAY_OF_MONTH)) * 24); calendar.set(Calendar.HOUR_OF_DAY, (int) aux); calendar.set(Calendar.MINUTE, (int) ((aux - calendar.get(Calendar.HOUR_OF_DAY)) * 60)); if (e &lt; 13.5) { m = e - 1; } else { m = e - 13; } // Se le resta uno al mes por el manejo de JAVA, donde los meses empiezan en 0. calendar.set(Calendar.MONTH, (int) m - 1); if (m &gt; 2.5) { calendar.set(Calendar.YEAR, (int) (c - 4716)); } else { calendar.set(Calendar.YEAR, (int) (c - 4715)); } return calendar.getTime(); } </code></pre> <p>EDIT: Added FuncionSoporte.java code.</p> <pre><code>public class FuncionSoporte { /** * Compare 2 dates. If the first is after the second result will be positive, if the second is after then negative, 0 if they are equal. */ public static int compararFechas(Date d1, Date d2) { Calendar c1 = new GregorianCalendar(); c1.setTime(d1); Calendar c2 = new GregorianCalendar(); c2.setTime(d2); if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) { if (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)) { return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH); } else { return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH); } } else { return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload