XMLGregorianCalendar used because it provides a standard formatting (ISO 8601) and timezone handling
@param millis
@return an XMLGregorianCalendar in the current timezone.<p>
TODO consider forcing TimeZone to UTC
962582
protected XMLGregorianCalendar date(Long millis) {
        if (millis == null) {
            return null;
        }
        GregorianCalendar gCal = new GregorianCalendar();
        gCal.setTimeInMillis(millis);
        return dtf.newXMLGregorianCalendar(gCal);
    }
513105
public PeriodFormatter getPeriodFormatter() {
    if (formatter == null) {
      formatter = ps.newPeriodFormatterFactory()
          .setLocale(localeName)
          .getFormatter();
    }
    return formatter;
  }
1321651
public static LocalDateTime from(long epochMillis) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), ZoneOffset.UTC);
    }
157760
public static Calendar popCalendar(long timeInMillis)
   {
      Calendar calendar = popCalendar();
      calendar.setTimeInMillis(timeInMillis);
      return calendar;
   }
38039
static double fromDate(Date dt) {

        // the number of milliseconds since January 1, 1970, 00:00:00 GMT
        long t = dt.getTime();

        // the number of milliseconds since January 1, 1970, 00:00:00 Local Time
        Calendar c = new GregorianCalendar();
        c.setTime(dt);
        t += (c.get(Calendar.ZONE_OFFSET) + c.get(Calendar.DST_OFFSET)) ;

        // the number of milliseconds since December 30, 1899, 00:00:00 Local Time
        t += 2209161600000L;

        // DATE is an offset from "30 December 1899"
        if (t < 0) {
            // -0.3 -> -0.7
            long offset = -(t % MSPD);    // TODO: check
            t = t - MSPD + offset;
        }
        double d = ((double) t) / MSPD;
        return d;
    }
891105
@Nullable
  public static XMLGregorianCalendar getXMLCalendarTime (@Nullable final Date aBase)
  {
    if (aBase == null)
      return null;
    return getXMLCalendarTime (getCalendar (aBase));
  }
766792
public static Calendar calendar(long millis) {
		final Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(millis);
		return cal;
	}
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;
  }
264373
public static XMLGregorianCalendar createCalendar(String isoDate) throws Exception {
        if (isoDate == null) {
            SimpleDateFormat format = new SimpleDateFormat(DATETIME_FORMAT);
            isoDate = format.format(new Date());
        }

        DatatypeFactory df = DatatypeFactory.newInstance();
        return df.newXMLGregorianCalendar(isoDate);
    }
