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

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

Introduction

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

Prototype

public static boolean isSameDay(Calendar cal1, Calendar cal2) 

Source Link

Document

Checks if two calendar objects are on the same day ignoring time.

28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true.

Usage

From source file:com.cubusmail.server.services.ConvertUtil.java

/**
 * Convert dedicated messages to string arrays for GridList.
 * //from   ww  w.  j  a v a  2 s. c  o  m
 * @param context
 * @param preferences
 * @param currentFolder
 * @param pageSize
 * @param msgs
 * @return
 * @throws MessagingException
 */
public static GWTMessageRecord[] convertMessagesToStringArray(ApplicationContext context,
        Preferences preferences, IMAPFolder currentFolder, int pageSize, Message msgs[])
        throws MessagingException {

    GWTMessageRecord[] result = new GWTMessageRecord[pageSize];

    // get date formats for message list date
    Locale locale = SessionManager.get().getLocale();
    TimeZone timezone = SessionManager.get().getTimeZone();
    String datePattern = context.getMessage(CubusConstants.MESSAGELIST_DATE_FORMAT_PATTERN, null, locale);
    String timePattern = context.getMessage(CubusConstants.MESSAGELIST_TIME_FORMAT_PATTERN, null, locale);

    NumberFormat sizeFormat = MessageUtils.createSizeFormat(locale);

    DateFormat dateFormat = null;
    DateFormat timeFormat = null;
    if (preferences.isShortTimeFormat()) {
        dateFormat = new SimpleDateFormat(datePattern, locale);
        timeFormat = new SimpleDateFormat(timePattern, locale);
        timeFormat.setTimeZone(timezone);
    } else {
        dateFormat = new SimpleDateFormat(datePattern + " " + timePattern, locale);
    }
    dateFormat.setTimeZone(timezone);
    Date today = Calendar.getInstance(timezone).getTime();

    for (int i = 0; i < pageSize; i++) {
        result[i] = new GWTMessageRecord();
        if (preferences.isShortTimeFormat() && DateUtils.isSameDay(today, msgs[i].getSentDate())) {
            // show only time
            convertToStringArray(currentFolder, msgs[i], result[i], timeFormat, sizeFormat);
        } else {
            convertToStringArray(currentFolder, msgs[i], result[i], dateFormat, sizeFormat);
        }
    }

    return result;
}

From source file:de.mmichaelis.maven.mojo.MailDevelopersMojoTest.java

@Test
public void testFullyConfiguredMail() throws Exception {
    final MavenProject project = mock(MavenProject.class);
    when(project.getDevelopers()).thenReturn(Arrays.asList(developers));
    mojoWrapper.setProject(project);// w  w w .ja  v a 2 s. c  om
    mojoWrapper.setCharset("UTF-8");
    mojoWrapper.setExpires("2");
    final String from = "John Doe <johndoe@github.com>";
    mojoWrapper.setFrom(from);
    mojoWrapper.setPriority("high");
    final String subject = "testFullyConfiguredMail";
    mojoWrapper.setSubject(subject);
    final String topic = "MyTopic";
    mojoWrapper.setTopic(topic);
    final Date now = new Date();
    mojoWrapper.execute();
    final Mailbox inbox = Mailbox.get(developers[0].getEmail());
    assertEquals("One new email for the first developer.", 1, inbox.size());
    final Message message = inbox.get(0);
    assertTrue("Sent date should signal to be today.", DateUtils.isSameDay(now, message.getSentDate()));
    assertEquals("Size of recipients should match number of developers.", developers.length,
            message.getAllRecipients().length);
    final Address[] senders = message.getFrom();
    assertNotNull("Sender address should be set.", senders);
    assertEquals("Number of senders should be 1.", 1, senders.length);
    assertEquals("Sender in message should match original sender.", from, senders[0].toString());
    final String messageSubject = message.getSubject();
    assertTrue("Subject should contain original subject.", messageSubject.contains(subject));
    assertTrue("Subject should contain topic.", messageSubject.contains(topic));

    // TODO: Check additional headers
}

From source file:net.audumla.scheduler.quartz.SunScheduleTest.java

@Test
public void testStartAndEndEvent() throws Exception {
    AstronomicEvent startEvent = getSunRiseEvent();
    AstronomicEvent endEvent = getSunRiseEvent();
    Date now = new Date();
    if (startEvent.getCalculatedEventTime().before(now)) {
        now = DateUtils.addDays(now, 1);
    }//from   w  w  w .j a v  a 2  s . com
    AstronomicTriggerImpl trigger = (AstronomicTriggerImpl) AstronomicScheduleBuilder.astronomicalSchedule()
            .startEvent(startEvent).startEventOffset(-3).endEvent(endEvent).forEveryEvent()
            .withIntervalInSeconds(1).repeatForever().build();
    trigger.computeFirstFireTime(null);
    Date fire = trigger.getNextFireTime();
    assert DateUtils.isSameDay(fire, now);
    Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime());
    trigger.triggered(null);
    Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime() + 1000);
    trigger.triggered(null);
    Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime() + 2000);
    trigger.triggered(null);
    if (DateUtils.isSameDay(trigger.getNextFireTime(), fire)) {
        Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime() + 3000);
        trigger.triggered(null);
    }
    fire = trigger.getNextFireTime();
    assert DateUtils.isSameDay(fire, DateUtils.addDays(now, 1));
    Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime(), 10);
    trigger.triggered(null);
    Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime() + 1000, 10);
}

