Java Utililty Methods Timestamp

List of utility methods to do Timestamp

Description

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

Method

StringgetTimeString(Timestamp timestamp)
get Time String
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
    String str = sdf.format(timestamp);
    return str;
} catch (Exception e) {
    e.printStackTrace();
return null;
...
TimestampgetTodayTimestamp()
get Today Timestamp
Calendar cal = new GregorianCalendar();
cal.set(GregorianCalendar.HOUR, 0);
cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
cal.set(GregorianCalendar.MINUTE, 0);
cal.set(GregorianCalendar.SECOND, 0);
cal.set(GregorianCalendar.MILLISECOND, 0);
long todayInMillis = cal.getTime().getTime();
Timestamp ts = new Timestamp(todayInMillis);
...
java.util.DategetUtilDate(java.sql.Timestamp time)
Convert SQL timestamp to Java Date
if (time != null)
    return new java.util.Date(time.getTime());
else
    return null;
IntegergetValue(Timestamp t)
get Value
Integer tempMinSum = 0;
tempMinSum += t.getHours() * 3600;
tempMinSum += t.getMinutes() * 60;
tempMinSum += t.getMinutes();
return tempMinSum;
ZonedDateTimegetZonedDateTime(final Timestamp timestamp)
get Zoned Date Time
return timestamp != null ? ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault()) : null;
booleanhasTimeExpired(Timestamp referenceTime)
has Time Expired
long currentTime = getCurrentTime().getTime();
if (referenceTime != null && currentTime < referenceTime.getTime()) {
    return false;
return true;
booleaninRange(Timestamp start, Timestamp end, boolean OnMonday, boolean OnTuesday, boolean OnWednesday, boolean OnThursday, boolean OnFriday, boolean OnSaturday, boolean OnSunday)
Is start..end on one of the days ?
if (OnSaturday && OnSunday && OnMonday && OnTuesday && OnWednesday && OnThursday && OnFriday)
    return false;
GregorianCalendar calStart = new GregorianCalendar();
calStart.setTimeInMillis(start.getTime());
int dayStart = calStart.get(Calendar.DAY_OF_WEEK);
GregorianCalendar calEnd = new GregorianCalendar();
calEnd.setTimeInMillis(end.getTime());
calEnd.add(Calendar.DAY_OF_YEAR, -1); 
...
java.sql.TimestampinternalToTimestamp(long v)
Converts the internal representation of a SQL TIMESTAMP (long) to the Java type used for UDF parameters ( java.sql.Timestamp ).
return new java.sql.Timestamp(v);
booleanisAllDay(Timestamp start, Timestamp end)
Is all day
GregorianCalendar calStart = new GregorianCalendar();
calStart.setTimeInMillis(start.getTime());
GregorianCalendar calEnd = new GregorianCalendar();
calEnd.setTimeInMillis(end.getTime());
if (calStart.get(Calendar.HOUR_OF_DAY) == calEnd.get(Calendar.HOUR_OF_DAY)
        && calStart.get(Calendar.MINUTE) == calEnd.get(Calendar.MINUTE)
        && calStart.get(Calendar.SECOND) == calEnd.get(Calendar.SECOND)
        && calStart.get(Calendar.MILLISECOND) == calEnd.get(Calendar.MILLISECOND)
...
booleanisDifferentDay(final Timestamp ts1, final Timestamp ts2)
is Different Day
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(ts1.getTime());
final int day1 = cal.get(Calendar.DAY_OF_MONTH), month1 = cal.get(Calendar.MONTH),
        year1 = cal.get(Calendar.YEAR);
cal.setTimeInMillis(ts2.getTime());
final int day2 = cal.get(Calendar.DAY_OF_MONTH), month2 = cal.get(Calendar.MONTH),
        year2 = cal.get(Calendar.YEAR);
return (day1 != day2 || month1 != month2 || year1 != year2);
...