Example usage for org.joda.time DateTime minusDays

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

Introduction

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

Prototype

public DateTime minusDays(int days) 

Source Link

Document

Returns a copy of this datetime minus the specified number of days.

Usage

From source file:com.ofalvai.bpinfo.util.UiUtils.java

License:Apache License

/**
 * Transforms the start and end timestamps into a human-friendly readable string,
 * with special replacements for special dates, times, and the API's strange notations.
 * @param context Context//from w w  w  . ja  v  a  2  s  . com
 * @param startTimestamp Start of the alert in seconds since the UNIX epoch
 * @param endTimestamp   End of the alert in seconds since the UNIX epoch
 * @return  A string in the format of [startdate] [starttime] [separator] [enddate] [endtime]
 */
@NonNull
public static String alertDateFormatter(Context context, long startTimestamp, long endTimestamp) {
    DateTime startDateTime = new DateTime(startTimestamp * 1000L);
    DateTime endDateTime = new DateTime(endTimestamp * 1000L);
    LocalDate startDate = startDateTime.toLocalDate();
    LocalDate endDate = endDateTime.toLocalDate();

    DateTime today = new DateTime();
    LocalDate todayDate = new DateTime().toLocalDate();
    DateTime yesterday = today.minusDays(1);
    LocalDate yesterdayDate = yesterday.toLocalDate();
    DateTime tomorrow = today.plusDays(1);
    LocalDate tomorrowDate = tomorrow.toLocalDate();

    // Alert start, date part
    String startDateString;
    if (startDate.equals(todayDate)) {
        // Start day is today, replacing month and day with today string
        startDateString = context.getString(R.string.date_today) + " ";
    } else if (startDate.year().get() < today.year().get()) {
        // The start year is less than the current year, displaying the year too
        startDateString = Config.FORMATTER_DATE_YEAR.print(startDateTime);
    } else if (startDate.equals(yesterdayDate)) {
        startDateString = context.getString(R.string.date_yesterday) + " ";
    } else if (startDate.equals(tomorrowDate)) {
        startDateString = context.getString(R.string.date_tomorrow) + " ";
    } else {
        startDateString = Config.FORMATTER_DATE.print(startDateTime);
    }

    // Alert start, time part
    String startTimeString;
    if (startDateTime.hourOfDay().get() == 0 && startDateTime.minuteOfHour().get() == 0) {
        // The API marks "first departure" as 00:00
        startTimeString = context.getString(R.string.date_first_departure);
    } else {
        startTimeString = Config.FORMATTER_TIME.print(startDateTime);
    }

    // Alert end, date part
    String endDateString;
    if (endTimestamp == 0) {
        // The API marks "until further notice" as 0 (in UNIX epoch), no need to display date
        // (the replacement string is displayed as the time part, not the date)
        endDateString = " ";
    } else if (endDate.year().get() > today.year().get()) {
        // The end year is greater than the current year, displaying the year too
        endDateString = Config.FORMATTER_DATE_YEAR.print(endDateTime);
    } else if (endDate.equals(todayDate)) {
        // End  day is today, replacing month and day with today string
        endDateString = context.getString(R.string.date_today) + " ";
    } else if (endDate.equals(yesterdayDate)) {
        endDateString = context.getString(R.string.date_yesterday) + " ";
    } else if (endDate.equals(tomorrowDate)) {
        endDateString = context.getString(R.string.date_tomorrow) + " ";
    } else {
        endDateString = Config.FORMATTER_DATE.print(endDateTime);
    }

    // Alert end, time part
    String endTimeString;
    if (endTimestamp == 0) {
        // The API marks "until further notice" as 0 (in UNIX epoch)
        endTimeString = context.getString(R.string.date_until_revoke);
    } else if (endDateTime.hourOfDay().get() == 23 && endDateTime.minuteOfHour().get() == 59) {
        // The API marks "last departure" as 23:59
        endTimeString = context.getString(R.string.date_last_departure);
    } else {
        endTimeString = Config.FORMATTER_TIME.print(endDateTime);
    }

    return startDateString + startTimeString + Config.DATE_SEPARATOR + endDateString + endTimeString;
}

