Converts milliseconds into a calendar object.

@param millis a time given in milliseconds after epoch
@return the calendar object for the given time
559909
public static GregorianCalendar millisToCalendar(long millis) {

        GregorianCalendar result = new GregorianCalendar();
        result.setTimeZone(TimeZone.getTimeZone("GMT"));
        result.setTimeInMillis((long)(Math.ceil(millis / 1000) * 1000));
        return result;
    }
658876
@Nonnull
  public static GregorianCalendar getCalendarUTC (final long nMillis)
  {
    final GregorianCalendar aCalendar = new GregorianCalendar (PDTConfig.getUTCTimeZone (),
                                                               Locale.getDefault (Locale.Category.FORMAT));
    aCalendar.setTimeInMillis (nMillis);
    return aCalendar;
  }
729736
public Calendar getEnd() {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(end);
		return cal;
	}
574610
private Calendar getCalendar(String iso8601Date) throws
      InvalidDateException
  {
    // YYYY-MM-DDThh:mm:ss.sTZD
    StringTokenizer st = new StringTokenizer(iso8601Date, "-T:.+Z", true);
    if (!st.hasMoreTokens())
    {
      throw new InvalidDateException("Empty Date");
    }
    Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.clear();
    try
    {
      // Year
      if (st.hasMoreTokens())
      {
        int year = Integer.parseInt(st.nextToken());
        calendar.set(Calendar.YEAR, year);
      }
      else
      {
        return calendar;
      }
      // Month
      if (checkValueAndNext(st, "-"))
      {
        int month = Integer.parseInt(st.nextToken()) - 1;
        calendar.set(Calendar.MONTH, month);
      }
      else
      {
        return calendar;
      }
      // Day
      if (checkValueAndNext(st, "-"))
      {
        int day = Integer.parseInt(st.nextToken());
        calendar.set(Calendar.DAY_OF_MONTH, day);
      }
      else
      {
        return calendar;
      }
      // Hour
      if (checkValueAndNext(st, "T"))
      {
        int hour = Integer.parseInt(st.nextToken());
        calendar.set(Calendar.HOUR_OF_DAY, hour);
      }
      else
      {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
      }
      // Minutes
      if (checkValueAndNext(st, ":"))
      {
        int minutes = Integer.parseInt(st.nextToken());
        calendar.set(Calendar.MINUTE, minutes);
      }
      else
      {
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
      }
      if (!st.hasMoreTokens())
      {
        return calendar;
      }
      // Not mandatory now
      // Seconds
      String tok = st.nextToken();
      if (tok.equals(":"))
      { // seconds
        if (st.hasMoreTokens())
        {
          int secondes = Integer.parseInt(st.nextToken());
          calendar.set(Calendar.SECOND, secondes);
          if (!st.hasMoreTokens())
          {
            return calendar;
          }
          // decimal fraction of a second
          tok = st.nextToken();
          if (tok.equals("."))
          {
            String nt = st.nextToken();
            while (nt.length() < 3)
            {
              nt += "0";
            }
            if (nt.length() > 3)
            {
              // check the other part from the decimal fraction to be formed only from digits
              for (int i = 3; i < nt.length(); i++)
              {
                if (!Character.isDigit(nt.charAt(i)))
                {
                  throw new InvalidDateException("Invalid digit in the decimal fraction of a second: " + nt.charAt(i));
                }
              }
            }
            nt = nt.substring(0, 3); //Cut trailing chars..
            int millisec = Integer.parseInt(nt);
            calendar.set(Calendar.MILLISECOND, millisec);
            if (!st.hasMoreTokens())
            {
              return calendar;
            }
            tok = st.nextToken();
          }
          else
          {
            calendar.set(Calendar.MILLISECOND, 0);
          }
        }
        else
        {
          throw new InvalidDateException("No secondes specified");
        }
      }
      else
      {
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
      }
      // Time zone
      if (!tok.equals("Z"))
      { // UTC
        if (!(tok.equals("+") || tok.equals("-")))
        {
          throw new InvalidDateException("only Z, + or - allowed");
        }
        boolean plus = tok.equals("+");
        if (!st.hasMoreTokens())
        {
          throw new InvalidDateException("Missing hour field");
        }
        int tzhour = Integer.parseInt(st.nextToken());
        int tzmin;
        if (checkValueAndNext(st, ":"))
        {
          tzmin = Integer.parseInt(st.nextToken());
        }
        else
        {
          throw new InvalidDateException("Missing minute field");
        }
        if (plus)
        {
          calendar.add(Calendar.HOUR, -tzhour);
          calendar.add(Calendar.MINUTE, -tzmin);
        }
        else
        {
          calendar.add(Calendar.HOUR, tzhour);
          calendar.add(Calendar.MINUTE, tzmin);
        }
      }
      else
      {
        if (st.hasMoreTokens())
        {
          throw new InvalidDateException("Unexpected field at the end of the date field: " + st.nextToken());
        }
      }
    }
    catch (NumberFormatException ex)
    {
      throw new InvalidDateException("[" + ex.getMessage() + "] is not an integer");
    }
    return calendar;
  }
346408
public Calendar getCalendar() {
        if (calendar == null) {
            return guessCalendar();
        }
        Calendar temp = (Calendar) calendar.clone(); // clone for safety
        temp.setTimeZone(getTimeZone());
        temp.setTimeInMillis(System.currentTimeMillis());
        return temp;
    }
1068987
public Calendar getTimeStampDate() {
        if (timeStampToken == null)
            return null;
        Calendar cal = new GregorianCalendar();
        Date date = timeStampToken.getTimeStampInfo().getGenTime();
        cal.setTime(date);
        return cal;
    }
