Java SQL Time Calculate getDiffTime(String _fromDate, String _fromTime, String _toDate, String _toTime)

Here you can find the source of getDiffTime(String _fromDate, String _fromTime, String _toDate, String _toTime)

Description

get Diff Time

License

Open Source License

Declaration

public static String getDiffTime(String _fromDate, String _fromTime, String _toDate, String _toTime) 

Method Source Code


//package com.java2s;

import java.util.*;

import java.sql.Time;

public class Main {
    public static String getDiffTime(String _fromDate, String _fromTime, String _toDate, String _toTime) {
        //       logger.debug(" DateHandler.getDiffTime. _fromDate:["  + _fromDate + "] _fromTime:["  + _fromTime + "] _toDate:[" + _toDate + "] _toTime:[" + _toTime + "]");
        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));
        int hour = Integer.parseInt(_fromTime.substring(0, 2));
        int minute = Integer.parseInt(_fromTime.substring(2, 4));
        int second = Integer.parseInt(_fromTime.substring(4));
        cal.set(year, month, day, hour, minute, second);
        long fromTimeL = cal.getTime().getTime();

        year = Integer.parseInt(_toDate.substring(0, 4));
        month = Integer.parseInt(_toDate.substring(4, 6));
        day = Integer.parseInt(_toDate.substring(6));
        hour = Integer.parseInt(_toTime.substring(0, 2));
        minute = Integer.parseInt(_toTime.substring(2, 4));
        second = Integer.parseInt(_toTime.substring(4));
        cal.set(year, month, day, hour, minute, second);
        long toTimeL = cal.getTime().getTime();

        long KST_DIFF = 9 * 60 * 60 * 1000;
        return new Time(toTimeL - fromTimeL - KST_DIFF).toString();

    }//from w  w w  . j  av a2 s.  c o  m

    public static Date getTime(String hhmm) {
        if (hhmm != null && hhmm.length() == 4 && isDigit(hhmm)) {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hhmm.substring(0, 2)));
            cal.set(Calendar.MINUTE, Integer.parseInt(hhmm.substring(2, 4)));

            return cal.getTime();
        }

        return null;
    }

    private static boolean isDigit(String digitStr) {
        if (digitStr != null) {
            for (int i = 0; i < digitStr.length(); i++)
                if (!Character.isDigit(digitStr.charAt(i)))
                    return false;
        }
        return true;
    }
}

Related

  1. addTime(String date, String type, int into, String pattern)
  2. addTime(Time time, int minutes)
  3. addTimeToDate(Date dt)
  4. appendTime(StringBuffer sb, Calendar cal, int nanos)
  5. getDifference(Time timeFrom, Time timeTo)
  6. getExpiresTime(Long validityPeriod)
  7. getMidNightTime()
  8. getMinute(Time time)
  9. getMonth(String dateTime)