Example usage for org.joda.time LocalDateTime toDateTime

List of usage examples for org.joda.time LocalDateTime toDateTime

Introduction

In this page you can find the example usage for org.joda.time LocalDateTime toDateTime.

Prototype

public DateTime toDateTime() 

Source Link

Document

Converts this object to a DateTime using the default zone.

Usage

From source file:org.kzac.common.dto.TimeOfDayInfo.java

License:Educational Community License

/**
 * //  w w w  . ja va  2s  .  com
 * @param date
 *            a java.util.Date to which timeOfDay is added (the hours,
 *            minutes, seconds and milliseconds are ignored and treated as
 *            0. Only the month, day, year of date is relevant)
 * @param timeOfDay
 *            the TimeOfDay that is added to the date parameter
 * @return a java.util.Date that is the sum of date and timeOfDay
 */
public static Date getDateWithTimeOfDay(Date date, TimeOfDay timeOfDay) {
    if (date == null || timeOfDay == null) {
        return null;
    }
    // This is JODA which is the preferred way to deal with time.
    // One issue: Java's Date class doesn't have a way to store the timezone
    // associated with the date.
    // Instead, it saves millis since Jan 1, 1970 (GMT), and uses the local
    // machine's timezone to
    // interpret it (although, it can also handle UTC as a timezone). To
    // store the timezone, you either
    // need Joda's LocalDateTime or use the Calendar class. Thus, it is
    // recommended that you either
    // pass in the date relative to the current timezone, or perhaps redo
    // this class where you pass
    // a timezone in, and don't use a Date class, but use a Joda DateTime
    // class. --cclin
    LocalDateTime time = new LocalDateTime(date);
    LocalDateTime result = new LocalDateTime(time.getYear(), time.getMonthOfYear(), time.getDayOfMonth(),
            timeOfDay.getHour() == null ? 0 : timeOfDay.getHour(),
            timeOfDay.getMinute() == null ? 0 : timeOfDay.getMinute(),
            timeOfDay.getSecond() == null ? 0 : timeOfDay.getSecond(), 0);
    return result.toDateTime().toDate();
}

From source file:org.smartdeveloperhub.harvesters.it.testing.generator.ProjectActivityGenerator.java

License:Apache License

private void createIssue(final String issueId, final LocalDateTime creationDate) {
    final Issue issue = new Issue();
    issue.setProjectId(this.project.getId());
    issue.setId(issueId);/*from   w  w  w  . j a  v a  2s.  c om*/
    issue.setName(StateUtil.generateSentence());
    issue.setDescription(StateUtil.generateSentences(1, 2 + this.random.nextInt(8)));
    issue.setStatus(Status.OPEN);
    issue.setCreationDate(creationDate.toDateTime());
    issue.setOpened(issue.getCreationDate());
    Contributor assignee = null;
    final Contributor reporter = selectContributor();
    issue.setReporter(reporter.getId());

    LOGGER.trace("   * Created issue {} at {}, reported by {}", issue.getId(), creationDate,
            reporter.getName());

    issue.setSeverity(selectSeverity());
    LOGGER.trace("     + Severity: {}", issue.getSeverity());

    issue.setPriority(selectPriority());
    LOGGER.trace("     + Priority: {}", issue.getPriority());

    if (this.random.nextBoolean()) {
        issue.setType(selectType());
        LOGGER.trace("     + Type: {}", issue.getType());
    }

    if (this.random.nextBoolean()) {
        assignee = selectContributor();
        issue.setAssignee(assignee.getId());
        LOGGER.trace("     + Assigned to {}", assignee.getName());
    }

    if (this.random.nextBoolean()) {
        final LocalDateTime dueTo = createDueTo(creationDate);
        issue.setDueTo(dueTo.toDateTime());
        LOGGER.trace("     + Scheduled for {}", dueTo);
        if (this.random.nextBoolean()) {
            issue.setEstimatedTime(estimateEffort(creationDate, dueTo));
            LOGGER.trace("     + Estimated {} hours ", issue.getEstimatedTime().getStandardHours());
        }
    }

    if (!this.components.isEmpty() && this.random.nextBoolean()) {
        final int affectedComponents = 1 + this.random.nextInt(2);
        final List<String> names = Lists.newArrayList();
        for (int i = 0; i < affectedComponents; i++) {
            final Component component = selectComponent();
            if (issue.getComponents().add(component.getId())) {
                names.add(component.getName());
            }
        }
        LOGGER.trace("     + Related to components: {}", Joiner.on(", ").join(names));
    }

    if (!this.versions.isEmpty() && this.random.nextBoolean()) {
        final int affectedVersions = 1 + this.random.nextInt(2);
        final List<String> names = Lists.newArrayList();
        for (int i = 0; i < affectedVersions; i++) {
            final Version version = selectVersion();
            if (issue.getVersions().add(version.getId())) {
                names.add(version.getName());
            }
        }
        LOGGER.trace("     + Affects versions: {}", Joiner.on(", ").join(names));
    }

    this.issues.put(issueId, issue);
    this.project.getIssues().add(issueId);
    this.project.getTopIssues().add(issueId);
}