From source file:TripServlet.java

void filterTrips(Date lastdate) {
    filterdtrips.clear();//  w  w  w . j  a va2 s .  c o  m
    Date today = new Date();
    for (int i = 0; i < alltrips.size(); i++) {
        Date d = alltrips.get(i).start_Date;
        if (d.after(lastdate) || DateUtils.isSameDay(d, today)) {
            filterdtrips.add(alltrips.get(i));
        }
    }
}

From source file:fragment.web.SystemHealthControllerTest.java

@SuppressWarnings("unchecked")
@Test// ww  w.  j  a  v a 2 s. com
public void testHealth() throws Exception {
    calendar.add(Calendar.HOUR_OF_DAY, -1);
    Date start = calendar.getTime();
    calendar.add(Calendar.HOUR_OF_DAY, 5);
    Date end = calendar.getTime();
    ServiceNotification notification = new ServiceNotification(getRootUser(), start, end, "planned",
            "planned down time", defaultServiceInstance);
    serviceNotificationDAO.save(notification);

    for (int i = 0; i < 3; i++) {
        calendar.add(Calendar.DAY_OF_MONTH, random.nextInt(5) + 2);
        start = calendar.getTime();
        calendar.add(Calendar.HOUR_OF_DAY, random.nextInt(5) + 1);
        end = calendar.getTime();
        notification = new ServiceNotification(getRootUser(), start, end, "planned", "planned down time",
                defaultServiceInstance);
        serviceNotificationDAO.save(notification);
    }
    serviceNotificationDAO.flush();

    ServiceInstance instance = serviceInstanceDao.find(1L);
    String view = controller.health(instance.getUuid(), 0, map);
    Assert.assertEquals("system.health", view);

    Object o = map.get("dateStatus");
    Assert.assertTrue(o instanceof Map);
    Map<Date, Health> dates = (Map<Date, Health>) o;
    Assert.assertEquals(AbstractBaseController.getDefaultPageSize().intValue(), dates.keySet().size());
    for (Date date : dates.keySet()) {
        System.out.println(date);
        Assert.assertTrue(DateUtils.isSameDay(new Date(), date) || date.before(new Date()));
    }

    o = map.get("dateStatusHistory");
    Assert.assertTrue(o instanceof Map);
    Map<Date, List<ServiceNotification>> datesStatusHistory = (Map<Date, List<ServiceNotification>>) o;
    Assert.assertEquals(AbstractBaseController.getDefaultPageSize().intValue(),
            datesStatusHistory.keySet().size());
    for (Date date : datesStatusHistory.keySet()) {
        System.out.println(date);
        Assert.assertTrue(DateUtils.isSameDay(new Date(), date) || date.before(new Date()));
    }

}

From source file:TripServlet.java

void filterTodayTrips() {
    Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
    filterdtrips.clear();//from  www. jav a 2s. com
    for (int i = 0; i < alltrips.size(); i++) {
        Date d = alltrips.get(i).start_Date;
        if (DateUtils.isSameDay(d, today)) {
            filterdtrips.add(alltrips.get(i));
        }
    }
}

From source file:net.audumla.scheduler.quartz.SunScheduleTest.java

@Test
public void test2SecondInterval3TimesEveryRise() throws Exception {
    AstronomicEvent event = getSunRiseEvent();
    Date now = new Date();
    if (event.getCalculatedEventTime().before(now)) {
        now = DateUtils.addDays(now, 1);
    }/*from   w ww . ja  va  2 s  .c  o m*/
    AstronomicTriggerImpl trigger = (AstronomicTriggerImpl) AstronomicScheduleBuilder.astronomicalSchedule()
            .startEvent(event).startEventOffset(0).forEveryEvent().withRepeatCount(3).withIntervalInSeconds(2)
            .build();
    trigger.computeFirstFireTime(null);
    Date fire = trigger.getNextFireTime();
    assert DateUtils.isSameDay(fire, now);
    trigger.triggered(null);
    Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime() + 2000, 10);
    trigger.triggered(null);
    Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime() + 4000, 10);
    trigger.triggered(null);
    Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime() + 6000, 10);

    trigger.triggered(null);
    fire = trigger.getNextFireTime();
    assert DateUtils.isSameDay(fire, DateUtils.addDays(now, 1));
    trigger.triggered(null);
    Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime() + 2000, 10);
    trigger.triggered(null);
    Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime() + 4000, 10);
    trigger.triggered(null);
    Assert.assertEquals(trigger.getNextFireTime().getTime(), fire.getTime() + 6000, 10);

}

From source file:net.audumla.climate.bom.ObserverTest.java

