Java Timestamp calcRealTime(Timestamp beginTime, Timestamp endTime)

Here you can find the source of calcRealTime(Timestamp beginTime, Timestamp endTime)

Description

calc Real Time

License

Open Source License

Declaration

public static long calcRealTime(Timestamp beginTime, Timestamp endTime) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.sql.Timestamp;
import java.util.Calendar;

public class Main {
    public static long calcRealTime(Timestamp beginTime, Timestamp endTime) {
        Calendar beginC = getRealCalendar(beginTime);
        Calendar endC = getRealCalendar(endTime);

        long span = (endC.getTimeInMillis() - beginC.getTimeInMillis()) / 1000;

        long a = span / (86400 * 7);
        long b = span % (86400 * 7);
        long c = b / 86400;
        long d = b % 86400;

        if (endC.get(Calendar.DAY_OF_WEEK) < beginC.get(Calendar.DAY_OF_WEEK)
                || endC.get(Calendar.DAY_OF_WEEK) == beginC.get(Calendar.DAY_OF_WEEK) && c == 6)
            c -= 2;/*from  w w w  .ja  v a 2s.co  m*/

        if (d >= 32400)
            d -= 54000;

        return (32400 * 5) * a + 32400 * c + d;
    }

    protected static Calendar getRealCalendar(Timestamp time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time.getTime());

        if (c.get(Calendar.DAY_OF_WEEK) == 6 && c.get(Calendar.HOUR_OF_DAY) >= 19) {
            c.setTimeInMillis(c.getTimeInMillis() + 86400000 * 3);
            clearTime(c);
            return c;
        }

        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            c.setTimeInMillis(c.getTimeInMillis() + 86400000 * 2);
            clearTime(c);
            return c;
        }

        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            c.setTimeInMillis(c.getTimeInMillis() + 86400000);
            clearTime(c);
            return c;
        }

        if (c.get(Calendar.HOUR_OF_DAY) >= 10 && c.get(Calendar.HOUR_OF_DAY) < 19)
            return c;

        if (c.get(Calendar.HOUR_OF_DAY) >= 19)
            c.setTimeInMillis(c.getTimeInMillis() + 86400000);

        clearTime(c);
        return c;
    }

    protected static void clearTime(Calendar c) {
        c.set(Calendar.HOUR_OF_DAY, 10);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
    }
}

Related

  1. adjustTimestamp(Timestamp timestamp, String timezone, int gmtOffset)
  2. afterMinutes(Timestamp date, long min)
  3. argMapTimestamp(Map argMap, Map argMapNotUsed, String key)
  4. beforeTimestamp2Safety(Timestamp ts1, Timestamp ts2)
  5. between(Timestamp t1, Timestamp t2)
  6. calendar2Timestamp(Calendar c)
  7. calendarToTimestamp(java.util.Calendar inCal)
  8. calToTimeStamp(Calendar cal)
  9. changeDate(Timestamp date, Integer amount, Integer unit)