From source file:org.smartdeveloperhub.harvesters.it.testing.generator.ProjectActivityGenerator.java

License:Apache License

private void evaluate(final Issue issue, final LocalDate today) {
    final Set<Item> changes = Sets.newLinkedHashSet();
    final LocalDateTime now = today.toLocalDateTime(this.workDay.workingTime());
    LOGGER.trace("   * Evaluated issue {} at {}", issue.getId(), now);

    if (issue.getAssignee() == null) {
        final Contributor assignee = selectContributor();
        issue.setAssignee(assignee.getId());

        final AssigneeChangeItem item = new AssigneeChangeItem();
        item.setOldValue(null);/* w w  w  .j a va 2  s .c  om*/
        item.setNewValue(issue.getAssignee());
        changes.add(item);

        LOGGER.trace("     + Assigned to {}", assignee.getName());
    }

    if (issue.getType() == null) {
        issue.setType(selectType());

        final TypeChangeItem item = new TypeChangeItem();
        item.setOldValue(null);
        item.setNewValue(issue.getType());
        changes.add(item);

        LOGGER.trace("     + Type: {}", issue.getType());
    }

    if (this.random.nextInt(100) < 10) {
        issue.setStatus(Status.CLOSED);
        issue.setClosed(now.toDateTime());

        final ClosedChangeItem item = new ClosedChangeItem();
        item.setOldValue(null);
        item.setNewValue(issue.getClosed());
        changes.add(item);

        LOGGER.trace("     + Action: close");
    } else {
        issue.setStatus(Status.IN_PROGRESS);
        LOGGER.trace("     + Action: start work");
    }

    final StatusChangeItem item = new StatusChangeItem();
    item.setOldValue(Status.OPEN);
    item.setNewValue(issue.getStatus());
    changes.add(item);

    final Entry entry = new Entry();
    entry.setTimeStamp(now.toDateTime());
    entry.setAuthor(issue.getAssignee());
    entry.getItems().addAll(changes);

    final ChangeLog changeLog = new ChangeLog();
    changeLog.getEntries().add(entry);

    issue.setChanges(changeLog);
}

From source file:org.smartdeveloperhub.harvesters.it.testing.generator.ProjectActivityGenerator.java

License:Apache License

