Example usage for org.apache.commons.lang3.time DateUtils isSameInstant

List of usage examples for org.apache.commons.lang3.time DateUtils isSameInstant

Introduction

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

Prototype

public static boolean isSameInstant(final Calendar cal1, final Calendar cal2) 

Source Link

Document

Checks if two calendar objects represent the same instant in time.

This method compares the long millisecond time of the two objects.

Usage

From source file:SampleLang.java

public static void checkDate() throws InterruptedException, ParseException {
    //date1 created
    Date date1 = new Date();
    //Print the date and time at this instant
    System.out.println("The time right now is >>" + date1);
    //Thread sleep for 1000 ms
    Thread.currentThread().sleep(DateUtils.MILLIS_PER_MINUTE);
    //date2 created.
    Date date2 = new Date();
    //Check if date1 and date2 have the same day
    System.out.println("Is Same Day >> " + DateUtils.isSameDay(date1, date2));
    //Check if date1 and date2 have the same instance
    System.out.println("Is Same Instant >> " + DateUtils.isSameInstant(date1, date2));
    //Round the hour
    System.out.println("Date after rounding >>" + DateUtils.round(date1, Calendar.HOUR));
    //Truncate the hour
    System.out.println("Date after truncation >>" + DateUtils.truncate(date1, Calendar.HOUR));
    //Three dates in three different formats
    String[] dates = { "2005.03.24 11:03:26", "2005-03-24 11:03", "2005/03/24" };
    //Iterate over dates and parse strings to java.util.Date objects
    for (int i = 0; i < dates.length; i++) {
        Date parsedDate = DateUtils.parseDate(dates[i],
                new String[] { "yyyy/MM/dd", "yyyy.MM.dd HH:mm:ss", "yyyy-MM-dd HH:mm" });
        System.out.println("Parsed Date is >>" + parsedDate);
    }/*from   w  ww  .  j  a  v a2s .com*/
    //Display date in HH:mm:ss format
    System.out.println("Now >>" + DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(System.currentTimeMillis()));
}

From source file:com.adobe.acs.commons.http.headers.impl.DailyExpiresHeaderFilterTest.java

@Test
public void testAdjustExpiresPast() throws Exception {

    Calendar actual = Calendar.getInstance();
    actual.set(Calendar.SECOND, 0);
    actual.set(Calendar.MILLISECOND, 0);

    Calendar expected = Calendar.getInstance();
    expected.setTime(actual.getTime());//from ww  w  .  j ava  2  s  .  c  om
    expected.add(Calendar.DAY_OF_MONTH, 1);

    filter.adjustExpires(actual);

    assertTrue(DateUtils.isSameInstant(expected, actual));
}

From source file:com.adobe.acs.commons.http.headers.impl.DailyExpiresHeaderFilterTest.java

@Test
public void testAdjustExpiresFuture() throws Exception {

    Calendar actual = Calendar.getInstance();
    actual.add(Calendar.MINUTE, 1);
    actual.set(Calendar.SECOND, 0);
    actual.set(Calendar.MILLISECOND, 0);

    Calendar expected = Calendar.getInstance();
    expected.setTime(actual.getTime());//w w  w . j av a  2s.  c  om

    filter.adjustExpires(actual);

    assertTrue(DateUtils.isSameInstant(expected, actual));
}

From source file:com.adobe.acs.commons.http.headers.impl.WeeklyExpiresHeaderFilterTest.java

@Test
public void testAdjustExpiresPastWeekday() throws Exception {

    Calendar actual = Calendar.getInstance();
    actual.set(Calendar.SECOND, 0);
    actual.set(Calendar.MILLISECOND, 0);

    Calendar expected = Calendar.getInstance();
    expected.setTime(actual.getTime());// w ww .  ja v a2  s.c  o  m
    expected.add(Calendar.DAY_OF_WEEK, -3);
    expected.add(Calendar.DAY_OF_WEEK, 7);

    int dayOfWeek = expected.get(Calendar.DAY_OF_WEEK);
    properties.put(WeeklyExpiresHeaderFilter.PROP_EXPIRES_DAY_OF_WEEK, dayOfWeek);

    filter.doActivate(componentContext);
    filter.adjustExpires(actual);

    assertTrue(DateUtils.isSameInstant(expected, actual));
    assertEquals(dayOfWeek, actual.get(Calendar.DAY_OF_WEEK));
}

From source file:com.adobe.acs.commons.http.headers.impl.MonthlyExpiresHeaderFilterTest.java

