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

Timestampadd(Timestamp target, int addNum, int addKind)
add
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(target);
cal.add(addKind, addNum);
return new Timestamp(new Date(cal.getTimeInMillis()).getTime());
Timestampadd(Timestamp ts, int field, int amount)
add
Calendar cal = Calendar.getInstance();
cal.setTime(ts);
cal.add(field, amount);
Timestamp ts2 = new Timestamp(cal.getTime().getTime());
return ts2;
TimestampaddDate(Timestamp date, String type, int into)
add Date
GregorianCalendar grc = new GregorianCalendar();
grc.setTime(new java.util.Date(date.getTime()));
if (type.equals("D")) {
    grc.add(GregorianCalendar.DATE, into);
} else if (type.equals("M")) {
    grc.add(GregorianCalendar.MONTH, into);
} else if (type.equals("Y")) {
    grc.add(GregorianCalendar.YEAR, into);
...
TimestampaddDays(Timestamp date, int days)
add Days
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, days); 
return new Timestamp(cal.getTime().getTime());
TimestampaddDays(Timestamp day, int offset)
Return Day + offset (truncates)
if (day == null)
    day = new Timestamp(System.currentTimeMillis());
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(day);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
...
TimestampaddDaysToTimestamp(Timestamp start, int days)
add Days To Timestamp
return new Timestamp(start.getTime() + (24L * 60L * 60L * 1000L * days));
TimestampaddDaysToTimestamp(Timestamp timestamp, int numDays)
Adds the specified number of Days to the specified Date object To subtract the specified number of Days to the specified Date object, juz use a negative number Example: DateUtil.addDaysToDate(date, -5) == subtracting 5 days from the specified date.
if (timestamp == null) {
    return null;
Calendar c = Calendar.getInstance();
c.setTime(timestamp);
c.add(Calendar.DATE, numDays);
return new Timestamp(c.getTimeInMillis());
TimestampaddHours(Timestamp date, int numOfHours)
add Hours
Long milliSecInAnHour = new Long(60 * 60 * 1000);
Timestamp newTS = new Timestamp(date.getTime());
long milliSecToAdd = milliSecInAnHour * numOfHours;
long newTimeMilliSec = newTS.getTime();
newTS.setTime(newTimeMilliSec + milliSecToAdd);
return newTS;
TimestampaddHoursToTimestamp(Timestamp start, int hours)
add Hours To Timestamp
return new Timestamp(start.getTime() + (60L * 60L * 1000L * hours));
TimestampaddMilli(Timestamp nowDate, int period)
addMilli.
Calendar calendar = Calendar.getInstance();
calendar.setTime(nowDate);
calendar.add(Calendar.MILLISECOND, period);
Timestamp stopTerm = date2Timestamp(calendar.getTime());
return stopTerm;