Example usage for org.joda.time DateTime minus

List of usage examples for org.joda.time DateTime minus

Introduction

In this page you can find the example usage for org.joda.time DateTime minus.

Prototype

public DateTime minus(ReadablePeriod period) 

Source Link

Document

Returns a copy of this datetime with the specified period taken away.

Usage

From source file:kr.debop4j.timeperiod.test.samples.TimeRangePeriodRelationTestData.java

License:Apache License

public TimeRangePeriodRelationTestData(DateTime start, DateTime end, Duration duration) {
    Guard.shouldBe(duration.compareTo(Duration.ZERO) >= 0,
            "duration? 0??? ?  .");

    setReference(new TimeRange(start, end, true));

    DateTime beforeEnd = start.minus(duration);
    DateTime beforeStart = beforeEnd.minus(reference.getDuration());
    DateTime insideStart = start.plus(duration);
    DateTime insideEnd = end.minus(duration);
    DateTime afterStart = end.plus(duration);
    DateTime afterEnd = afterStart.plus(reference.getDuration());

    after = new TimeRange(beforeStart, beforeEnd, true);
    startTouching = new TimeRange(beforeStart, start, true);
    startInside = new TimeRange(beforeStart, insideStart, true);
    insideStartTouching = new TimeRange(start, afterStart, true);
    enclosingStartTouching = new TimeRange(start, insideEnd, true);
    enclosing = new TimeRange(insideStart, insideEnd, true);
    enclosingEndTouching = new TimeRange(insideStart, end, true);
    exactMatch = new TimeRange(start, end, true);
    inside = new TimeRange(beforeStart, afterEnd, true);
    insideEndTouching = new TimeRange(beforeStart, end, true);
    endInside = new TimeRange(insideEnd, afterEnd, true);
    endTouching = new TimeRange(end, afterEnd, true);
    before = new TimeRange(afterStart, afterEnd, true);

    allPeriods.add(reference);/*  ww  w.j av a  2 s .c om*/
    allPeriods.add(after);
    allPeriods.add(startTouching);
    allPeriods.add(startInside);
    allPeriods.add(insideStartTouching);
    allPeriods.add(enclosingStartTouching);
    allPeriods.add(enclosing);
    allPeriods.add(enclosingEndTouching);
    allPeriods.add(exactMatch);
    allPeriods.add(inside);
    allPeriods.add(insideEndTouching);
    allPeriods.add(endInside);
    allPeriods.add(endTouching);
    allPeriods.add(before);
}

From source file:kr.debop4j.timeperiod.TimeBlock.java

License:Apache License

public TimeBlock(Duration duration, DateTime end, boolean readonly) {
    super(null, end, readonly);
    assertValidDuration(duration);/* ww w  .j av a 2 s  .c  o m*/
    this.duration = duration;
    this.start = end.minus(duration);
}

From source file:kr.debop4j.timeperiod.TimeCalendar.java

License:Apache License

@Override
public DateTime unmapStart(final DateTime moment) {
    shouldNotBeNull(moment, "moment");
    return moment.compareTo(TimeSpec.MinPeriodTime) > 0 ? moment.minus(startOffset) : moment;
}

From source file:kr.debop4j.timeperiod.TimeCalendar.java

License:Apache License

@Override
public DateTime unmapEnd(final DateTime moment) {
    shouldNotBeNull(moment, "moment");
    return moment.compareTo(TimeSpec.MaxPeriodTime) < 0 ? moment.minus(endOffset) : moment;
}

From source file:kr.debop4j.timeperiod.tools.Times.java

License:Apache License

/**  ??   ?? ? */
public static DateTime ago(DateTime moment, Duration duration) {
    return moment.minus(duration);
}

From source file:main.MessagesService.java

License:Apache License

