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:elw.web.FormatTool.java

License:Open Source License

public String format(final long dateLong, String pattern, String patternWeek, String patternToday) {
    final DateTimeFormatter formatter = lookupFormatter(pattern);
    final DateTimeFormatter formatterToday = lookupFormatter(patternToday);
    final DateTimeFormatter formatterWeek = lookupFormatter(patternWeek);

    final DateTime now = new DateTime(System.currentTimeMillis());
    final DateTime midnight = new DateTime(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), 0, 0, 0,
            0);/*from w  w w.  j a v  a 2 s  .c  om*/

    if (midnight.isBefore(dateLong) && midnight.plusDays(1).isAfter(dateLong)) {
        return formatterToday.print(dateLong);
    } else if (now.minusDays(7).isBefore(dateLong) && now.plusDays(7).isAfter(dateLong)) {
        return formatterWeek.print(dateLong);
    } else {
        return formatter.print(dateLong);
    }
}

From source file:es.jpons.temporal.types.PossibilisticVTP.java

License:Open Source License

/**
 * Constructor/*from  w w w  .  ja  v a2  s  .  c o  m*/
 *
 * @param sday Starting day of month
 * @param smonth Starting month of year
 * @param syear Starting year
 * @param sLeftDays Number of days of the left starting margin.
 * @param sRightDays Number of days of the right starting margin.
 * @param eday Ending day of month.
 * @param emonth Ending month of year.
 * @param eyear Ending year.
 * @param eLeftDays Number of days of the ending margin.
 * @param eRightDays Number of days of the right ending margin.
 */
public PossibilisticVTP(Integer sday, Integer smonth, Integer syear, Integer sLeftDays, Integer sRightDays,
        Integer eday, Integer emonth, Integer eyear, Integer eLeftDays, Integer eRightDays) {
    DateTime dateStartingMainPoint, dateStartingLeft, dateStartingRight;
    DateTime dateEndingMainPoint, dateEndingLeft, dateEndingRight;

    dateStartingMainPoint = new DateTime(syear, smonth, sday, 0, 0);

    dateStartingLeft = dateStartingMainPoint.minusDays(sLeftDays);
    dateStartingRight = dateStartingMainPoint.plusDays(sRightDays);

    Duration d = new Duration(dateStartingLeft, dateStartingMainPoint);
    Duration d1 = new Duration(dateStartingMainPoint, dateStartingRight);

    dateEndingMainPoint = new DateTime(eyear, emonth, eday, 0, 0);
    dateEndingLeft = dateEndingMainPoint.minusDays(eLeftDays);
    dateEndingRight = dateEndingMainPoint.plusDays(eRightDays);

    Duration d2 = new Duration(dateEndingLeft, dateEndingMainPoint);
    Duration d3 = new Duration(dateEndingMainPoint, dateEndingRight);

    //            PossibilisticTP start, end;
    PossibilisticVTP pvp;

    //            
    //            start = new PossibilisticTP(
    //                    dateStartingMainPoint.toDate(),
    //                    dateStartingLeft.toDate(),
    //                    dateStartingRight.toDate());
    //            end = new PossibilisticTP(   
    //                    dateEndingMainPoint.toDate(),
    //                    dateEndingLeft.toDate(),
    //                    dateEndingRight.toDate());

    //PossibilisticVTP(dateStartingMainPoint.toDate(),d.getMillis(),d1.getMillis(),dateEndingMainPoint.toDate(),d2.getMillis(),d3.getMillis());
    this.startMP = dateStartingMainPoint.getMillis();
    this.startLeft = d.getMillis();
    this.startRight = d1.getMillis();
    this.endMP = dateEndingMainPoint.getMillis();
    this.endLeft = d2.getMillis();
    this.endRight = d3.getMillis();
    this.startDate = dateStartingMainPoint.toDate();
    this.endDate = dateEndingMainPoint.toDate();

    convertToTM();

}

From source file:es.jpons.temporal.types.PossibilisticVTP.java

License:Open Source License