private void workOn(final Issue issue, final LocalDate today) {
    final Set<Item> changes = Sets.newLinkedHashSet();
    final LocalDateTime now = today.toLocalDateTime(this.workDay.workingTime());
    LOGGER.trace("   * Worked on issue {} at {}", issue.getId(), now);

    // Bounce assignment
    if (this.random.nextBoolean()) {
        final Contributor oldValue = findContributor(issue.getAssignee());
        final Contributor newValue = selectAlternativeContributor(issue.getAssignee());
        if (newValue != null) {
            issue.setAssignee(newValue.getId());

            final AssigneeChangeItem item = new AssigneeChangeItem();
            item.setOldValue(oldValue.getId());
            item.setNewValue(issue.getAssignee());
            changes.add(item);//  ww w. ja v  a 2s.  c o  m

            LOGGER.trace("     + Changed assignment from {} to {}", oldValue.getName(), newValue.getName());
        }
    }

    // Change type
    if (this.random.nextBoolean()) {
        final Type oldValue = issue.getType();
        final Type newValue = selectAlternativeType(oldValue);

        issue.setType(newValue);

        final TypeChangeItem item = new TypeChangeItem();
        item.setOldValue(oldValue);
        item.setNewValue(newValue);
        changes.add(item);

        LOGGER.trace("     + Changed type from {} to {}", oldValue, newValue);
    }

    // Change title
    if (this.random.nextBoolean()) {
        final String oldValue = issue.getName();
        final String newValue = StateUtil.generateSentence();

        issue.setName(newValue);

        final TitleChangeItem item = new TitleChangeItem();
        item.setOldValue(oldValue);
        item.setNewValue(newValue);
        changes.add(item);

        LOGGER.trace("     + Changed title from '{}' to '{}'", oldValue, newValue);
    }

    // Change description
    if (this.random.nextBoolean()) {
        final String oldValue = issue.getName();
        final String newValue = StateUtil.generateSentences(1, 2 + this.random.nextInt(8));

        issue.setDescription(newValue);

        final DescriptionChangeItem item = new DescriptionChangeItem();
        item.setOldValue(oldValue);
        item.setNewValue(newValue);
        changes.add(item);

        LOGGER.trace("     + Changed description from '{}' to '{}'", oldValue, newValue);
    }

    // Change severity
    if (this.random.nextBoolean()) {
        final Severity oldValue = issue.getSeverity();
        final Severity newValue = selectAlternativeSeverity(oldValue);

        issue.setSeverity(newValue);

        final SeverityChangeItem item = new SeverityChangeItem();
        item.setOldValue(oldValue);
        item.setNewValue(newValue);
        changes.add(item);

        LOGGER.trace("     + Changed severity from {} to {}", oldValue, newValue);
    }

    // Change priority
    if (this.random.nextBoolean()) {
        final Priority oldValue = issue.getPriority();
        final Priority newValue = selectAlternativePriority(oldValue);

        issue.setPriority(newValue);

        final PriorityChangeItem item = new PriorityChangeItem();
        item.setOldValue(oldValue);
        item.setNewValue(newValue);
        changes.add(item);

        LOGGER.trace("     + Changed priority from {} to {}", oldValue, newValue);
    }

    // Change due to
    boolean reescheduled = false;
    boolean isCloseable = true;
    if (this.random.nextBoolean()) {
        isCloseable = false;
        reescheduled = scheduleIssue(issue, changes, now);
    }

    // Change effort, if required or decided to.
    if (reescheduled || issue.getDueTo() != null && this.random.nextBoolean()) {
        isCloseable = false;
        estimateIssue(issue, changes, now);
    }

    final ChangeDecissionPoint cdp = new ChangeDecissionPoint(this.random);

    // Add/remove components
    final ChangeInformationPoint ccip = new ComponentChangeInformationPoint(issue);
    if (ccip.canModify() && this.random.nextBoolean()) {
        final ComponentManager pep = new ComponentManager(issue, changes);
        final int affectedComponents = 1 + this.random.nextInt(Math.max(2, issue.getComponents().size() - 1));
        for (int i = 0; i < affectedComponents; i++) {
            cdp.decide(ccip).apply(pep);
        }
        pep.logActivity();
    }

    // Add/remove versions
    final ChangeInformationPoint vcip = new VersionsChangeInformationPoint(issue);
    if (vcip.canModify() && this.random.nextBoolean()) {
        final VersionManager pep = new VersionManager(issue, changes);
        final int affectedVersions = 1 + this.random.nextInt(Math.max(2, issue.getVersions().size() - 1));
        for (int i = 0; i < affectedVersions; i++) {
            cdp.decide(vcip).apply(pep);
        }
        pep.logActivity();
    }

    /**
     * TODO: Add logic for adding commits
     */
    /**
     * TODO: Add logic for adding sub-issues
     */
    /**
     * TODO: Add logic for adding blocked-issues
     */

    // Add/remove tags
    final ChangeInformationPoint tcip = new TagsChangeInformationPoint(issue);
    if (tcip.canModify() && this.random.nextBoolean()) {
        final TagManager pep = new TagManager(issue, changes);
        final int affectedTags = 1 + this.random.nextInt(Math.max(2, issue.getTags().size() - 1));
        for (int i = 0; i < affectedTags; i++) {
            cdp.decide(tcip).apply(pep);
        }
        pep.logActivity();
    }

    if (isCloseable && mustCloseIssue(issue, now)) {
        issue.setStatus(Status.CLOSED);
        issue.setClosed(now.toDateTime());

        final StatusChangeItem sChange = new StatusChangeItem();
        sChange.setOldValue(Status.IN_PROGRESS);
        sChange.setNewValue(Status.CLOSED);
        changes.add(sChange);

        final ClosedChangeItem cdChange = new ClosedChangeItem();
        cdChange.setOldValue(null);
        cdChange.setNewValue(issue.getClosed());
        changes.add(cdChange);

        LOGGER.trace("     + Action: close");
    }

    final Entry entry = new Entry();
    entry.setTimeStamp(now.toDateTime());
    entry.setAuthor(issue.getAssignee());
    entry.getItems().addAll(changes);

    final ChangeLog changeLog = issue.getChanges();
    changeLog.getEntries().add(entry);
}

