Example usage for org.joda.time DateTime plusDays

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

Introduction

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

Prototype

public DateTime plusDays(int days) 

Source Link

Document

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

Usage

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);//  ww  w  .j av  a 2  s  .c  om
        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.ingestion.google.webmaster.ProducerJob.java

License:Apache License

public List<? extends ProducerJob> partitionJobs() {
    DateTime start = dateFormatter.parseDateTime(getStartDate());
    DateTime end = dateFormatter.parseDateTime(getEndDate());
    int days = Days.daysBetween(start, end).getDays();
    if (days <= 0) {
        return new ArrayList<>();
    }//w w  w. j a  va2s.  c  o m
    int step = days / 2;
    return Arrays.asList(
            new SimpleProducerJob(getPage(), getStartDate(), dateFormatter.print(start.plusDays(step))),
            new SimpleProducerJob(getPage(), dateFormatter.print(start.plusDays(step + 1)), getEndDate()));
}

From source file:google.registry.rde.PendingDepositChecker.java

License:Open Source License

private static DateTime advanceToDayOfWeek(DateTime date, int dayOfWeek) {
    while (date.getDayOfWeek() != dayOfWeek) {
        date = date.plusDays(1);
    }//ww w .ja  v  a 2s.  c  om
    return date;
}

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

License:Open Source License

private void fillMissingIntervals(TreeSet<Long> treeSet, String rollup, long toLong) {

    //TODO: add non existing intervals
    if (rollup.endsWith("d")) {
        DateTime firstDate = new DateTime(treeSet.iterator().next());

        while (firstDate.isBefore(toLong)) {
            firstDate = firstDate.plusDays(1);
            if (!treeSet.contains(firstDate.getMillis())) {
                treeSet.add(firstDate.getMillis());
            }/*from   w w w . ja va  2  s  . co  m*/
        }
    } else if (rollup.endsWith("h")) {
        DateTime firstDate = new DateTime(treeSet.iterator().next());

        while (firstDate.isBefore(toLong)) {
            firstDate = firstDate.plusHours(1);
            if (!treeSet.contains(firstDate.getMillis())) {
                treeSet.add(firstDate.getMillis());
            }
        }
    } else if (rollup.endsWith("m")) {
        DateTime firstDate = new DateTime(treeSet.iterator().next());

        while (firstDate.isBefore(toLong)) {
            firstDate = firstDate.plusMinutes(1);
            if (!treeSet.contains(firstDate.getMillis())) {
                treeSet.add(firstDate.getMillis());
            }
        }
    }
}

From source file:graph.inference.module.LaterThanWorker.java

License:Open Source License

private Interval parseDate(DAGNode date, DateTime now) {
    String dateStr = date.toString();
    if (dateStr.equals("Now") || dateStr.equals("Now-Generally"))
        return new Interval(now.getMillis(), now.getMillis() + 1);
    if (dateStr.equals("Today-Indexical"))
        return new Interval(now.dayOfYear().roundFloorCopy(), now.dayOfYear().roundCeilingCopy());
    if (dateStr.equals("Tomorrow-Indexical")) {
        return new Interval(now.plusDays(1).dayOfYear().roundFloorCopy(),
                now.plusDays(1).dayOfYear().roundCeilingCopy());
    }/*from  w w  w.j  a va  2s  .c  om*/
    if (dateStr.equals("Yesterday-Indexical")) {
        return new Interval(now.minusDays(1).dayOfYear().roundFloorCopy(),
                now.minusDays(1).dayOfYear().roundCeilingCopy());
    }
    if (dateStr.equals("TheYear-Indexical")) {
        return new Interval(now.year().roundFloorCopy(), now.year().roundCeilingCopy());
    }

    // Parse the date from the DAGNode
    String parsePattern = null;
    for (int i = DATE_PARSE_INTERVALS.length - 1; i >= 0; i--) {
        StringBuilder newPattern = new StringBuilder("(" + DATE_PARSE_INTERVALS[i]);
        if (parsePattern != null)
            newPattern.append(" " + parsePattern);
        newPattern.append(")");
        parsePattern = newPattern.toString();

        DateTimeFormatter dtf = DateTimeFormat.forPattern(parsePattern);
        try {
            DateTime dateTime = dtf.parseDateTime(dateStr);
            if (dateTime != null) {
                switch (i) {
                case 0:
                    return new Interval(dateTime.getMillis(),
                            dateTime.plusSeconds(1).minusMillis(1).getMillis());
                case 1:
                    return new Interval(dateTime.getMillis(),
                            dateTime.plusMinutes(1).minusMillis(1).getMillis());
                case 2:
                    return new Interval(dateTime.getMillis(), dateTime.plusHours(1).minusMillis(1).getMillis());
                case 3:
                    return new Interval(dateTime.getMillis(), dateTime.plusDays(1).minusMillis(1).getMillis());
                case 4:
                    return new Interval(dateTime.getMillis(),
                            dateTime.plusMonths(1).minusMillis(1).getMillis());
                case 5:
                    return new Interval(dateTime.getMillis(), dateTime.plusYears(1).minusMillis(1).getMillis());
                }
            }
        } catch (Exception e) {
        }
    }
    return null;
}