From source file:com.pamarin.income.util.DateUtils.java

/**
 * @param date//from   w w  w .  j a v a 2 s.c  om
 * @param minusDate
 * @return
 */
public static Date minusDate(Date date, int minusDate) {
    DateTime dateTime = new DateTime(filterDate(date).getTime());
    DateTime minusDays = dateTime.minusDays(minusDate);
    return toStartTime(minusDays.toDate());
}

From source file:com.pandits.opensource.JodaTimeUtil.java

License:Open Source License

public Date retrogessBy(Date date, int numOfDays) {
    DateTime dateTime = createDateTime(date);
    return dateTime.minusDays(numOfDays).toDate();
}

From source file:com.pureblue.quant.util.frequency.Daily.java

@Override
public DateTime periodBegin(DateTime t) {
    DateTime currentDayStart = new DateTime(t.getYear(), t.getMonthOfYear(), t.getDayOfMonth(),
            time.getHourOfDay(), time.getMinuteOfHour(), time.getSecondOfMinute(), time.getMillisOfSecond(),
            t.getZone());//from w  w  w  .j a  v  a 2s  .  c om
    if (currentDayStart.isAfter(t)) {
        return currentDayStart.minusDays(1);
    } else {
        return currentDayStart;
    }
}

From source file:com.pureblue.quant.util.frequency.Weekly.java

@Override
public DateTime periodBegin(DateTime time) {
    int timeDifference = time.getDayOfWeek() - day.toJodaTimeValue();
    if (timeDifference < 0) {
        timeDifference += 7;//  w  w  w.  j a  va 2 s .c  o  m
    }
    DateTime startDay = time.minusDays(timeDifference);
    DateTime weekStart = new DateTime(startDay.getYear(), startDay.getMonthOfYear(), startDay.getDayOfMonth(),
            dayStartTime.getHourOfDay(), dayStartTime.getMinuteOfHour(), dayStartTime.getSecondOfMinute(),
            dayStartTime.getMillisOfSecond(), startDay.getZone());
    if (weekStart.isAfter(time)) {
        return weekStart.minusWeeks(1);
    } else {
        return weekStart;
    }
}

From source file:com.qcadoo.mes.basic.shift.Shift.java

License:Open Source License

private DateRange buildDateRangeFrom(final TimeRange timeRange, final Date date) {
    DateTime dateTime = new DateTime(date);
    DateTime midnight = dateTime.withTimeAtStartOfDay();
    DateTime from;//from w  w  w .j  a  v a 2 s . co  m
    DateTime to;
    if (timeRange.startsDayBefore()) {
        if (dateTime.toLocalTime().isBefore(timeRange.getFrom())) {
            from = timeRange.getFrom().toDateTime(midnight.minusDays(1));
            to = timeRange.getTo().toDateTime(midnight);
        } else {
            from = timeRange.getFrom().toDateTime(midnight);
            to = timeRange.getTo().toDateTime(midnight.plusDays(1));
        }
    } else {
        from = timeRange.getFrom().toDateTime(midnight);
        to = timeRange.getTo().toDateTime(midnight);
    }
    return new DateRange(from.toDate(), to.toDate());
}

From source file:com.qcadoo.view.internal.components.grid.PredefinedFilter.java

License:Open Source License

private static String evalExpression(final String expression) {
    DateTime today = new DateTime();
    DateTime date;/*  w w  w  . java2 s.  c o  m*/
    if ("today".equals(expression)) {
        date = today;
    } else if ("yesterday".equals(expression)) {
        date = today.minusDays(1);
    } else if ("tomorrow".equals(expression)) {
        date = today.plusDays(1);
    } else {
        throw new IllegalStateException("unsupported predefined filter expression: '" + expression + "'");
    }
    return new SimpleDateFormat(DateUtils.L_DATE_FORMAT, getLocale()).format(date.toDate());
}