public Map<String, Object> getEventsInfoFromDB2(HttpServletRequest request, boolean reset,
        DateTime callTimestamp) {
    HttpSession session = request.getSession(true);

    System.out.println("getEventsInfoFromDB2 requested session id=" + request.getRequestedSessionId());
    System.out.println("sessionid=" + session.getId());

    if (!session.getId().equalsIgnoreCase(request.getRequestedSessionId()))
        reset = true;//w ww .  j a  v  a 2 s .c o m

    EventsInfo PreviousEventsInfo = (EventsInfo) session.getAttribute("PreviousEventsInfo");
    AlertInfo LastAlert = (AlertInfo) session.getAttribute("LastAlert");

    long TimeOffsetMillis = 0;

    System.out.println("value from sesion=" + (session.getAttribute("TimeOffsetMillis2") == null ? "null"
            : session.getAttribute("TimeOffsetMillis2").toString()));

    if (reset) {
        //dataReset(request);

        PreviousEventsInfo = null;
        LastAlert = null;
        session.setAttribute("PreviousEventsInfo", PreviousEventsInfo);
        session.setAttribute("LastAlert", LastAlert);

    }

    //if(!reset)
    if (session.getAttribute("TimeOffsetMillis2") != null)
        TimeOffsetMillis = (long) session.getAttribute("TimeOffsetMillis2");

    System.out.println("TimeOffsetMillis2=" + TimeOffsetMillis);

    ClientEventsInfo clientEventsInfo = new ClientEventsInfo();
    List<ClientAlertInfo> alertInfoList = new LinkedList<ClientAlertInfo>();

    EventsInfo eventsInfo = null;
    String pdQuery = "";
    if (session.getAttribute("TimeOffsetMillis2") == null && !reset) {
        if (PreviousEventsInfo != null) {
            pdQuery = "select * from " + DataServices.PD_TBL_NAME + " where " + DataServices.PD_COLNAME_ID + ">"
                    + PreviousEventsInfo.Id + " order by " + DataServices.PD_COLNAME_TimeStamp + " asc limit 1";
        }

    }

    else {
        if (reset) {
            pdQuery = "select * from " + DataServices.PD_TBL_NAME + " order by "
                    + DataServices.PD_COLNAME_TimeStamp + " asc limit 1";
        } else {
            String condition = " where ";
            if (PreviousEventsInfo != null)
                condition += DataServices.PD_COLNAME_TimeStamp + ">'"
                        + PreviousEventsInfo.TimeStamp.minus(TimeOffsetMillis).toString(Utils.TimeFormat)
                        + "' and ";

            condition += DataServices.PD_COLNAME_TimeStamp + "<='"
                    + callTimestamp.minus(TimeOffsetMillis).toString(Utils.TimeFormat) + "' ";

            pdQuery = "select * from " + DataServices.PD_TBL_NAME + condition + " order by "
                    + DataServices.PD_COLNAME_TimeStamp + " desc limit 1";
        }

    }

    ResultSet rs;

    Connection dbcon = null;
    try {

        dbcon = DataServices.connectToDB(getDataConnectionString());

        if (!Utils.isStringNullOrEmpty(pdQuery)) {
            System.out.println(DateTime.now().toString(Utils.TimeFormat) + " running pdQuery: " + pdQuery);

            rs = DataServices.getResultSet(dbcon, pdQuery);
            if (rs.next()) {
                DateTime recordTimestamp = new DateTime(rs.getTimestamp(DataServices.PD_COLNAME_TimeStamp));

                if (reset || TimeOffsetMillis == 0) // first row
                {
                    DateTime systemTimestamp = callTimestamp;//.withZone(DateTimeZone.UTC);
                    TimeOffsetMillis = systemTimestamp.getMillis() - recordTimestamp.getMillis();
                    System.out.println("setting TimeOffsetMillis=" + TimeOffsetMillis);
                    session.setAttribute("TimeOffsetMillis2", TimeOffsetMillis);
                }

                eventsInfo = new EventsInfo();
                eventsInfo.Id = rs.getLong(DataServices.PD_COLNAME_ID);
                eventsInfo.NodeId = rs.getInt(DataServices.PD_COLNAME_NodeId);
                eventsInfo.TimeStamp = recordTimestamp.plus(TimeOffsetMillis);

                eventsInfo.TransactionsProcessed = rs.getLong(DataServices.PD_COLNAME_EventsProcessed);
                eventsInfo.TotalAlerts = rs.getLong(DataServices.PD_COLNAME_Total_Alerts);
                eventsInfo.UTF_Alerts = rs.getLong(DataServices.PD_COLNAME_UTF_Alerts);
                eventsInfo.EB1_Alerts = rs.getLong(DataServices.PD_COLNAME_EB1_Alerts);
                eventsInfo.EB2_Alerts = rs.getLong(DataServices.PD_COLNAME_EB2_Alerts);
                eventsInfo.NOD_Alerts = rs.getLong(DataServices.PD_COLNAME_NOD_Alerts);
                eventsInfo.OD1_Alerts = rs.getLong(DataServices.PD_COLNAME_OD1_Alerts);
                eventsInfo.OD2_Alerts = rs.getLong(DataServices.PD_COLNAME_OD2_Alerts);
                eventsInfo.OD3_Alerts = rs.getLong(DataServices.PD_COLNAME_OD3_Alerts);
                eventsInfo.LB_Alerts = rs.getLong(DataServices.PD_COLNAME_LB_Alets);
                eventsInfo.EndToEndLatency = rs.getLong(DataServices.PD_COLNAME_EventsLatency);
                eventsInfo.EngineLatencyForAlerts = rs.getLong(DataServices.PD_COLNAME_AlertsLatency);

            } else {
                eventsInfo = null;
                clientEventsInfo = null;

            }

            rs.close();
        }

        if (eventsInfo != null) {
            clientEventsInfo = new ClientEventsInfo(eventsInfo);
            if (PreviousEventsInfo != null) {
                //int duration = Seconds.secondsBetween(PreviousEventsInfo.TimeStamp, eventsInfo.TimeStamp).getSeconds();
                double durationInSecsonds = (eventsInfo.TimeStamp.getMillis()
                        - PreviousEventsInfo.TimeStamp.getMillis()) / 1000.0;

                if (durationInSecsonds > 0) {
                    clientEventsInfo.TransactionsPerSecond = (long) ((eventsInfo.TransactionsProcessed
                            - PreviousEventsInfo.TransactionsProcessed) / durationInSecsonds);
                    clientEventsInfo.AlertsPerSecond = (long) ((eventsInfo.TotalAlerts
                            - PreviousEventsInfo.TotalAlerts) / durationInSecsonds);
                } else {
                    clientEventsInfo.TransactionsPerSecond = 0;
                    clientEventsInfo.AlertsPerSecond = 0;
                }

                clientEventsInfo.EndToEndLatency = ((eventsInfo.EndToEndLatency
                        - PreviousEventsInfo.EndToEndLatency)
                        / (eventsInfo.TransactionsProcessed - PreviousEventsInfo.TransactionsProcessed))
                        / 1000.0;
            } else {
                clientEventsInfo.TransactionsPerSecond = 0;
                clientEventsInfo.AlertsPerSecond = 0;
            }
        }

        //alerts

        /*String alertCondition = "";
        DateTime nowTS = DateTime.now();
        if(LastAlert != null)
         alertCondition = " where " + DataServices.Alert_COLNAME_TimeStamp + ">'" + LastAlert.TimeStamp.minus(TimeOffsetMillis).toString(Utils.TimeFormat)+"' and ";
        else 
         alertCondition = " where ";
                
        alertCondition += DataServices.Alert_COLNAME_TimeStamp + "<='" + DateTime.now().minus(TimeOffsetMillis).toString(Utils.TimeFormat) + "'";;
                
        String alertsQuery = "select * from " + DataServices.Alert_TBL_NAME + alertCondition;
                
        System.out.println(DateTime.now().toString(Utils.TimeFormat) + " running alertsQuery: "+alertsQuery);
                
        rs = DataServices.getResultSet(dbcon, alertsQuery);
        if (rs.next())
        {
         do
         {
            AlertInfo alertInfo = new AlertInfo();
            alertInfo.AlertType = rs.getString(DataServices.Alert_COLNAME_AlertType);
            alertInfo.MobileNumber = rs.getString(DataServices.Alert_COLNAME_MobileNumber);
            alertInfo.TimeStamp = new DateTime(rs.getTimestamp(DataServices.Alert_COLNAME_TimeStamp)).plus(TimeOffsetMillis);
            alertInfo.AlertText = "Hi";
            alertInfo.Balance = rs.getFloat(DataServices.Alert_COLNAME_RunLedgerAmount);
            alertInfo.MessagePostTimeStamp = new DateTime(rs.getTimestamp(DataServices.Alert_COLNAME_MsgPostTimeStamp)).plus(TimeOffsetMillis);
            alertInfo.AlertPostTimeStamp = new DateTime(rs.getTimestamp(DataServices.Alert_COLNAME_AlertPostTimeStamp)).plus(TimeOffsetMillis);
                    
            alertInfo.AlertLatency = 
             (alertInfo.MessagePostTimeStamp.getMillis() - alertInfo.AlertPostTimeStamp.getMillis()) / 1000.0;
                    
            alertInfoList.add(new ClientAlertInfo(alertInfo));
                    
            LastAlert = alertInfo;
         }while(rs.next());
        }
        rs.close();*/

        if (dbcon != null)
            DataServices.closeConnection(dbcon);
    } catch (Exception ex) {

    }

    Map<String, Object> result = new LinkedHashMap<String, Object>();
    result.put("EventsInfo", clientEventsInfo);
    result.put("Alerts", alertInfoList);

    if (eventsInfo != null) {
        PreviousEventsInfo = eventsInfo;//caching
        session.setAttribute("PreviousEventsInfo", PreviousEventsInfo);
    }

    session.setAttribute("LastAlert", LastAlert);

    if (TimeOffsetMillis != 0)
        session.setAttribute("TimeOffsetMillis2", TimeOffsetMillis);

    System.out.println("leaving method getEventsInfoFromDB2 with session value="
            + session.getAttribute("TimeOffsetMillis2"));
    System.out.println("------------------------------------------------------------------");

    return result;
}

