/*[deutsch]
<p>Ermittelt das modifizierte julianische Datum. </p>

@param   date    gregorian date
@return  days since [1858-11-17] (modified Julian date)
@throws  IllegalArgumentException if the argument is out of range
@see     #toMJD(int, int, int)
358508
public static long toMJD(GregorianDate date) {

        return GregorianMath.toMJD(date.getYear(), date.getMonth(), date.getDayOfMonth());

    }
1213206
public static boolean isLastDayOfTheMonth(final Date date) {

    Date dateOfMonthsBack = getDateOfMonthsBack(-1, date);
    int dayOfMonth = getDayOfMonth(dateOfMonthsBack);
    Date dateOfDaysBack = getDateOfDaysBack(dayOfMonth, dateOfMonthsBack);
    return dateOfDaysBack.equals(date);
  }
133313
public static Calendar getCalendarInstance(final Date date) {
        Calendar instance;
        if (date instanceof DateTime) {
            final DateTime dateTime = (DateTime) date;
            if (dateTime.getTimeZone() != null) {
                instance = Calendar.getInstance(dateTime.getTimeZone());
            }
            else if (dateTime.isUtc()) {
                instance = Calendar.getInstance(TimeZones.getUtcTimeZone());
            }
            else {
            	// a date-time without a timezone but not UTC is floating
                instance = Calendar.getInstance();
            }
        }
        else {
            instance = Calendar.getInstance(TimeZones.getDateTimeZone());
        }
        return instance;
    }
1267641
static public long msDiffJulianToGregorian(java.util.Date date)
  {
    // get the year of the date
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

    // if date is before 1582-10-05, apply the difference
    // by (H-(H/4)-2) where H is the hundreds digit of the year according to:
    // http://en.wikipedia.org/wiki/Gregorian_calendar
    if (date.getTime() < -12220156800000L)
    {
      // for dates on or before 02/28, use the previous year otherwise use
      // current year.
      // TODO: we need to revisit this since there is a potential issue using
      // the year/month/day from the calendar since that may not be the same
      // year/month/day as the original date (which is the problem we are
      // trying to solve here).

      if (month == 0 || (month == 1 && dayOfMonth <= 28))
      {
        year = year - 1;
      }

      int hundreds = year / 100;
      int differenceInDays = hundreds - (hundreds / 4) - 2;

      return differenceInDays * 86400000;
    }
    else
    {
      return 0;
    }
  }
1319732
public static DayOfWeek dayOfWeek(DateValue date) {
		int dayIndex = fixedFromGregorian(date.year(), date.month(), date.day()) % 7;
		if (dayIndex < 0) {
			dayIndex += 7;
		}
		return DAYS_OF_WEEK[dayIndex];
	}
83708
public static Date fromJavaUtilDate(java.util.Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    // Calender.MONTH starts from 0 while G C date starts from 1
    return new Date(
        cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH));
  }
1336558
function (date) {
                if (date instanceof Date === false) {
                    throw new ArgumentError(
                        Logger.logMessage(Logger.LEVEL_SEVERE, "SunPosition", "getAsCelestialLocation",
                            "missingDate"));
                }

                var julianDate = this.computeJulianDate(date);

                //number of days (positive or negative) since Greenwich noon, Terrestrial Time, on 1 January 2000 (J2000.0)
                var numDays = julianDate - 2451545;

                var meanLongitude = WWMath.normalizeAngle360(280.460 + 0.9856474 * numDays);

                var meanAnomaly = WWMath.normalizeAngle360(357.528 + 0.9856003 * numDays) * Angle.DEGREES_TO_RADIANS;

                var eclipticLongitude = meanLongitude + 1.915 * Math.sin(meanAnomaly) + 0.02 * Math.sin(2 * meanAnomaly);
                var eclipticLongitudeRad = eclipticLongitude * Angle.DEGREES_TO_RADIANS;

                var obliquityOfTheEcliptic = (23.439 - 0.0000004 * numDays) * Angle.DEGREES_TO_RADIANS;

                var declination = Math.asin(Math.sin(obliquityOfTheEcliptic) * Math.sin(eclipticLongitudeRad)) *
                    Angle.RADIANS_TO_DEGREES;

                var rightAscension = Math.atan(Math.cos(obliquityOfTheEcliptic) * Math.tan(eclipticLongitudeRad)) *
                    Angle.RADIANS_TO_DEGREES;

                //compensate for atan result
                if (eclipticLongitude >= 90 && eclipticLongitude < 270) {
                    rightAscension += 180;
                }
                rightAscension = WWMath.normalizeAngle360(rightAscension);

                return {
                    declination: declination,
                    rightAscension: rightAscension
                };
            }
887318
public static Integer getRemainingDayOfMonth(Date date) {

        if (date == null)
            return null;

        // 月天数
        int days = getDayNumOfMonth(date);
        // 第几天
        int dayOfMonth = getDayOfMonth(date);

        return days - dayOfMonth;
    }
186601
public static String formatDate(Date date) {
		if (null == date) {
			return null;
		}
		return DatePattern.NORM_DATE_FORMAT.format(date);
	}
1018837
public static int[] getDate(final double JULIAN_DAY) throws Exception {
        if (JULIAN_DAY < 2299160.0 && JULIAN_DAY >= 2299150.0) {
            throw new Exception("invalid julian day " + JULIAN_DAY + ". This date does not exist.");
        }

        // The conversion formulas are from Meeus,
        // Chapter 7.
        double Z = Math.floor(JULIAN_DAY + 0.5);
        double F = JULIAN_DAY + 0.5 - Z;
        double A = Z;
        if (Z >= 2299161D) {
            int a = (int) ((Z - 1867216.25) / 36524.25);
            A += 1 + a - a / 4;
        }
        double B = A + 1524;
        int    C = (int) ((B - 122.1) / 365.25);
        int    D = (int) (C * 365.25);
        int    E = (int) ((B - D) / 30.6001);

        double exactDay = F + B - D - (int) (30.6001 * E);
        int    day      = (int) exactDay;
        int    month    = (E < 14) ? E - 1 : E - 13;
        int    year     = C - 4715;
        if (month > 2) year--;
        double h = ((exactDay - day) * SECONDS_PER_DAY) / 3600.0;

        int    hour   = (int) h;
        double m      = (h - hour) * 60.0;
        int    minute = (int) m;
        int    second = (int) ((m - minute) * 60.0);

        return new int[] { year, month, day, hour, minute, second };
    }
222038
public static Date addDays(long dt, int days)
    {
        Calendar c = getCalendar();
        if(dt > 0L)
            c.setTimeInMillis(dt);
        c.add(Calendar.DATE, days);
        return c.getTime();
    }
615354
@Deprecated
	public static String getDateStringMMDDYYYY(Date date) {
		if(date==null) return "";
		Calendar C=Calendar.getInstance();
		C.setTime(date);
		int day=C.get(Calendar.DATE);
		int month=C.get(Calendar.MONTH)+1;
		return
			(month>=0 && month<=9 ? "0":"")
			+month
			+(day>=0 && day<=9 ? "0":"")
			+day
			+C.get(Calendar.YEAR)
		;
	}
98424
public static String toDateString(final Date date) {
        SimpleDateFormat df = RFC1123_PATTERN_FORMAT.get();
        //we always need to set the time zone
        //because date format is stupid, and calling parse() can mutate the timezone
        //see UNDERTOW-458
        df.setTimeZone(GMT_ZONE);
        return df.format(date);
    }
864499
@Deprecated
	public static String getDateString(Date date) {
		synchronized(StringUtility.class) {
			calendar.setTime(date);
			int day=calendar.get(Calendar.DATE);
			return (day>=0 && day<=9 ? "0":"")+String.valueOf(calendar.get(Calendar.DATE))+MONTHS[calendar.get(Calendar.MONTH)]+calendar.get(Calendar.YEAR);
		}
	}
451708
public static XMLGregorianCalendar convertDateToXMLGregorianCalendar(final Date date) {
        try {
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(date);
            XMLGregorianCalendar calXml = DatatypeFactory.newInstance().newXMLGregorianCalendar(
                    cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH) + 1,
                    cal.get(Calendar.DAY_OF_MONTH),
                    cal.get(Calendar.HOUR_OF_DAY),
                    cal.get(Calendar.MINUTE),
                    cal.get(Calendar.SECOND),
                    cal.get(Calendar.MILLISECOND),
                    0);
            return calXml;
        } catch (DatatypeConfigurationException e) {
            throw new SwidException("Cannot convert date", e);
        }
    }
1257104
public static String dateTime(Date date) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		return dateTime(gc);
	}
300314
public Query addDateParameter(java.util.Date date) {
        return addParameter(new Date(date.getTime()));
    }