Example usage for org.apache.commons.lang.time DateUtils truncate

List of usage examples for org.apache.commons.lang.time DateUtils truncate

Introduction

In this page you can find the example usage for org.apache.commons.lang.time DateUtils truncate.

Prototype

public static Date truncate(Object date, int field) 

Source Link

Document

Truncate this date, leaving the field specified as the most significant field.

For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:00:00.000.

Usage

From source file:org.pau.assetmanager.viewmodel.AnnotationViewModel.java

protected void addDefaultValuesToAnnotation(Annotation annotation, Book selectedBook, Integer currentYear,
        AnnotationsFilter annotationsFilter) {
    annotation.setBook(selectedBook);/*  w  w w .j a  v a2s  .  c  om*/
    annotation.setAmount(0.0);
    Calendar calendar = GregorianCalendar.getInstance();
    Integer systemDateYear = calendar.get(Calendar.YEAR);
    if (!systemDateYear.equals(currentYear)) {
        String dateAsString = "01/01/" + currentYear;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        try {
            Date dateForNewAnnotation = simpleDateFormat.parse(dateAsString);
            annotation.setDate(dateForNewAnnotation);
        } catch (ParseException e) {
            logger.error("Error parsing date", e);
        }
    } else {
        Calendar currentCalendar = GregorianCalendar.getInstance();
        currentCalendar = DateUtils.truncate(currentCalendar, Calendar.DATE);
        annotation.setDate(currentCalendar.getTime());
    }
    if (annotationsFilter.getConcept() != null
            && !annotationsFilter.getConcept().equals(AnnotationsFilter.ALL_CONCEPTS)) {
        annotation.setConcept(annotationsFilter.getConcept());
    } else {
        annotation.setConcept(getDefaultConcept(annotation));
    }
}

From source file:org.sakaiproject.chat2.model.impl.ChatManagerImpl.java

/**
 * {@inheritDoc}/*from   w  w w.j  a va2  s.co m*/
 */
public boolean getCanPostMessage(ChatChannel channel) {
    // We don't currently support posting messages by anonymous users
    if (SessionManager.getCurrentSessionUserId() == null)
        return false;

    boolean allowed = false;
    if (channel != null) {
        allowed = can(ChatFunctions.CHAT_FUNCTION_NEW, channel.getContext());
        if (allowed) {
            // check the dates if they are set (https://jira.sakaiproject.org/browse/SAK-24207)
            Date today = new Date();
            Date start = channel.getStartDate();
            if (start == null) {
                start = today;
            } else {
                // fix up the date to shift to be beginning or end of the day (drop any time component)
                start = DateUtils.truncate(start, Calendar.DATE);
            }
            Date end = channel.getEndDate();
            if (end == null) {
                end = today;
            } else {
                // fix up the date to shift to be beginning or end of the day (drop any time component)
                end = DateUtils.truncate(end, Calendar.DATE);
                end = DateUtils.addSeconds(end, 86398); // just short of a full day in seconds
            }
            if (today.before(start) || today.after(end)) {
                // today is outside the configured dates so no posting allowed
                allowed = false;
            }
        }
    }
    return allowed;
}

From source file:org.sakaiproject.chat2.tool.DecoratedChatChannel.java

public void setStartDate(Date startDate) {
    if (startDate != null) {
        // fix up the date to shift to be beginning or end of the day (drop any time component)
        startDate = DateUtils.truncate(startDate, Calendar.DATE);
    }/*  w w  w.  j a  v  a2 s  .  co m*/
    this.startDate = startDate;
}

From source file:org.sakaiproject.chat2.tool.DecoratedChatChannel.java

public Date getEndDate() {
    if (endDate != null) {
        // fix up the date to drop any time component
        endDate = DateUtils.truncate(endDate, Calendar.DATE);
    }/*from   w  ww .  jav a2  s .  co  m*/
    return endDate;
}

From source file:org.sakaiproject.chat2.tool.DecoratedChatChannel.java

public void setEndDate(Date endDate) {
    if (endDate != null) {
        // fix up the date to shift to be beginning or end of the day (drop any time component)
        endDate = DateUtils.truncate(endDate, Calendar.DATE);
        endDate = DateUtils.addSeconds(endDate, 86398); // just short of a full day in seconds
    }/*w  w w.j  ava  2  s .c  o  m*/
    this.endDate = endDate;
}

From source file:org.simbasecurity.core.domain.UserEntity.java

private void setInactiveDate(Status status) {
    if (this.status != status) {
        this.inactiveDate = INACTIVE.equals(status) ? DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH)
                : null;//from  ww w.j  a v a 2s .c o  m
    }
}

From source file:org.simbasecurity.core.domain.UserEntity.java

private void setPassword(String newPassword) {
    getPasswordValidator().validatePassword(newPassword);

    ConfigurablePasswordEncryptor encryptor = retrievePasswordEncryptor();
    try {//from ww  w  .  j ava 2s  .c  o m
        this.password = encryptor.encryptPassword(newPassword);
    } finally {
        returnPasswordEncryptor(encryptor);
    }

    this.dateOfLastPasswordChange = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
}

From source file:org.simbasecurity.core.domain.UserEntityTest.java

@Test
public void setStatus() {
    UserEntity entity = new UserEntity(USERNAME);
    assertEquals(Status.ACTIVE, entity.getStatus());
    assertNull(entity.getInactiveDate());

    entity.setStatus(Status.INACTIVE);
    assertEquals(Status.INACTIVE, entity.getStatus());
    Date inactiveDate = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
    assertEquals(inactiveDate, entity.getInactiveDate());

    entity.setStatus(Status.INACTIVE);
    assertEquals(Status.INACTIVE, entity.getStatus());
    assertEquals(inactiveDate, entity.getInactiveDate());
}

From source file:org.simbasecurity.core.domain.UserEntityTest.java

@Test
public void changePassword_changeSuccess() {
    UserEntity entity = new UserEntity(USERNAME, null, null, null, Language.nl_NL, Status.ACTIVE, true, true);
    entity.changePassword(VALID_PASSWORD, VALID_PASSWORD);
    assertFalse(entity.isChangePasswordOnNextLogon());
    assertEquals(DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH), entity.getDateOfLastPasswordChange());
}

From source file:org.simbasecurity.core.service.CredentialServiceImpl.java

@Override
public void markUsersForPasswordChange() {
    Collection<User> allUsers = userRepository.findAll();

    int passwordLifeTime = configurationService.getValue(SimbaConfigurationParameter.PASSWORD_LIFE_TIME);

    Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);

    for (User user : allUsers) {
        Date changeDate = user.getDateOfLastPasswordChange();
        if (changeDate == null) {
            changeDate = new Date(0);
        }//w w w .  j  a  va 2  s  .c o  m
        Date lastDateForNextChange = DateUtils.addDays(changeDate, passwordLifeTime);

        if (user.isPasswordChangeRequired() && today.after(lastDateForNextChange)) {
            user.setChangePasswordOnNextLogon(true);
        }
    }
}