From source file:net.craigstars.dao.impl.UserSessionDataDaoImpl.java

License:MIT License

/**
 * Purge all the records that are older than (now - days)
 *///from  w  w w .  j a va 2s .  co  m
public void purgeOlderThan(int days) {
    Criteria crit = getSession().createCriteria(getPersistentClass());
    DateTime daysAgo = new DateTime();
    daysAgo = daysAgo.minus(Duration.standardDays(days));
    crit.add(Restrictions.le("createDate", daysAgo));
    for (Object obj : crit.list()) {
        getSession().delete(obj);
    }
}

From source file:net.lshift.diffa.config.WindowRefiner.java

License:Apache License

private WindowRefiner(String periodExpression, String offsetExpression, DateTime now) {
    this.periodExpression = periodExpression;
    this.offsetExpression = offsetExpression;

    DateTime end;
    if (offsetExpression == null || offsetExpression.equals("")) {
        end = now;//from  ww  w  .j av a2s .c  o m
    } else {
        DateTime startOfDay = now.toDateMidnight().toDateTime();
        end = startOfDay.plus(periodFormatter.parsePeriod(offsetExpression).toDurationFrom(startOfDay));
    }
    DateTime start = end.minus(periodFormatter.parsePeriod(periodExpression));
    this.windowInterval = new Interval(start, end);
}

From source file:net.schweerelos.timeline.model.Timeline.java

License:Open Source License

public Interval convertSliceToInterval(int row) {
    if (row > -1) {
        DateTime periodStart = start;//from   www .ja v  a  2  s. c  o  m
        for (int i = 0; i < row; i++) {
            Duration addDuration = increment.toDurationFrom(periodStart);
            periodStart = periodStart.plus(addDuration);
        }
        Duration addDuration = increment.toDurationFrom(periodStart);
        DateTime periodEnd = periodStart.plus(addDuration);
        if (periodEnd.isAfter(end)) {
            periodEnd = end;
        }
        periodEnd = periodEnd.minus(Minutes.ONE);
        return new Interval(periodStart, periodEnd);
    } else {
        return null;
    }
}

From source file:net.sourceforge.fenixedu.domain.accounting.PostingRule.java

License:Open Source License

public void deactivate(final DateTime when) {
    super.setEndDate(when.minus(10000));
}