From source file:Gui.panels.analyticsguipanels.CondencePanel.java

public boolean is_inside_period(DateTime startdate, DateTime testdate) {

    switch (condence_value) {
    case 1://from www  . j  a  v  a 2s .c  o  m
        return false;
    case 2:
        return startdate.plusDays(1).isAfter(testdate);
    case 3:
        return startdate.plusDays(7).isAfter(testdate);
    case 4:
        return startdate.plusMonths(1).isAfter(testdate);
    }
    return false;
}

From source file:Implement.DAO.BookingDAOImpl.java

@Override
public void insertOfflineEngineBooking(String bookingDate, String tripDate, String tripTime, int noPackage,
        int packageID, String resourceNote, String customerName, String customerPhone, String customerEmail,
        String durationType, int duration, int smallestInterval, int providerID) {

    //  construct dynamic sql for used resources
    DateTimeFormatter formatterDateAndHour = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
    String travelTimeStr = tripDate;
    if (!durationType.equals("days")) {
        travelTimeStr += " " + tripTime + ":00";
    } else {//from  ww w  .j  ava 2  s. c om
        travelTimeStr += " 00:00:00";
    }
    DateTime travelTime = formatterDateAndHour.parseDateTime(travelTimeStr);
    DateTime endTime = new DateTime(travelTime);

    if (!durationType.equals("days")) {
        // change hours to minutes
        if (durationType.equals("hours")) {
            duration *= 60;
        }
        travelTime = travelTime.minusMinutes(duration);
        endTime = endTime.plusMinutes(duration);
    } else {
        travelTime = travelTime.minusDays(duration - 1);
        endTime = endTime.plusDays(duration + 1);
    }

    // loop each 5 minutes
    DateTimeFormatter fmtDate = DateTimeFormat.forPattern("MM/dd/YYYY");
    DateTimeFormatter fmtTime = DateTimeFormat.forPattern("HH:mm");
    String valueStr = "VALUES ";
    DateTime eachTime = new DateTime(travelTime);
    String date = fmtDate.print(eachTime);
    String time = fmtTime.print(eachTime);
    valueStr += " (@ResourceIDVar,'" + date + "','" + time + "',@NoUsedResourcesVar, @providerIDVar)";
    eachTime = eachTime.plusMinutes(smallestInterval);
    while (eachTime.isBefore(endTime)) {
        date = fmtDate.print(eachTime);
        time = fmtTime.print(eachTime);
        valueStr += ",(@ResourceIDVar,'" + date + "','" + time + "',@NoUsedResourcesVar, @providerIDVar)";
        eachTime = eachTime.plusMinutes(smallestInterval);
    }

    String insertStmt = "INSERT INTO UsedResources(ResourceID, TripDate, TripTime, NoUsedResources, ProviderID) ";
    insertStmt += valueStr;

    // Build Param Condition For Procedure
    String paramCondition = "@ResourceIDVar INT, @NoUsedResourcesVar INT, @providerIDVar INT";

    simpleJdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("InsertOfflineBooking");
    SqlParameterSource in = new MapSqlParameterSource().addValue("bookingDate", bookingDate)
            .addValue("providerID", providerID).addValue("tripDate", tripDate).addValue("tripTime", tripTime)
            .addValue("noPackage", noPackage).addValue("packageID", packageID)
            .addValue("resourceNote", resourceNote).addValue("customerName", customerName)
            .addValue("customerPhone", customerPhone).addValue("customerEmail", customerEmail)
            .addValue("InsertStatement", insertStmt).addValue("ParmDefinition", paramCondition);
    simpleJdbcCall.execute(in);
}

From source file:Implement.DAO.BookingDAOImpl.java