From source file:org.smartdeveloperhub.harvesters.it.testing.generator.ProjectActivityGenerator.java

License:Apache License

private boolean scheduleIssue(final Issue issue, final Set<Item> changes, final LocalDateTime now) {
    final LocalDateTime oldValue = Utils.toLocalDateTime(issue.getDueTo());
    final LocalDateTime newValue = createDueTo(now);

    issue.setDueTo(newValue.toDateTime());

    final DueToDateChangeItem item = new DueToDateChangeItem();
    item.setOldValue(Utils.toDateTime(oldValue));
    item.setNewValue(newValue.toDateTime());
    changes.add(item);/* w  w  w.  j a  v a2s .  c o m*/

    boolean result = false;
    if (oldValue == null) {
        LOGGER.trace("     + Scheduled for {}", newValue);
    } else {
        result = true;
        LOGGER.trace("     + Reescheduled from {} to {}", oldValue, newValue);
    }
    return result;
}

From source file:org.smartdeveloperhub.harvesters.it.testing.generator.ProjectActivityGenerator.java

License:Apache License

private void reopen(final Issue issue, final LocalDate today) {
    final Set<Item> changes = Sets.newLinkedHashSet();
    final LocalDateTime now = today.toLocalDateTime(this.workDay.workingTime());
    LOGGER.trace("   * Reopened issue {} at {}", issue.getId(), now);

    // Bounce assignment
    if (this.random.nextBoolean()) {
        final Contributor oldValue = findContributor(issue.getAssignee());
        final Contributor newValue = selectContributor();

        issue.setAssignee(newValue.getId());

        final AssigneeChangeItem item = new AssigneeChangeItem();
        item.setOldValue(oldValue.getId());
        item.setNewValue(issue.getAssignee());
        changes.add(item);//  w  w w  . j a  v  a  2s  .c o m

        LOGGER.trace("     + Changed assignment from {} to {}", oldValue.getName(), newValue.getName());
    }

    {
        issue.setStatus(Status.OPEN);

        final StatusChangeItem item = new StatusChangeItem();
        item.setOldValue(Status.CLOSED);
        item.setNewValue(Status.OPEN);
        changes.add(item);
    }
    {
        final DateTime oldValue = issue.getOpened();
        issue.setOpened(now.toDateTime());

        final OpenedChangeItem item = new OpenedChangeItem();
        item.setOldValue(oldValue);
        item.setNewValue(issue.getOpened());
        changes.add(item);
    }
    {
        final DateTime oldValue = issue.getClosed();
        issue.setClosed(null);

        final ClosedChangeItem item = new ClosedChangeItem();
        item.setOldValue(oldValue);
        item.setNewValue(issue.getClosed());
        changes.add(item);
    }
    {
        boolean scheduled = false;
        if (this.random.nextBoolean()) {
            scheduled = true;
            scheduleIssue(issue, changes, now);
        } else {
            final DateTime oldValue = issue.getDueTo();
            if (oldValue != null) {
                final DueToDateChangeItem item = new DueToDateChangeItem();
                item.setOldValue(oldValue);
                item.setNewValue(null);
                changes.add(item);
            }
        }

        if (scheduled && this.random.nextBoolean()) {
            estimateIssue(issue, changes, now);
        } else {
            final Duration oldValue = issue.getEstimatedTime();
            if (oldValue != null) {
                final EstimatedTimeChangeItem item = new EstimatedTimeChangeItem();
                item.setOldValue(oldValue);
                item.setNewValue(null);
                changes.add(item);
            }
        }
    }

    final Entry entry = new Entry();
    entry.setTimeStamp(now.toDateTime());
    entry.setAuthor(issue.getAssignee());
    entry.getItems().addAll(changes);

    final ChangeLog changeLog = issue.getChanges();
    changeLog.getEntries().add(entry);
}

From source file:org.smartdeveloperhub.harvesters.it.testing.generator.Utils.java

License:Apache License

static DateTime toDateTime(LocalDateTime localDateTime) {
    DateTime result = null;/*  www  .ja  va2 s.  com*/
    if (localDateTime != null) {
        result = localDateTime.toDateTime();
    }
    return result;
}

From source file:uk.co.onehp.trickle.util.DateUtil.java

License:Open Source License

public static String toShortString(LocalDateTime localDateTime) {
    return String.format("%s %s %s:%s", localDateTime.toDateTime().monthOfYear().getAsShortText(),
            localDateTime.toDateTime().dayOfMonth().getAsText(), localDateTime.getHourOfDay(),
            localDateTime.getMinuteOfHour());
}