Java Utililty Methods Date to Time

List of utility methods to do Date to Time

Description

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

Method

DategetTimeByOffset(Date reqDate, int offset)
get Time By Offset
Date result;
Calendar cal = Calendar.getInstance();
cal.setTime(reqDate);
cal.add(Calendar.HOUR, offset);
result = cal.getTime();
return result;
java.util.DategetTimeDateByMinuteAndSeconds(Integer minutes, Integer seconds)
getTimeDateByMinuteAndSeconds: converts the minutes to an Date object {Category} TimestampUtil {talendTypes} Date {param} Integer(5): minutes {param} Integer(5): seconds {example} getTimeDateByMinuteAndSeconds(90,120).
if (minutes != null || seconds != null) {
    java.util.Calendar c = java.util.Calendar.getInstance(getUTCTimeZone());
    c.setTime(new java.util.Date(0l));
    c.set(java.util.Calendar.HOUR_OF_DAY, 0);
    c.set(java.util.Calendar.MINUTE, 0);
    c.set(java.util.Calendar.SECOND, 0);
    if (minutes != null) {
        c.add(java.util.Calendar.MINUTE, minutes);
...
StringgetTimeDateStr(Date date)
get Time Date Str
if (date == null) {
    return null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return getFixedTwoStr(cal.get(Calendar.HOUR_OF_DAY)) + ":" + getFixedTwoStr(cal.get(Calendar.MINUTE)) + " "
        + cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH) + 1) + "/"
        + cal.get(Calendar.YEAR);
...
intgetTimeDeltaValue(Date t, Date baseDate, String tDelta)
Get time value - Time delta value of base date
Calendar cal = Calendar.getInstance();
cal.setTime(baseDate);
long sl = cal.getTimeInMillis();
long el, delta;
int value = 0;
cal.setTime(t);
el = cal.getTimeInMillis();
delta = el - sl;
...
longgetTimeFromDate(Date date)
get Time From Date
Date now = Calendar.getInstance().getTime();
long timePortion = now.getTime() % MILLIS_PER_DAY;
return timePortion;
intgetTimeFromDate(final Date date)
Create an integer time from a Date object.
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
final int hour = calendar.get(Calendar.HOUR_OF_DAY) * 100;
final int minute = calendar.get(Calendar.MINUTE);
return hour + minute;
longgetTimeImMillis(Date date)
get Time Im Millis
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getTimeInMillis();
intgetTimeInSeconds(Date date)
Gets time in seconds.
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY) * 60 * 60 + calendar.get(Calendar.MINUTE) * 60
        + calendar.get(Calendar.SECOND);
StringgetTimeInterval(Date fromDate)
get Time Interval
Calendar cal = Calendar.getInstance();
Date currentDate = cal.getTime();
return getTimeInterval(fromDate, currentDate);
intgetTimeIntervalMins(Date firstDate, Date lastDate)
get Time Interval Mins
if (null == firstDate || null == lastDate) {
    return -1;
long intervalMilli = lastDate.getTime() - firstDate.getTime();
return (int) (intervalMilli / (60 * 1000));