Java Utililty Methods SQL Time Calculate

List of utility methods to do SQL Time Calculate

Description

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

Method

StringaddDateTime(String date, String type, int into)
add Date Time
date = date.replaceAll("-", "/");
GregorianCalendar grc = new GregorianCalendar();
grc.setTime(new java.util.Date(date));
if (type.equals("D")) {
    grc.add(GregorianCalendar.DATE, into);
} else if (type.equals("M")) {
    grc.add(GregorianCalendar.MONTH, into);
} else if (type.equals("Y")) {
...
StringaddTime(String date, String type, int into, String pattern)
add Time
date = date.replaceAll("-", "/");
GregorianCalendar grc = new GregorianCalendar();
Date d = parseDate(date);
if (d == null) {
    d = new Date();
grc.setTime(d);
if (type.equals("D")) {
...
TimeaddTime(Time time, int minutes)
add Time
return null;
TimestampaddTimeToDate(Date dt)
Adds the time to date.
Timestamp dateVal = null;
try {
    if (dt != null) {
        long onedayminusonesec = (24 * 60 * 60 * 1000) - 10;
        dateVal = new Timestamp(dt.getTime() + onedayminusonesec);
} catch (Exception e) {
    e.printStackTrace();
...
voidappendTime(StringBuffer sb, Calendar cal, int nanos)
append Time
int hours = cal.get(Calendar.HOUR_OF_DAY);
if (hours < 10)
    sb.append('0');
sb.append(hours);
sb.append(':');
int minutes = cal.get(Calendar.MINUTE);
if (minutes < 10)
    sb.append('0');
...
longgetDifference(Time timeFrom, Time timeTo)
Gets the difference.
try {
    DateFormat format = new SimpleDateFormat("HH:mm:ss");
    Date date = format.parse(timeFrom.toString());
    Date date2 = format.parse(timeTo.toString());
    long difference = (date2.getTime() - date.getTime()) / 1000 / 60;
    return difference;
} catch (Exception e) {
    throw new RuntimeException(e);
...
StringgetDiffTime(String _fromDate, String _fromTime, String _toDate, String _toTime)
get Diff Time
if (_fromDate == null || _fromTime == null || _toDate == null || _toTime == null
        || _fromDate.trim().length() != 8 || _fromTime.trim().length() != 6 || _toDate.trim().length() != 8
        || _toTime.trim().length() != 6)
    return null;
Calendar cal = new GregorianCalendar();
int year = Integer.parseInt(_fromDate.substring(0, 4));
int month = Integer.parseInt(_fromDate.substring(4, 6));
int day = Integer.parseInt(_fromDate.substring(6));
...
longgetExpiresTime(Long validityPeriod)
Method to return expires time for access token.
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
return (timestamp.getTime() + validityPeriod * 1000);
DategetMidNightTime()
get Mid Night Time
java.util.Calendar cal = new java.util.GregorianCalendar();
cal.add(Calendar.DATE, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
intgetMinute(Time time)
get Minute
LocalTime localTime = time.toLocalTime();
return localTime.getMinute();