Java Utililty Methods Timestamp Convert To

List of utility methods to do Timestamp Convert To

Description

The list of methods to do Timestamp Convert To are organized into topic(s).

Method

shorttimestampToDayNumber(long timestamp)
timestamp To Day Number
long result = timestamp / (1000 * 60 * 60 * 24);
if (result < 0 || result > Short.MAX_VALUE) {
    throw new IllegalArgumentException("Wrong date input '" + result + "'");
return (short) result;
longtimestampToInternal(java.sql.Timestamp ts)
Converts the Java type used for UDF parameters of SQL TIMESTAMP type ( java.sql.Timestamp ) to internal representation (long).
return ts.getTime();
longtimestampToLong(Timestamp ts)
Convert a timestamp to seconds.
return (ts.getTime() / 1000) - (ts.getTimezoneOffset() * 60);
longtimestampToMicros(Timestamp timestamp)
Converts a Timestamp to microseconds since the Unix epoch (1970-01-01T00:00:00Z).
long millis = timestamp.getTime() * 1000L;
long micros = (timestamp.getNanos() % 1000000L) / 1000L;
if (micros >= 0) {
    return millis + micros;
} else {
    return millis + 1000000L + micros;
int[]timestampToParts(long time)
Returns the given time as int array that has exactly 6 items:
  • 0 - milliseconds;
  • 1 - seconds;
  • 2 - minutes;
  • 3 - hours;
  • 4 - days;
  • 5 - weeks;
int s = (int) Math.floor(time / 1000);
int ms = (int) (time - s * 1000);
int w = (int) Math.floor(s / 604800);
s -= w * 604800;
int d = (int) Math.floor(s / 86400);
s -= d * 86400;
int h = (int) Math.floor(s / 3600);
s -= h * 3600;
...
StringtimestampToPrettyString(long ts)
Format ts from "0m00" to "59m59", then "1h00" to "xxxxh59"
long s = (ts /= 1000) % 60;
long m = (ts /= 60) % 60;
long h = ts / 60;
return (h > 0 ? (h + "h") : "") + ((m < 10 && h > 0) ? "0" : "") + m
        + (h == 0 ? ("m" + ((s < 10) ? "0" : "") + s) : "");
StringtimestampToSearchString(final Timestamp timestamp)
timestamp To Search String
if (timestamp == null) {
    return "";
return timestamp.toString();
StringtimestampToString(long time)
Formats the given time into string like "7w 1d 2h 34m 56s".
int[] t = timestampToParts(time);
StringBuilder result = new StringBuilder();
if (t[5] > 0)
    result.append(t[5]).append("w ");
if (t[4] > 0)
    result.append(t[4]).append("d ");
if (t[3] > 0)
    result.append(t[3]).append("h ");
...
longtimestampToTicks(int year, int month, int day, int hours, int minutes, int seconds, int milliseconds)
Converts a field by field timestamp into millisecond ticks.
boolean isLeapYear = isLeapYear(year);
long dayComponent = (long) (day - 1) * MILLIS_PER_DAY;
long monthComponent = millisToStartOfMonth(month, isLeapYear);
long yearComponent = millisToYearStart(year);
long hoursComponent = (long) hours * MILLIS_PER_HOUR;
long minutesComponent = (long) minutes * MILLIS_PER_MINUTE;
long secondsComponent = (long) seconds * MILLIS_PER_SECOND;
return dayComponent + monthComponent + yearComponent + hoursComponent + minutesComponent + secondsComponent
...
Timestampto_timestamp(String date)
ttimestamp
return new Timestamp(to_date(date).getTime());