@Override
public void insertOfflineBooking(int providerID, int days, int hours, int minutes, long bookingTime,
        long tripTime, String dateStr, String timeStr, String customerName, String customerPhone, String email,
        List<OfflineResourceDTO> offlineResources, int smallestInterval, long resourceTime) {
    DateTimeFormatter fmtDate = DateTimeFormat.forPattern("MM/dd/YYYY");
    DateTimeFormatter fmtTime = DateTimeFormat.forPattern("HH:mm");

    //  construct dynamic sql for used resources
    DateTimeFormatter formatterDateAndHour = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
    String travelTimeStr = dateStr;
    if (days == 0) {
        travelTimeStr += " " + timeStr + ":00";
    } else {//  ww  w  .j  a va2 s.  c om
        travelTimeStr += " 00:00:00";
    }

    // insert offline resources first
    int i = 0;
    OfflineResourceDTO offlineResource = offlineResources.get(i);
    int providerResurceID = offlineResource.getProviderResourceID();
    int resourceHours = offlineResource.getHours();
    int resourceMinutes = offlineResource.getMinutes();
    int resourceDays = offlineResource.getDays();
    int noUnits = offlineResource.getNoUnits();
    String offlineResourceValue = "VALUES ";
    offlineResourceValue += "(@offlineBookingIDVar, " + providerResurceID + "," + noUnits + "," + resourceHours
            + "," + resourceMinutes + "," + resourceDays + ")";

    String usedResourceValue = "VALUES ";
    DateTime travelTime = formatterDateAndHour.parseDateTime(travelTimeStr);
    DateTime endTime = new DateTime(travelTime);
    if (resourceDays > 0) {
        endTime = endTime.plusDays(resourceDays);
    } else {
        // change hours to minutes
        int duration = resourceHours * 60 + resourceMinutes;
        endTime = endTime.plusMinutes(duration);
    }

    // loop each 5 minutes
    DateTime eachTime = new DateTime(travelTime);
    String date = fmtDate.print(eachTime);
    String time = fmtTime.print(eachTime);
    int usedResouceMinutes = eachTime.getMinuteOfDay();

    DateTime usedResourceOnlyDate = formatterDateAndHour.parseDateTime(date + " 00:00:00");
    long usedResourceMil = usedResourceOnlyDate.getMillis();
    usedResourceValue += " (" + providerResurceID + "," + usedResourceMil + "," + usedResouceMinutes + ","
            + noUnits + ",'" + date + "','" + time + "',@bookingCodeVar,@offlineBookingIDVar)";

    eachTime = eachTime.plusMinutes(smallestInterval);
    while (eachTime.isBefore(endTime)) {
        date = fmtDate.print(eachTime);
        time = fmtTime.print(eachTime);

        usedResourceOnlyDate = formatterDateAndHour.parseDateTime(date + " 00:00:00");
        usedResourceMil = usedResourceOnlyDate.getMillis();
        usedResouceMinutes = eachTime.getMinuteOfDay();
        usedResourceValue += ",(" + providerResurceID + "," + usedResourceMil + "," + usedResouceMinutes + ","
                + noUnits + ",'" + date + "','" + time + "',@bookingCodeVar,@offlineBookingIDVar)";
        eachTime = eachTime.plusMinutes(smallestInterval);
    }

    i++;
    int resourceLength = offlineResources.size();
    for (; i < resourceLength; i++) {
        offlineResource = offlineResources.get(i);
        providerResurceID = offlineResource.getProviderResourceID();
        resourceHours = offlineResource.getHours();
        resourceMinutes = offlineResource.getMinutes();
        resourceDays = offlineResource.getDays();
        noUnits = offlineResource.getNoUnits();
        offlineResourceValue += ",(@offlineBookingIDVar, " + providerResurceID + "," + noUnits + ","
                + resourceHours + "," + resourceMinutes + "," + resourceDays + ")";

        travelTime = formatterDateAndHour.parseDateTime(travelTimeStr);
        endTime = new DateTime(travelTime);
        if (resourceDays > 0) {
            endTime = endTime.plusDays(resourceDays);
        } else {

            // change hours to minutes
            int duration = resourceHours * 60 + resourceMinutes;
            endTime = endTime.plusMinutes(duration);
        }

        // loop each 5 minutes
        eachTime = new DateTime(travelTime);
        date = fmtDate.print(eachTime);
        time = fmtTime.print(eachTime);

        usedResouceMinutes = eachTime.getMinuteOfDay();

        usedResourceOnlyDate = formatterDateAndHour.parseDateTime(date + " 00:00:00");
        usedResourceMil = usedResourceOnlyDate.getMillis();
        usedResourceValue += ",(" + providerResurceID + "," + usedResourceMil + "," + usedResouceMinutes + ","
                + noUnits + ",'" + date + "','" + time + "',@bookingCodeVar,@offlineBookingIDVar)";

        eachTime = eachTime.plusMinutes(smallestInterval);
        while (eachTime.isBefore(endTime)) {
            date = fmtDate.print(eachTime);
            time = fmtTime.print(eachTime);

            usedResourceOnlyDate = formatterDateAndHour.parseDateTime(date + " 00:00:00");
            usedResourceMil = usedResourceOnlyDate.getMillis();
            usedResouceMinutes = eachTime.getMinuteOfDay();
            usedResourceValue += ",(" + providerResurceID + "," + usedResourceMil + "," + usedResouceMinutes
                    + "," + noUnits + ",'" + date + "','" + time + "',@bookingCodeVar,@offlineBookingIDVar)";
            eachTime = eachTime.plusMinutes(smallestInterval);
        }
    }

    String resourceInsertingStmt = "INSERT INTO OfflineResource(OfflineBookingID, ProviderResourceID, NoUnits,"
            + "Hours, Minutes,Days) ";
    resourceInsertingStmt += offlineResourceValue;
    String resourceInsertingParmDefinition = "@offlineBookingIDVar INT";

    String usedResourceInsertingStmt = "INSERT INTO UsedProviderResource(ProviderResourceID, Date, Time, NoUsedResources,"
            + "DateStr,TimeStr,BookingCode,OfflineBookingID)";
    usedResourceInsertingStmt += usedResourceValue;
    String usedResourceInsertingParmDefinition = "@bookingCodeVar NVARCHAR(10)," + "@offlineBookingIDVar INT";

    simpleJdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("InsertNewOfflineBooking");
    SqlParameterSource in = new MapSqlParameterSource().addValue("days", days).addValue("minutes", minutes)
            .addValue("hours", hours).addValue("bookingTime", bookingTime).addValue("tripTime", tripTime)
            .addValue("dateStr", dateStr).addValue("timeStr", timeStr).addValue("customerName", customerName)
            .addValue("customerPhone", customerPhone).addValue("customerPhone", customerPhone)
            .addValue("email", email).addValue("providerID", providerID)
            .addValue("ResourceInsertingStmt", resourceInsertingStmt)
            .addValue("ResourceInsertingParmDefinition", resourceInsertingParmDefinition)
            .addValue("UsedResourceInsertingStmt", usedResourceInsertingStmt)
            .addValue("UsedResourceInsertingParmDefinition", usedResourceInsertingParmDefinition);
    simpleJdbcCall.execute(in);
}