46303
public static String formatMillis(double theMillis) {
		StringBuilder buf = new StringBuilder(20);
		if (theMillis > 0.0 && theMillis < 1.0) {
			buf.append(getSubMillisecondMillisFormat().format(theMillis));
			buf.append("ms");
		} else if (theMillis < (10 * DateUtils.MILLIS_PER_SECOND)) {
			buf.append((int) theMillis);
			buf.append("ms");
		} else if (theMillis >= DateUtils.MILLIS_PER_DAY) {
			double days = theMillis / DateUtils.MILLIS_PER_DAY;
			if (days >= 10) {
				buf.append(getTenDayFormat().format(days));
				buf.append(" days");
			} else if (days != 1.0f) {
				buf.append(getDayFormat().format(days));
				buf.append(" days");
			} else {
				buf.append(getDayFormat().format(days));
				buf.append(" day");
			}
		} else {
			long millisAsLong = (long) theMillis;
			append(buf, "", 2, ((millisAsLong % DateUtils.MILLIS_PER_DAY) / DateUtils.MILLIS_PER_HOUR));
			append(buf, ":", 2, ((millisAsLong % DateUtils.MILLIS_PER_HOUR) / DateUtils.MILLIS_PER_MINUTE));
			append(buf, ":", 2, ((millisAsLong % DateUtils.MILLIS_PER_MINUTE) / DateUtils.MILLIS_PER_SECOND));
			if (theMillis <= DateUtils.MILLIS_PER_MINUTE) {
				append(buf, ".", 3, (millisAsLong % DateUtils.MILLIS_PER_SECOND));
			}
		}
		return buf.toString();
	}
737867
public static Calendar startOfHour(Calendar origin) {
        Calendar cal = (Calendar) origin.clone();
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        return cal;
    }
374628
public static double getJulianDayNumber(Calendar time) {

        if (time == null) {
            time = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
        }

        double jd = BASE_JD + time.getTimeInMillis() / MILLISEC_PER_DAY;

        return jd;
    }
794906
function _getDateMillis (epoc) {
  // Use highres time if available as JS date is +- 15ms
  try {
    var microtime = require('microtime');
    // Time in microsecs, convert to ms
    return (Math.round(microtime.now() / 1000) - epoc.getTime()).toString(2);
  } catch (err) {}

  return ((new Date()) - epoc).toString(2);
}
732116
public static long midnightUTC(long epochMillis) {
    Calendar day = Calendar.getInstance(UTC);
    day.setTimeInMillis(epochMillis);
    day.set(Calendar.MILLISECOND, 0);
    day.set(Calendar.SECOND, 0);
    day.set(Calendar.MINUTE, 0);
    day.set(Calendar.HOUR_OF_DAY, 0);
    return day.getTimeInMillis();
  }
1021777
public static CharSequence getJSDate(Calendar calendar)
	{
		StringBuilder dateJavascript = new StringBuilder();
		dateJavascript.append("new Date(");
		dateJavascript.append(calendar.get(Calendar.YEAR)).append(',');
		dateJavascript.append(calendar.get(Calendar.MONTH)).append(',');
		dateJavascript.append(calendar.get(Calendar.DAY_OF_MONTH)).append(',');
		dateJavascript.append(calendar.get(Calendar.HOUR_OF_DAY)).append(',');
		dateJavascript.append(calendar.get(Calendar.MINUTE)).append(',');
		dateJavascript.append(calendar.get(Calendar.SECOND)).append(',');
		dateJavascript.append(calendar.get(Calendar.MILLISECOND));
		dateJavascript.append(')');

		return dateJavascript.toString();
	}
635335
private static Calendar getCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }
1321212
public String format(Calendar calendar, Calendar smscCalendar) {
    if (calendar == null || smscCalendar == null) {
      return null;
    }

    long diffTimeInMillis = calendar.getTimeInMillis() - smscCalendar.getTimeInMillis();
    if (diffTimeInMillis < 0) {
      throw new IllegalArgumentException("The requested relative time has already past.");
    }

    // calculate period from epoch, this is not as accurate as Joda-Time Period class or Java 8 Period
    Calendar offsetEpoch = Calendar.getInstance(utcTimeZone);
    offsetEpoch.setTimeInMillis(diffTimeInMillis);
    int years = offsetEpoch.get(Calendar.YEAR) - 1970;
    int months = offsetEpoch.get(Calendar.MONTH);
    int days = offsetEpoch.get(Calendar.DAY_OF_MONTH) - 1;
    int hours = offsetEpoch.get(Calendar.HOUR_OF_DAY);
    int minutes = offsetEpoch.get(Calendar.MINUTE);
    int seconds = offsetEpoch.get(Calendar.SECOND);

    if (years >= 100) {
      throw new IllegalArgumentException("The requested relative time is more then a century (" + years + " years).");
    }

    return format(years, months, days, hours, minutes, seconds);
  }
1121888
@Override
    public <B extends Appendable> B format(final long millis, final B buf) {
        final Calendar c = newCalendar();
        c.setTimeInMillis(millis);
        return applyRules(c, buf);
    }
632819
public static PeriodType millis() {
        PeriodType type = cMillis;
        if (type == null) {
            type = new PeriodType(
                "Millis",
                new DurationFieldType[] { DurationFieldType.millis() },
                new int[] { -1, -1, -1, -1, -1, -1, -1, 0, }
            );
            cMillis = type;
        }
        return type;
    }
962582
protected XMLGregorianCalendar date(Long millis) {
        if (millis == null) {
            return null;
        }
        GregorianCalendar gCal = new GregorianCalendar();
        gCal.setTimeInMillis(millis);
        return dtf.newXMLGregorianCalendar(gCal);
    }