public PossibilisticVTP(Integer day, Integer month, Integer year, Integer leftDays, Integer rightDays,
        OpenInterval side) {/*from www .ja  va 2 s .c  o m*/
    DateTime dateMainPoint, dateLeft, dateRight;

    dateMainPoint = new DateTime(year, month, day, 0, 0);

    dateLeft = dateMainPoint.minusDays(leftDays);
    dateRight = dateMainPoint.plusDays(rightDays);

    Duration d = new Duration(dateLeft, dateMainPoint);
    Duration d1 = new Duration(dateMainPoint, dateRight);

    switch (side) {
    case UC:
        this.startMP = dateMainPoint.getMillis();
        this.startLeft = d.getMillis();
        this.startRight = d1.getMillis();
        this.startDate = dateMainPoint.toDate();
        this.side = OpenInterval.UC;
        break;
    case FB:
        this.endMP = dateMainPoint.getMillis();
        this.endLeft = d.getMillis();
        this.endRight = d1.getMillis();
        this.endDate = dateMainPoint.toDate();
        this.side = OpenInterval.FB;
        break;
    }

}

From source file:es.ugr.decsai.jpons.testapplication.Main.java

License:Open Source License

protected static PossibilisticVTP createVTP(Integer sday, Integer smonth, Integer syear, Integer eday,
        Integer emonth, Integer eyear) {
    DateTime dateStartingMainPoint, dateStartingLeft, dateStartingRight;
    DateTime dateEndingMainPoint, dateEndingLeft, dateEndingRight;

    dateStartingMainPoint = new DateTime(syear, smonth, sday, 0, 0);

    dateStartingLeft = dateStartingMainPoint.minusDays(3);
    dateStartingRight = dateStartingMainPoint.plusDays(3);

    Duration d = new Duration(dateStartingLeft, dateStartingMainPoint);
    Duration d1 = new Duration(dateStartingMainPoint, dateStartingRight);

    dateEndingMainPoint = new DateTime(eyear, emonth, eday, 0, 0);
    dateEndingLeft = dateEndingMainPoint.minusDays(3);
    dateEndingRight = dateEndingMainPoint.plusDays(3);

    Duration d2 = new Duration(dateEndingLeft, dateEndingMainPoint);
    Duration d3 = new Duration(dateEndingMainPoint, dateEndingRight);

    PossibilisticTP start, end;/*from w ww  . j a v  a 2  s. co m*/
    PossibilisticVTP pvp;

    //            start = new PossibilisticTP(
    //                    dateStartingMainPoint.toDate(),
    //                    dateStartingLeft.toDate(),
    //                    dateStartingRight.toDate());
    //            end = new PossibilisticTP(   
    //                    dateEndingMainPoint.toDate(),
    //                    dateEndingLeft.toDate(),
    //                    dateEndingRight.toDate());

    pvp = new PossibilisticVTP(dateStartingMainPoint.toDate(), d.getMillis(), d1.getMillis(),
            dateEndingMainPoint.toDate(), d2.getMillis(), d3.getMillis());
    return pvp;
}

From source file:es.usc.citius.servando.calendula.scheduling.DailyAgenda.java

License:Open Source License

public void setupForToday(Context ctx, final boolean force) {

    final SharedPreferences settings = ctx.getSharedPreferences(PREFERENCES_NAME, 0);
    final Long lastDate = settings.getLong(PREF_LAST_DATE, 0);
    final DateTime now = DateTime.now();

    Log.d(TAG, "Setup daily agenda. Last updated: " + new DateTime(lastDate).toString("dd/MM - kk:mm"));

    Interval today = new Interval(now.withTimeAtStartOfDay(), now.withTimeAtStartOfDay().plusDays(1));

    // we need to update daily agenda
    if (!today.contains(lastDate) || force) {
        // Start transaction
        try {/*from  w  w w . j av a 2 s  .  co m*/
            TransactionManager.callInTransaction(DB.helper().getConnectionSource(), new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    if (!force) {
                        LocalDate yesterday = now.minusDays(1).toLocalDate();
                        LocalDate tomorrow = now.plusDays(1).toLocalDate();

                        // delete items older than yesterday
                        DB.dailyScheduleItems().removeOlderThan(yesterday);
                        // delete items beyond tomorrow (only possible when changing date)
                        DB.dailyScheduleItems().removeBeyond(tomorrow);
                    } else {
                        DB.dailyScheduleItems().removeAll();
                    }
                    // and add new ones
                    createDailySchedule(now);
                    // Save last date to prefs
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putLong(PREF_LAST_DATE, now.getMillis());
                    editor.commit();
                    return null;
                }
            });
        } catch (SQLException e) {
            if (!force) {
                Log.e(TAG, "Error setting up daily agenda. Retrying with force = true", e);
                // setup with force, destroy current daily agenda but continues working
                setupForToday(ctx, true);
            } else {
                Log.e(TAG, "Error setting up daily agenda", e);
            }
        }
        // Update alarms
        AlarmScheduler.instance().updateAllAlarms(ctx);
        CalendulaApp.eventBus().post(new AgendaUpdatedEvent());
    } else {
        Log.d(TAG, "No need to update daily schedule (" + DailyScheduleItem.findAll().size()
                + " items found for today)");
    }
}