From source file:influent.server.dataaccess.DataAccessHelper.java

License:MIT License

public static DateTime getExclusiveEndDate(FL_DateRange date) {

    if (date == null) {
        return null;
    }/*from  w  ww .  j  a v  a  2s . c om*/

    DateTime d = new DateTime((long) date.getStartDate(), DateTimeZone.UTC);

    switch (date.getDurationPerBin().getInterval()) {
    case SECONDS:
        return d.plusSeconds(date.getNumBins().intValue());
    case HOURS:
        return d.plusHours(date.getNumBins().intValue());
    case DAYS:
        return d.plusDays(date.getNumBins().intValue());
    case WEEKS:
        return d.plusWeeks(date.getNumBins().intValue());
    case MONTHS:
        return d.plusMonths(date.getNumBins().intValue());
    case QUARTERS:
        return d.plusMonths(date.getNumBins().intValue() * 3);
    case YEARS:
        return d.plusYears(date.getNumBins().intValue());
    }

    return d;
}

From source file:io.apiman.manager.api.rest.impl.OrganizationResourceImpl.java

License:Apache License

/**
 * Parses a query param representing a date into an actual date object.
 * @param dateStr/*from   ww  w .  j  a  va2s  .c o m*/
 * @param defaultDate
 * @param floor
 */
private static DateTime parseDate(String dateStr, DateTime defaultDate, boolean floor) {
    if ("now".equals(dateStr)) { //$NON-NLS-1$
        return DateTime.now();
    }
    if (dateStr.length() == 10) {
        DateTime parsed = ISODateTimeFormat.date().withZoneUTC().parseDateTime(dateStr);
        // If what we want is the floor, then just return it.  But if we want the
        // ceiling of the date, then we need to set the right params.
        if (!floor) {
            parsed = parsed.plusDays(1).minusMillis(1);
        }
        return parsed;
    }
    if (dateStr.length() == 20) {
        return ISODateTimeFormat.dateTimeNoMillis().withZoneUTC().parseDateTime(dateStr);
    }
    if (dateStr.length() == 24) {
        return ISODateTimeFormat.dateTime().withZoneUTC().parseDateTime(dateStr);
    }
    return defaultDate;
}