Example usage for org.apache.commons.lang.time DateUtils addHours

List of usage examples for org.apache.commons.lang.time DateUtils addHours

Introduction

In this page you can find the example usage for org.apache.commons.lang.time DateUtils addHours.

Prototype

public static Date addHours(Date date, int amount) 

Source Link

Document

Adds a number of hours to a date returning a new object.

Usage

From source file:com.ikanow.aleph2.core.shared.utils.TimeSliceDirUtils.java

/** Low level util because java8 time "plus" is odd
 * @param to_adjust/*from   w w w . j  a v a2 s .c o m*/
 * @param increment
 * @return
 */
private static Date adjustTime(Date to_adjust, ChronoUnit increment) {
    return Patterns.match(increment).<Date>andReturn()
            .when(t -> t == ChronoUnit.SECONDS, __ -> DateUtils.addSeconds(to_adjust, 1))
            .when(t -> t == ChronoUnit.MINUTES, __ -> DateUtils.addMinutes(to_adjust, 1))
            .when(t -> t == ChronoUnit.HOURS, __ -> DateUtils.addHours(to_adjust, 1))
            .when(t -> t == ChronoUnit.DAYS, __ -> DateUtils.addDays(to_adjust, 1))
            .when(t -> t == ChronoUnit.WEEKS, __ -> DateUtils.addWeeks(to_adjust, 1))
            .when(t -> t == ChronoUnit.MONTHS, __ -> DateUtils.addMonths(to_adjust, 1))
            .when(t -> t == ChronoUnit.YEARS, __ -> DateUtils.addYears(to_adjust, 1)).otherwiseAssert();
}

From source file:mitm.common.sms.hibernate.SMSGatewayDAOTest.java

@Test
public void testGetAllSMS() throws DatabaseException {
    executor.executeTransaction(new DatabaseAction<Long>() {
        @Override/* w  w w .  ja va  2s.com*/
        public Long doAction(Session session) throws DatabaseException {
            SMS sms = new SMSImpl("123456", "sms body", "some extra data");

            return addSMSAction(session, sms, DateUtils.addHours(new Date(), -1));
        }
    });

    executor.executeTransaction(new DatabaseAction<Long>() {
        @Override
        public Long doAction(Session session) throws DatabaseException {
            SMS sms = new SMSImpl("789", "qwerty", "111");

            return addSMSAction(session, sms, DateUtils.addHours(new Date(), -2));
        }
    });

    executor.executeTransaction(new DatabaseAction<Long>() {
        @Override
        public Long doAction(Session session) throws DatabaseException {
            SMS sms = new SMSImpl("1111", "asasadsa", null);

            return addSMSAction(session, sms, DateUtils.addHours(new Date(), -2));
        }
    });

    List<SMS> all = executor.executeTransaction(new DatabaseAction<List<SMS>>() {
        @Override
        public List<SMS> doAction(Session session) throws DatabaseException {
            return getAllAction(session, null, null, SortColumn.PHONENUMBER, SortDirection.ASC);
        }
    });

    assertEquals(3, all.size());
    assertEquals("1111", all.get(0).getPhoneNumber());
    assertEquals("123456", all.get(1).getPhoneNumber());
    assertEquals("789", all.get(2).getPhoneNumber());

    all = executor.executeTransaction(new DatabaseAction<List<SMS>>() {
        @Override
        public List<SMS> doAction(Session session) throws DatabaseException {
            return getAllAction(session, null, null, SortColumn.PHONENUMBER, SortDirection.DESC);
        }
    });

    assertEquals(3, all.size());
    assertEquals("789", all.get(0).getPhoneNumber());
    assertEquals("123456", all.get(1).getPhoneNumber());
    assertEquals("1111", all.get(2).getPhoneNumber());

    all = executor.executeTransaction(new DatabaseAction<List<SMS>>() {
        @Override
        public List<SMS> doAction(Session session) throws DatabaseException {
            return getAllAction(session, 1, 2, SortColumn.PHONENUMBER, SortDirection.DESC);
        }
    });

    assertEquals(2, all.size());
    assertEquals("123456", all.get(0).getPhoneNumber());
    assertEquals("1111", all.get(1).getPhoneNumber());

    all = executor.executeTransaction(new DatabaseAction<List<SMS>>() {
        @Override
        public List<SMS> doAction(Session session) throws DatabaseException {
            return getAllAction(session, 1, 2, SortColumn.PHONENUMBER, SortDirection.ASC);
        }
    });

    assertEquals(2, all.size());
    assertEquals("123456", all.get(0).getPhoneNumber());
    assertEquals("789", all.get(1).getPhoneNumber());
}

From source file:iddb.runtime.db.model.dao.impl.mysql.UserDAOImpl.java