387139
public static String formatTimeText(int millis){
        // Format the milliseconds into the form - 'hh:mm:ss'
        return String.format(Locale.US, "%02d:%02d:%02d",
                TimeUnit.MILLISECONDS.toHours(millis),
                TimeUnit.MILLISECONDS.toMinutes(millis) -
                        TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                TimeUnit.MILLISECONDS.toSeconds(millis) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
    }
133804
public static ZonedDateTime toNullableDateTime(Object value) {
		if (value == null)
			return null;
		if (value instanceof ZonedDateTime)
			return (ZonedDateTime) value;

		if (value instanceof Calendar) {
			Calendar calendar = (Calendar) value;
			return ZonedDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId());
		}
		if (value instanceof Date)
			return ZonedDateTime.ofInstant(((Date) value).toInstant(), ZoneId.systemDefault());

		if (value instanceof LocalDate)
			return ZonedDateTime.of((LocalDate) value, LocalTime.of(0, 0), ZoneId.systemDefault());
		if (value instanceof LocalDateTime)
			return ZonedDateTime.of((LocalDateTime) value, ZoneId.systemDefault());

		if (value instanceof Integer)
			return millisToDateTime((int) value);
		if (value instanceof Short)
			return millisToDateTime((short) value);
		if (value instanceof Long)
			return millisToDateTime((long) value);
		if (value instanceof Float)
			return millisToDateTime((long) ((float) value));
		if (value instanceof Double)
			return millisToDateTime((long) ((double) value));
		if (value instanceof Duration)
			return millisToDateTime(((Duration) value).toMillis());

		if (value instanceof String) {
			try {
				return ZonedDateTime.parse((String) value, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
			} catch (DateTimeParseException ex) {
			}

			try {
				return ZonedDateTime.of(LocalDateTime.parse((String) value, simpleDateTimeFormatter),
						ZoneId.systemDefault());
			} catch (DateTimeParseException ex) {
			}

			try {
				return ZonedDateTime.of(LocalDate.parse((String) value, simpleDateFormatter), LocalTime.of(0, 0),
						ZoneId.systemDefault());
			} catch (DateTimeParseException ex) {
			}
		}

		return null;
	}
1272780
public XMLGregorianCalendar newGMonth(int month) {
        XMLGregorianCalendar cal=dataFactory.newXMLGregorianCalendar();
        cal.setMonth(month);
        return cal;
    }
669756
public static final StringBuilder appendDate(StringBuilder sb, Long millis) {
        if (millis == null)
            return sb.append((Long) null);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(millis);
        TimeZone zone = cal.getTimeZone();
        sb.append(cal.get(Calendar.YEAR)).append('/');
        int m = cal.get(Calendar.MONTH);
        m++; // Month range is 0 - 11. Bump one for formatting
        if (m < 10)
            sb.append('0');
        sb.append(m).append('/');
        int d = cal.get(Calendar.DAY_OF_MONTH);
        if (d < 10)
            sb.append('0');
        sb.append(d).append('-');
        int h = cal.get(Calendar.HOUR_OF_DAY);
        if (h < 10)
            sb.append('0');
        sb.append(h).append(':');
        m = cal.get(Calendar.MINUTE);
        if (m < 10)
            sb.append('0');
        sb.append(m).append(':');
        int s = cal.get(Calendar.SECOND);
        if (s < 10)
            sb.append('0');
        sb.append(s).append('.');
        int ms = cal.get(Calendar.MILLISECOND);
        if (ms < 100)
            sb.append('0');
        if (ms < 10)
            sb.append('0');
        sb.append(ms).append('-');
        sb.append(zone.getDisplayName(zone.inDaylightTime(cal.getTime()), TimeZone.SHORT));
        sb.append('[').append(millis).append(']');
        return sb;
    }
573840
public static DateTimeValue toDateTimeValue(long millisFromEpoch, TimeZone zone) {
		GregorianCalendar c = new GregorianCalendar(zone);
		c.clear();
		c.setTimeInMillis(millisFromEpoch);
		//@formatter:off
		return new DateTimeValueImpl (
			c.get(Calendar.YEAR),
			c.get(Calendar.MONTH) + 1,
			c.get(Calendar.DAY_OF_MONTH),
			c.get(Calendar.HOUR_OF_DAY),
			c.get(Calendar.MINUTE),
			c.get(Calendar.SECOND)
		);
		//@formatter:on
	}
547386
public XMLGregorianCalendar addMonths(final XMLGregorianCalendar cal, final int amount) {
        XMLGregorianCalendar to = buildXMLGregorianCalendarDate(cal);
        // Add amount of months
        to.add(addMonths(amount));
        return to;
    }
542278
public Date convertToDate(long millis, TimeZone tz) {

    // no adjustments for the inifity hack values
    if (millis <= PGStatement.DATE_NEGATIVE_INFINITY
        || millis >= PGStatement.DATE_POSITIVE_INFINITY) {
      return new Date(millis);
    }
    if (tz == null) {
      tz = getDefaultTz();
    }
    if (isSimpleTimeZone(tz.getID())) {
      // Truncate to 00:00 of the day.
      // Suppose the input date is 7 Jan 15:40 GMT+02:00 (that is 13:40 UTC)
      // We want it to become 7 Jan 00:00 GMT+02:00
      // 1) Make sure millis becomes 15:40 in UTC, so add offset
      int offset = tz.getRawOffset();
      millis += offset;
      // 2) Truncate hours, minutes, etc. Day is always 86400 seconds, no matter what leap seconds
      // are
      millis = millis / ONEDAY * ONEDAY;
      // 2) Now millis is 7 Jan 00:00 UTC, however we need that in GMT+02:00, so subtract some
      // offset
      millis -= offset;
      // Now we have brand-new 7 Jan 00:00 GMT+02:00
      return new Date(millis);
    }
    Calendar cal = calendarWithUserTz;
    cal.setTimeZone(tz);
    cal.setTimeInMillis(millis);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return new Date(cal.getTimeInMillis());
  }
728348
public static Date addMilliseconds(final Date date, final int addMilliseconds)
	{
		final Calendar dateOnCalendar = Calendar.getInstance();
		dateOnCalendar.setTime(date);
		dateOnCalendar.add(Calendar.MILLISECOND, addMilliseconds);
		return dateOnCalendar.getTime();
	}