From source file:fr.ybonnel.model.ScoreWithHistory.java

License:Apache License

private void aggregateOneTypeOfScore(DateTime now, Iterator<Score> itScores) {
    Set<String> hoursKept = new HashSet<>();
    while (itScores.hasNext()) {
        Score score = itScores.next();/*  www  . j  av a  2s. co  m*/
        DateTime dateTimeOfScore = new DateTime(score.getTimeOfScore());
        String hourOfScore = dateTimeOfScore.toString("yyyyMMddHH");
        String hoursForPast1Day = dateTimeOfScore.withHourOfDay((dateTimeOfScore.getHourOfDay() / 4) * 4)
                .toString("yyyyMMddHH");
        if (dateTimeOfScore.compareTo(now.minusHours(2)) < 0 && hoursKept.contains(hourOfScore)
                || dateTimeOfScore.compareTo(now.minusDays(1)) < 0 && hoursKept.contains(hoursForPast1Day)) {
            itScores.remove();
        } else {
            hoursKept.add(hourOfScore);
            hoursKept.add(hoursForPast1Day);
        }
    }
}

From source file:galileonews.setup.service.SetupService.java

License:Apache License

public void execute(String server, String port, String user, String password)
        throws ClassNotFoundException, SQLException {

    createDb(server, port, user, password);

    String jdbcUrl = "jdbc:mysql://" + server + ":" + port + "/galileonews";

    try (Connection conn = DriverManager.getConnection(jdbcUrl, "galileonews", "galileonews");
            Statement stmt = conn.createStatement()) {

        UsersTable usersTable = new UsersTable();
        RolesTable rolesTable = new RolesTable();
        UsersRolesTable usersRolesTable = new UsersRolesTable();
        UrlsRolesTable urlsRolesTable = new UrlsRolesTable();
        ConfigsTable configsTable = new ConfigsTable();
        NewsTable newsTable = new NewsTable();
        GtidsTable gtidsTable = new GtidsTable();
        AttachmentTable attachmentTable = new AttachmentTable();

        urlsRolesTable.drop(stmt);/*from   ww w. j  a va  2  s  .  co  m*/
        usersRolesTable.drop(stmt);
        usersTable.drop(stmt);
        rolesTable.drop(stmt);
        configsTable.drop(stmt);
        attachmentTable.drop(stmt);
        newsTable.drop(stmt);
        gtidsTable.drop(stmt);

        usersTable.create(stmt);
        rolesTable.create(stmt);
        usersRolesTable.create(stmt);
        urlsRolesTable.create(stmt);
        configsTable.create(stmt);
        newsTable.create(stmt);
        attachmentTable.create(stmt);
        gtidsTable.create(stmt);

        String roleName = "ADM";
        String roleDesc = "Administrator";
        String roleMenu = "adm.xhtml";
        final int adminRoleId = rolesTable.insert(roleName, roleDesc, roleMenu, conn);

        roleName = "USR";
        roleDesc = "User";
        roleMenu = "user.xhtml";
        final int userRoleId = rolesTable.insert(roleName, roleDesc, roleMenu, conn);

        Map paramMap = new HashMap<>();
        String adminName1 = "adm1";
        paramMap.put("user_name", adminName1);
        String userPassword = hashPassword("adm1");
        paramMap.put("user_password", userPassword);
        paramMap.put("user_full_name", "Administrator One");
        paramMap.put("user_desc", "Created By Setup");
        paramMap.put("user_email", "-");
        final int adminUserId1 = usersTable.insert(paramMap, conn);
        usersRolesTable.insert(adminUserId1, adminRoleId, conn);

        paramMap = new HashMap<>();
        String userName1 = "user1";
        userPassword = hashPassword("user1");
        paramMap.put("user_name", userName1);
        paramMap.put("user_password", userPassword);
        paramMap.put("user_full_name", "User One");
        paramMap.put("user_desc", "Created By Setup");
        final int userId1 = usersTable.insert(paramMap, conn);

        paramMap = new HashMap<>();
        String userName2 = "user2";
        userPassword = hashPassword("user2");
        paramMap.put("user_name", userName2);
        paramMap.put("user_password", userPassword);
        paramMap.put("user_full_name", "User Two");
        paramMap.put("user_desc", "Created By Setup");
        final int userId2 = usersTable.insert(paramMap, conn);

        usersRolesTable.insert(userId1, userRoleId, conn);
        usersRolesTable.insert(userId2, userRoleId, conn);

        urlsRolesTable.insertAdm(adminRoleId, conn);
        urlsRolesTable.insertUser(userRoleId, conn);

        DateTime today = new DateTime();
        paramMap = new HashMap<>();
        StringBuilder newsText = new StringBuilder();
        newsText.append("Berita 1\n\n");
        newsText.append("Baris 1\n");
        newsText.append("Baris 2\n");
        newsText.append("Baris 3\n");
        paramMap.put("news_text", newsText.toString());
        paramMap.put("news_valid_from", today.getMillis());
        paramMap.put("news_valid_to", today.plusDays(1).getMillis());
        paramMap.put("news_important", false);
        paramMap.put("news_pcc", "*");
        newsTable.insert(paramMap, conn);
        paramMap = new HashMap<>();

        newsText = new StringBuilder();
        newsText.append("Berita 2\n\n");
        newsText.append("Baris 1\n");
        newsText.append("Baris 2\n\n");
        newsText.append("Baris 3\n");
        paramMap.put("news_text", newsText.toString());
        paramMap.put("news_valid_from", today.getMillis());
        paramMap.put("news_valid_to", today.plusDays(1).getMillis());
        paramMap.put("news_important", false);
        paramMap.put("news_pcc", "*");
        newsTable.insert(paramMap, conn);

        newsText = new StringBuilder();
        newsText.append("Berita 3\n\n");
        newsText.append("Baris 1\n");
        newsText.append("Baris 2\n");
        newsText.append("Baris 3\n");
        newsText.append("Baris 4\n");
        paramMap.put("news_text", newsText.toString());
        paramMap.put("news_valid_from", today.getMillis());
        paramMap.put("news_valid_to", today.plusDays(1).getMillis());
        paramMap.put("news_important", true);
        paramMap.put("news_pcc", "756O");
        newsTable.insert(paramMap, conn);

        newsText = new StringBuilder();
        newsText.append("Berita 4\n\n");
        newsText.append("Baris 1\n");
        newsText.append("Baris 2\n");
        newsText.append("Baris 3\n");
        newsText.append("Baris 4\n");
        paramMap.put("news_text", newsText.toString());
        paramMap.put("news_valid_from", today.plusDays(1).getMillis());
        paramMap.put("news_valid_to", today.plusDays(2).getMillis());
        paramMap.put("news_important", false);
        paramMap.put("news_pcc", "756O");
        newsTable.insert(paramMap, conn);

        newsText = new StringBuilder();
        newsText.append("Berita 5\n\n");
        newsText.append("Baris 1\n");
        newsText.append("Baris 2\n\n");
        newsText.append("Baris 3\n");
        paramMap.put("news_text", newsText.toString());
        paramMap.put("news_valid_from", today.plusDays(1).getMillis());
        paramMap.put("news_valid_to", today.plusDays(2).getMillis());
        paramMap.put("news_important", false);
        paramMap.put("news_pcc", "*");
        newsTable.insert(paramMap, conn);

        newsText = new StringBuilder();
        newsText.append("Berita 6\n\n");
        newsText.append("Baris 1\n");
        newsText.append("Baris 2\n");
        newsText.append("Baris 3\n");
        newsText.append("Baris 4\n");
        paramMap.put("news_text", newsText.toString());
        paramMap.put("news_valid_from", today.plusDays(1).getMillis());
        paramMap.put("news_valid_to", today.plusDays(2).getMillis());
        paramMap.put("news_important", true);
        paramMap.put("news_pcc", "756O");
        newsTable.insert(paramMap, conn);

        newsText = new StringBuilder();
        newsText.append("Berita 7\n\n");
        newsText.append("Baris 1\n");
        newsText.append("Baris 2\n");
        newsText.append("Baris 3\n");
        newsText.append("Baris 4\n");
        paramMap.put("news_text", newsText.toString());
        paramMap.put("news_valid_from", today.minusDays(1).getMillis());
        paramMap.put("news_valid_to", today.plusDays(2).getMillis());
        paramMap.put("news_important", true);
        paramMap.put("news_pcc", "756O");
        newsTable.insert(paramMap, conn);

        newsText = new StringBuilder();
        newsText.append("Berita 8\n\n");
        newsText.append("Baris 1\n");
        newsText.append("Baris 2\n");
        newsText.append("Baris 3\n");
        newsText.append("Baris 4\n");
        paramMap.put("news_text", newsText.toString());
        paramMap.put("news_valid_from", today.minusDays(1).getMillis());
        paramMap.put("news_valid_to", today.plusDays(2).getMillis());
        paramMap.put("news_important", true);
        paramMap.put("news_pcc", "756O");
        newsTable.insert(paramMap, conn);

    }
}