@Test
public void testSequentialObservations() throws IOException {
    Date now = DateUtils.addDays(new Date(), -2);
    ClimateDataSource source = ClimateDataSourceFactory.getInstance().newInstance();
    source.setId("086068");
    ClimateObserver station = ClimateObserverCatalogue.getInstance().getClimateObserver(source);
    ClimateData cd = station.getClimateData(now);
    ClimateObservation obs = cd.getObservation(now, ClimateData.ObservationMatch.SUBSEQUENT);
    Date lastDate = null;/*from   w  w w.j  a  v a  2s .c om*/
    while (obs != null
            && obs.getDataSource().getType() == ClimateDataSource.ClimateDataSourceType.PERIODIC_OBSERVATION) {
        lastDate = obs.getTime();
        logger.debug(lastDate + " - " + obs.getDataSource().getType());
        obs = obs.getNextObservation();

    }
    Assert.assertFalse(DateUtils.isSameDay(lastDate, now));
}

From source file:com.emental.mindraider.ui.outline.treetable.OutlineTreeInstance.java

/**
 * Get string to be rendered for creation time (today, 6 days, this year,
 * another year)./* w  ww.  ja  v  a  2 s .  c  om*/
 *
 * @param created
 * @return
 */
public static String getCreatedToRender(long created) {
    int type = CREATED;

    Date date = new Date(created);
    Date today = new Date(System.currentTimeMillis());
    SimpleDateFormat simpleDateFormat;

    /*
     * if (date.getYear() == today.getYear() && date.getMonth() ==
     * today.getMonth() && date.getDate() == today.getDate())
     */
    if (DateUtils.isSameDay(date, today)) {
        type = CREATED_TODAY;
    } else {
        if (System.currentTimeMillis() - created < SIX_DAYS) {
            type = CREATED_6_DAYS;
        } else {
            if (date.getYear() == today.getYear()) {
                type = CREATED_THIS_YEAR;
            }
        }
    }

    String bgColor = null, text = null;
    switch (type) {
    case CREATED:
        bgColor = "bbbbbb";
        simpleDateFormat = new SimpleDateFormat("yyyy");
        text = simpleDateFormat.format(date);
        break;
    case CREATED_6_DAYS:
        bgColor = "555555";
        simpleDateFormat = new SimpleDateFormat("EEE", new Locale("en", "US"));
        text = simpleDateFormat.format(date);
        break;
    case CREATED_THIS_YEAR:
        bgColor = "888888";
        simpleDateFormat = new SimpleDateFormat("MMM dd", new Locale("en", "US"));
        text = simpleDateFormat.format(date);
        break;
    case CREATED_TODAY:
        bgColor = "000000";
        simpleDateFormat = new SimpleDateFormat("HH:mm");
        text = simpleDateFormat.format(date);
        break;
    }

    // TODO colorize & transform according to the date

    // note that long timestamp is added to the HTML - it is convenient for parsing/sorting/processing
    return "<html><body bgColor='#" + bgColor + "'><font color='#ffffff'>&nbsp;&nbsp;" + text
            + "&nbsp;&nbsp;</font><span hidden='" + created + "'/></body></html>";
}

From source file:com.delicious.deliciousfeeds4J.DeliciousUtilTest.java

@Test
public void testDeserializeBookmarksFromJson() throws Exception {

    final String SAMPLE_DATA = "[{\"a\": \"test123\", \"d\": \"Writing Reactive Apps with ReactiveMongo and Play\", "
            + "\"n\": \"\", \"u\": \"http://stephane.godbillon.com/2012/10/18/writing-a-simple-app-with-reactivemongo-and-play-framework-pt-1.html\","
            + " \"t\": [\"web\", \"programming\", \"mongodb\", \"scala\"], \"dt\": \"2012-10-22T13:40:31Z\", \"md5\": \"4967ef979fca2b4629c3d5ad70f83c01\"}]";

    final List<Bookmark> bookmarks = DeliciousUtil.deserializeBookmarksFromJson(SAMPLE_DATA);

    assertNotNull(bookmarks);/* w  w  w  . jav a  2s  .  co m*/
    assertFalse(bookmarks.isEmpty());
    assertEquals(1, bookmarks.size());

    final Bookmark bookmark = bookmarks.iterator().next();

    assertEquals("test123", bookmark.getUser());
    assertEquals("Writing Reactive Apps with ReactiveMongo and Play", bookmark.getTitle());
    assertEquals("", bookmark.getDescription());
    assertEquals(
            "http://stephane.godbillon.com/2012/10/18/writing-a-simple-app-with-reactivemongo-and-play-framework-pt-1.html",
            bookmark.getUrl());
    assertNotNull(bookmark.getTags());
    assertTrue(bookmark.getTags().contains("web"));

    final Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.DAY_OF_MONTH, 22);
    calendar.set(Calendar.MONTH, Calendar.OCTOBER);
    calendar.set(Calendar.YEAR, 2012);

    assertTrue(DateUtils.isSameDay(calendar.getTime(), bookmark.getLastUpdatedDate()));
}