From source file:com.reclabs.recomendar.common.helpers.types.DateHelper.java

License:Open Source License

/**
 * Subtract to the current date the amount of that precisionType represent.
 * @param date The date to subtract/*from   w w w  .jav a 2 s.co  m*/
 * @param amount The amount to subtract
 * @param precisionType The field to subtract
 * @return The new date
 */
public static Date subtract(Date date, int amount, DatePrecisionType precisionType) {
    DateTime jodaDate1 = new DateTime(date.getTime());
    DateTime result;
    switch (precisionType) {
    case YEAR:
        result = jodaDate1.minusYears(amount);
        break;
    case MONTH:
        result = jodaDate1.minusMonths(amount);
        break;
    case DAY:
        result = jodaDate1.minusDays(amount);
        break;
    case HOUR:
        result = jodaDate1.minusHours(amount);
        break;
    case MINUTE:
        result = jodaDate1.minusMinutes(amount);
        break;
    case SECOND:
        result = jodaDate1.minusSeconds(amount);
        break;
    case MILLISECOND:
        result = jodaDate1.minusMillis(amount);
        break;
    default:
        LOGGER.warn("[Error subtract, precision value is invalid: {}]", precisionType);
        throw new RecIllegalArgumentException("The precision value is invalid " + precisionType);
    }
    return result.toDate();
}

From source file:com.redprairie.moca.server.db.translate.filter.TU_FunctionMonthsBetween.java

License:Open Source License

public void testMonthsBetween() throws MocaException {
    String sql;//from   w w w .j  a  va 2s.c om

    sql = "select months_between(sysdate, sysdate) result from dual";
    runTest(sql, "result", 0);

    DateTime now = new DateTime();
    DateTime then = now.minusDays(60);

    // We have to find the correct difference of months.  Since we could
    // be crossing a year we have to make sure to normalize it if so by
    // adding 12 months to it.
    int monthDifference = (monthDifference = now.getMonthOfYear() - then.getMonthOfYear()) < 0
            ? monthDifference + 12
            : monthDifference;
    // There is technically still a race condition if the month of year
    // changes after we calculate it but before we execute that this could
    // fail when going to a month that differs in the number of months when
    // subtracting 60 days.
    sql = "select round(months_between(sysdate, sysdate-/*=moca_util.days(*/ 60 /*=)*/)) result from dual";
    runTest(sql, "result", monthDifference);

    sql = "select round(months_between(sysdate-/*=moca_util.days(*/ 60 /*=)*/, sysdate)) result from dual";
    runTest(sql, "result", -monthDifference);
}

From source file:com.rhythm.swagr.SwagrDAO.java

License:Apache License

private SwagrStructure timelog(SwagrOptionsPB req) throws Exception {
    Date start;//from   w  w  w  .jav a 2  s . co  m
    Date end;
    DateTime enddt = new DateTime();
    DateTime startdt = enddt.minusDays(30);
    String request = TIMELOG_DAY;
    if (req.hasStartDt() && req.hasEndDt()) {
        //    if((req.getStartDt()-req.getEndDt())<=604800)
        //        request = TIMELOG_HOUR;
        start = new Date(req.getStartDt());
        end = new Date(req.getEndDt());
    } else {
        start = new Date(startdt.getMillis());
        end = new Date(enddt.getMillis());
    }

    try (JdbcService jdbc = drjJdbcFac.newService(request)) {
        PreparedStatement ps = jdbc.getPreparedStatement();
        ps.setDate(1, start);
        ps.setDate(2, end);
        ps.setInt(3, req.getHostTypeId());
        ResultSet rs = jdbc.executeQuery();
        return timelogResSet(rs, req.getHostTypeId());
    }
}