From source file:gobblin.source.extractor.utils.Utils.java

License:Apache License

/**
 * Helper method for getting a value containing CURRENTDAY-1 or CURRENTHOUR-1 in the form yyyyMMddHHmmss
 * @param value// w w  w  . jav  a2  s .  com
 * @param timezone
 * @return
 */
public static long getLongWithCurrentDate(String value, String timezone) {
    if (Strings.isNullOrEmpty(value)) {
        return 0;
    }

    DateTime time = getCurrentTime(timezone);
    DateTimeFormatter dtFormatter = DateTimeFormat.forPattern(CURRENT_DATE_FORMAT).withZone(time.getZone());
    if (value.toUpperCase().startsWith(CURRENT_DAY)) {
        return Long.parseLong(
                dtFormatter.print(time.minusDays(Integer.parseInt(value.substring(CURRENT_DAY.length() + 1)))));
    }
    if (value.toUpperCase().startsWith(CURRENT_HOUR)) {
        return Long.parseLong(dtFormatter
                .print(time.minusHours(Integer.parseInt(value.substring(CURRENT_HOUR.length() + 1)))));
    }
    return Long.parseLong(value);
}

From source file:gr.cti.android.experimentation.controller.api.SmartphoneController.java

License:Open Source License

private Map<Long, Long> getLast7DaysTotalReadings(final Smartphone smartphone) {
    //Readings in the past 7 days
    final DateTime date = new DateTime().withMillisOfDay(0);
    final Set<Result> results = resultRepository.findByDeviceIdAndTimestampAfter(smartphone.getId(),
            date.minusDays(7).getMillis());

    return extractCounters(results, date);
}

From source file:gr.cti.android.experimentation.controller.ui.RestRankingController.java

License:Open Source License

@Deprecated
@ResponseBody/*from  w  w w.j a  v a 2s.com*/
@RequestMapping(value = "/statistics/{phoneId}", method = RequestMethod.GET, produces = "application/json")
public Map<Long, Long> statisticsByPhone(@PathVariable("phoneId") final String phoneId,
        final HttpServletResponse response) {

    final DateTime date = new DateTime().withMillisOfDay(0);
    final Set<Result> results = resultRepository.findByDeviceIdAndTimestampAfter(Integer.parseInt(phoneId),
            date.minusDays(7).getMillis());

    return extractCounters(results, date);
}