@Override
public String findPassKey(String passkey, Integer hoursLimit) {
    String sql = "select * from user_pass_rec where created between ? and ? and passkey = ? limit 1";
    Connection conn = null;/*www  .  j  a  v a  2s .c o  m*/
    String value = null;
    try {
        conn = ConnectionFactory.getMasterConnection();
        PreparedStatement st = conn.prepareStatement(sql);
        Date limit = DateUtils.addHours(new Date(), Math.abs(hoursLimit) * -1);
        st.setTimestamp(1, new Timestamp(limit.getTime()));
        st.setTimestamp(2, new Timestamp(new Date().getTime()));
        st.setString(3, passkey);
        ResultSet rs = st.executeQuery();
        if (rs.next()) {
            value = rs.getString("loginid");
        }
    } catch (SQLException e) {
        logger.error("findPassKey: {}", e);
    } catch (IOException e) {
        logger.error("findPassKey: {}", e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
    return value;
}

From source file:com.hdfstoftp.main.HdfsToFtp.java

public static Long[] parseTimeRange(CommandLine commandLine) throws ParseException, java.text.ParseException {

    SimpleDateFormat sdf = null;/*from   w w w .ja  va  2 s  .c  o m*/
    Long beginTime = null;
    Long endTime = null;

    if (commandLine.hasOption("d")) {
        sdf = new SimpleDateFormat(Config.FORMAT_DATE);
        Date beginDate = sdf.parse(commandLine.getOptionValue("d").trim());
        beginTime = beginDate.getTime();
        Date endDate = DateUtils.addDays(beginDate, 1);
        endTime = endDate.getTime();
    } else if (commandLine.hasOption("h")) {
        sdf = new SimpleDateFormat(Config.FORMAT_HOUR);
        Date beginDate = sdf.parse(commandLine.getOptionValue("h").trim());
        beginTime = beginDate.getTime();
        Date endDate = DateUtils.addHours(beginDate, 1);
        endTime = endDate.getTime();

    } else if (commandLine.hasOption("t")) {
        sdf = new SimpleDateFormat(Config.FORMAT_SS);
        Date beginDate = sdf.parse(commandLine.getOptionValue("t").trim());
        beginTime = beginDate.getTime();
    }

    Long[] timeRange = { beginTime, endTime };
    return timeRange;
}

From source file:iddb.runtime.db.model.dao.impl.mysql.UserDAOImpl.java

@Override
public Integer cleanUp(Integer hoursLimit) {
    String sql = "delete from user_pass_rec where created < ?";
    Connection conn = null;//from   w w w  .j  a  v  a2  s.co  m
    Integer res = 0;
    try {
        conn = ConnectionFactory.getMasterConnection();
        PreparedStatement st = conn.prepareStatement(sql);
        Date limit = DateUtils.addHours(new Date(), Math.abs(hoursLimit) * -1);
        st.setTimestamp(1, new Timestamp(limit.getTime()));
        res = st.executeUpdate();
    } catch (SQLException e) {
        logger.error("cleanUp: {}", e);
    } catch (IOException e) {
        logger.error("cleanUp: {}", e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
    return res;
}

From source file:mitm.common.sms.hibernate.SMSGatewayDAOTest.java

@Test
public void testGetAvailableCount() throws DatabaseException {
    executor.executeTransaction(new DatabaseAction<Long>() {
        @Override//from   w w  w .  j  ava  2  s .  c  o m
        public Long doAction(Session session) throws DatabaseException {
            SMS sms = new SMSImpl("123456", "sms body", "some extra data");

            return addSMSAction(session, sms, DateUtils.addHours(new Date(), -1));
        }
    });

    executor.executeTransaction(new DatabaseAction<Long>() {
        @Override
        public Long doAction(Session session) throws DatabaseException {
            SMS sms = new SMSImpl("789", "qwerty", "111");

            return addSMSAction(session, sms, DateUtils.addHours(new Date(), -2));
        }
    });

    final Date notAfter = new Date();

    int count = executor.executeTransaction(new DatabaseAction<Integer>() {
        @Override
        public Integer doAction(Session session) throws DatabaseException {
            return getAvailableCountAction(session, notAfter);
        }
    });

    assertEquals(2, count);
}

From source file:mitm.common.sms.hibernate.SMSGatewayDAOTest.java

@Test
public void testGetAvailableCountNullDate() throws DatabaseException {
    executor.executeTransaction(new DatabaseAction<Long>() {
        @Override/*from  w  w  w.  ja v a 2s.com*/
        public Long doAction(Session session) throws DatabaseException {
            SMS sms = new SMSImpl("123456", "sms body", "some extra data");

            return addSMSAction(session, sms, DateUtils.addHours(new Date(), -1));
        }
    });

    executor.executeTransaction(new DatabaseAction<Long>() {
        @Override
        public Long doAction(Session session) throws DatabaseException {
            SMS sms = new SMSImpl("789", "qwerty", "111");

            return addSMSAction(session, sms, DateUtils.addHours(new Date(), -2));
        }
    });

    final Date notAfter = null;

    int count = executor.executeTransaction(new DatabaseAction<Integer>() {
        @Override
        public Integer doAction(Session session) throws DatabaseException {
            return getAvailableCountAction(session, notAfter);
        }
    });

    assertEquals(2, count);
}

From source file:mitm.common.sms.hibernate.SMSGatewayDAOTest.java

@Test
public void testGetAvailableCountOneMatch() throws DatabaseException {
    executor.executeTransaction(new DatabaseAction<Long>() {
        @Override/*from ww w .  j av a2  s.c  om*/
        public Long doAction(Session session) throws DatabaseException {
            SMS sms = new SMSImpl("123456", "sms body", "some extra data");

            return addSMSAction(session, sms, DateUtils.addHours(new Date(), 1));
        }
    });

    executor.executeTransaction(new DatabaseAction<Long>() {
        @Override
        public Long doAction(Session session) throws DatabaseException {
            SMS sms = new SMSImpl("789", "qwerty", "111");

            return addSMSAction(session, sms, DateUtils.addHours(new Date(), -2));
        }
    });

    final Date notAfter = new Date();

    int count = executor.executeTransaction(new DatabaseAction<Integer>() {
        @Override
        public Integer doAction(Session session) throws DatabaseException {
            return getAvailableCountAction(session, notAfter);
        }
    });

    assertEquals(1, count);
}

From source file:com.haulmont.timesheets.web.calendar.CalendarScreen.java

protected FactAndPlan[] calculateSummariesByWeeks() {
    Date start = firstDayOfMonth;
    java.util.Calendar javaCalendar = java.util.Calendar.getInstance(userSession.getLocale());
    javaCalendar.setMinimalDaysInFirstWeek(1);
    javaCalendar.setTime(firstDayOfMonth);
    int countOfWeeksInTheMonth = javaCalendar.getActualMaximum(java.util.Calendar.WEEK_OF_MONTH);
    Date lastDayOfMonth = DateUtils.addHours(DateTimeUtils.getLastDayOfMonth(firstDayOfMonth), 23);

    FactAndPlan[] summariesByWeeks = new FactAndPlan[countOfWeeksInTheMonth + 1];
    for (int i = 0; i < countOfWeeksInTheMonth; i++) {
        Date firstDayOfWeek = DateTimeUtils.getFirstDayOfWeek(start);
        Date lastDayOfWeek = DateUtils.addHours(DateTimeUtils.getLastDayOfWeek(start), 23);

        if (firstDayOfWeek.getTime() < firstDayOfMonth.getTime()) {
            firstDayOfWeek = firstDayOfMonth;
        }//from  w w w. j av  a 2 s.  c o m
        if (lastDayOfWeek.getTime() > lastDayOfMonth.getTime()) {
            lastDayOfWeek = lastDayOfMonth;
        }
        FactAndPlan summaryForTheWeek = new FactAndPlan();
        User currentOrSubstitutedUser = userSession.getCurrentOrSubstitutedUser();
        summaryForTheWeek.fact.setTime(validationTools.actualWorkHoursForPeriod(firstDayOfWeek, lastDayOfWeek,
                currentOrSubstitutedUser));
        summaryForTheWeek.plan.setTime(
                validationTools.workHoursForPeriod(firstDayOfWeek, lastDayOfWeek, currentOrSubstitutedUser));
        summariesByWeeks[i + 1] = summaryForTheWeek;
        start = DateUtils.addWeeks(start, 1);
    }
    return summariesByWeeks;
}

From source file:mitm.common.sms.hibernate.SMSGatewayDAOTest.java

@Test
public void testGetExpiredNoExpired() throws DatabaseException {
    executor.executeTransaction(new DatabaseAction<Long>() {
        @Override//from   w w w  .j a v a 2  s .  co  m
        public Long doAction(Session session) throws DatabaseException {
            SMS sms = new SMSImpl("123456", "sms body", "some extra data");

            return addSMSAction(session, sms, new Date());
        }
    });

    final Date olderThan = DateUtils.addHours(new Date(), -1);

    List<SMSGatewayEntity> entities = executor.executeTransaction(new DatabaseAction<List<SMSGatewayEntity>>() {
        @Override
        public List<SMSGatewayEntity> doAction(Session session) throws DatabaseException {
            return getExpiredAction(session, olderThan);
        }
    });

    assertNotNull(entities);
    assertEquals(0, entities.size());
}