@Test
public void testAdjustExpiresPastDay() throws Exception {

    Calendar actual = Calendar.getInstance();
    actual.set(Calendar.SECOND, 0);
    actual.set(Calendar.MILLISECOND, 0);

    Calendar expected = Calendar.getInstance();
    expected.setTime(actual.getTime());// w  ww. j  ava 2s .  c o m
    expected.add(Calendar.DAY_OF_MONTH, -3);
    expected.add(Calendar.MONTH, 1);

    actual.set(Calendar.DAY_OF_MONTH, 15);

    final int month = expected.get(Calendar.MONTH);
    properties.put(MonthlyExpiresHeaderFilter.PROP_EXPIRES_DAY_OF_MONTH, expected.get(Calendar.DAY_OF_MONTH));

    filter.doActivate(componentContext);
    filter.adjustExpires(actual);

    assertTrue(DateUtils.isSameInstant(expected, actual));
    assertEquals(month, actual.get(Calendar.MONTH));
}

From source file:com.adobe.acs.commons.http.headers.impl.WeeklyExpiresHeaderFilterTest.java

@Test
public void testAdjustExpiresSameDayPast() throws Exception {

    Calendar actual = Calendar.getInstance();
    actual.set(Calendar.SECOND, 0);
    actual.set(Calendar.MILLISECOND, 0);

    Calendar expected = Calendar.getInstance();
    expected.setTime(actual.getTime());/*w  w  w  .  j av  a  2 s  .  c o m*/
    expected.add(Calendar.DAY_OF_WEEK, 7);

    int dayOfWeek = expected.get(Calendar.DAY_OF_WEEK);
    properties.put(WeeklyExpiresHeaderFilter.PROP_EXPIRES_DAY_OF_WEEK, dayOfWeek);

    filter.doActivate(componentContext);
    filter.adjustExpires(actual);

    assertTrue(DateUtils.isSameInstant(expected, actual));
    assertEquals(dayOfWeek, actual.get(Calendar.DAY_OF_WEEK));
}

From source file:com.inkubator.common.util.DateTimeUtil.java

/**
 * Checking from two date objects (included time ), will return true if the
 * date have the same value and same time instance.
 *
 * @return Boolean/*  ww w  .j ava2s  .  c  om*/
 * @param date1 Date reference
 * @param date2 Date reference
 */
public static Boolean isSameDateAndTime(Date date1, Date date2) {
    return DateUtils.isSameInstant(date1, date2);
}

From source file:com.adobe.acs.commons.http.headers.impl.MonthlyExpiresHeaderFilterTest.java

@Test
public void testAdjustExpiresSameDayPast() throws Exception {

    Calendar actual = Calendar.getInstance();
    actual.set(Calendar.SECOND, 0);
    actual.set(Calendar.MILLISECOND, 0);

    Calendar expected = Calendar.getInstance();
    expected.setTime(actual.getTime());//  ww w.j a v  a  2 s  . c o m
    expected.add(Calendar.MONTH, 1);

    final int month = expected.get(Calendar.MONTH);
    properties.put(MonthlyExpiresHeaderFilter.PROP_EXPIRES_DAY_OF_MONTH, expected.get(Calendar.DAY_OF_MONTH));

    filter.doActivate(componentContext);
    filter.adjustExpires(actual);

    assertTrue(DateUtils.isSameInstant(expected, actual));
    assertEquals(month, actual.get(Calendar.MONTH));
}

From source file:com.adobe.acs.commons.http.headers.impl.AbstractExpiresHeaderFilterTest.java

@Test
public void testGetHeaderValue() throws Exception {

    when(componentContext.getProperties()).thenReturn(properties);

    Calendar expected = Calendar.getInstance();
    expected.set(Calendar.HOUR_OF_DAY, 2);
    expected.set(Calendar.MINUTE, 30);
    expected.set(Calendar.SECOND, 0);
    expected.set(Calendar.MILLISECOND, 0);

    filter.doActivate(componentContext);
    String header = filter.getHeaderValue();
    Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").parse(header);
    Calendar actual = Calendar.getInstance();
    actual.setTime(date);/*from   w w  w .  ja va  2s. co m*/
    actual.set(Calendar.SECOND, 0);
    actual.set(Calendar.MILLISECOND, 0);

    assertTrue(DateUtils.isSameInstant(expected, actual));
}

From source file:com.adobe.acs.commons.http.headers.impl.WeeklyExpiresHeaderFilterTest.java

@Test
public void testAdjustExpiresSameDayFuture() throws Exception {

    Calendar actual = Calendar.getInstance();
    actual.add(Calendar.MINUTE, 1);
    actual.set(Calendar.SECOND, 0);
    actual.set(Calendar.MILLISECOND, 0);

    Calendar expected = Calendar.getInstance();
    expected.setTime(actual.getTime());/*  ww w .ja  v a2  s .  c  o m*/
    int dayOfWeek = expected.get(Calendar.DAY_OF_WEEK);
    properties.put(WeeklyExpiresHeaderFilter.PROP_EXPIRES_DAY_OF_WEEK, dayOfWeek);

    filter.doActivate(componentContext);
    filter.adjustExpires(actual);

    assertTrue(DateUtils.isSameInstant(expected, actual));
    assertEquals(dayOfWeek, actual.get(Calendar.DAY_OF_WEEK));
}