Java Code Examples for com.ibm.icu.util.Calendar#setTime()

The following examples show how to use com.ibm.icu.util.Calendar#setTime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateRange.java    From org.openntf.domino with Apache License 2.0 6 votes vote down vote up
@Override
public List<Date> getDays() {
	try {
		final List<Date> dates = new ArrayList<Date>();
		final Calendar calendar = new GregorianCalendar();
		calendar.setTime(this.getStartDate());

		while (calendar.getTime().before(this.getEndDate())) {
			final Date result = calendar.getTime();
			dates.add(result);
			calendar.add(Calendar.DATE, 1);
		}

		return dates;

	} catch (final Exception e) {
		throw new RuntimeException((null == e.getCause()) ? e : e.getCause());
	}
}
 
Example 2
Source File: DateTime.java    From org.openntf.domino with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isAfterIgnoreTime(final org.openntf.domino.DateTime compareDate) {
	Calendar cal = calendar.get();
	cal.setTime(date_);
	cal.set(Calendar.HOUR_OF_DAY, 0);
	cal.set(Calendar.MINUTE, 0);
	cal.set(Calendar.SECOND, 0);
	cal.set(Calendar.MILLISECOND, 0);
	Date d1 = cal.getTime();
	cal.setTime(compareDate.toJavaDate());
	cal.set(Calendar.HOUR_OF_DAY, 0);
	cal.set(Calendar.MINUTE, 0);
	cal.set(Calendar.SECOND, 0);
	cal.set(Calendar.MILLISECOND, 0);
	Date d2 = cal.getTime();
	return d1.after(d2);
}
 
Example 3
Source File: PortfolioCourseNodeRunController.java    From olat with Apache License 2.0 6 votes vote down vote up
private Date getDeadline() {
    final String type = (String) config.get(PortfolioCourseNodeConfiguration.DEADLINE_TYPE);
    if (StringHelper.containsNonWhitespace(type)) {
        switch (DeadlineType.valueOf(type)) {
        case none:
            return null;
        case absolut:
            final Date date = (Date) config.get(PortfolioCourseNodeConfiguration.DEADLINE_DATE);
            return date;
        case relative:
            final Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            boolean applied = applyRelativeToDate(cal, PortfolioCourseNodeConfiguration.DEADLINE_MONTH, Calendar.MONTH, 1);
            applied |= applyRelativeToDate(cal, PortfolioCourseNodeConfiguration.DEADLINE_WEEK, Calendar.DATE, 7);
            applied |= applyRelativeToDate(cal, PortfolioCourseNodeConfiguration.DEADLINE_DAY, Calendar.DATE, 1);
            if (applied) {
                return cal.getTime();
            }
            return null;
        default:
            return null;
        }
    }
    return null;
}
 
Example 4
Source File: DateTime.java    From org.openntf.domino with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equalsIgnoreTime(final org.openntf.domino.DateTime compareDate) {
	Calendar cal = calendar.get();
	cal.setTime(date_);
	cal.set(Calendar.HOUR_OF_DAY, 0);
	cal.set(Calendar.MINUTE, 0);
	cal.set(Calendar.SECOND, 0);
	cal.set(Calendar.MILLISECOND, 0);
	Date d1 = cal.getTime();
	cal.setTime(compareDate.toJavaDate());
	cal.set(Calendar.HOUR_OF_DAY, 0);
	cal.set(Calendar.MINUTE, 0);
	cal.set(Calendar.SECOND, 0);
	cal.set(Calendar.MILLISECOND, 0);
	Date d2 = cal.getTime();
	return d1.equals(d2);
}
 
Example 5
Source File: ServerHitBin.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
private static long getEvenStartingTime(long binLength) {
    // binLengths should be a divisable evenly into 1 hour
    long curTime = System.currentTimeMillis();

    // find the first previous millis that are even on the hour
    Calendar cal = Calendar.getInstance();

    cal.setTime(new Date(curTime));
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    while (cal.getTime().getTime() < (curTime - binLength)) {
        cal.add(Calendar.MILLISECOND, (int) binLength);
    }

    return cal.getTime().getTime();
}
 
Example 6
Source File: PortfolioCourseNodeRunController.java    From olat with Apache License 2.0 6 votes vote down vote up
private Date getDeadline() {
    final String type = (String) config.get(PortfolioCourseNodeConfiguration.DEADLINE_TYPE);
    if (StringHelper.containsNonWhitespace(type)) {
        switch (DeadlineType.valueOf(type)) {
        case none:
            return null;
        case absolut:
            final Date date = (Date) config.get(PortfolioCourseNodeConfiguration.DEADLINE_DATE);
            return date;
        case relative:
            final Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            boolean applied = applyRelativeToDate(cal, PortfolioCourseNodeConfiguration.DEADLINE_MONTH, Calendar.MONTH, 1);
            applied |= applyRelativeToDate(cal, PortfolioCourseNodeConfiguration.DEADLINE_WEEK, Calendar.DATE, 7);
            applied |= applyRelativeToDate(cal, PortfolioCourseNodeConfiguration.DEADLINE_DAY, Calendar.DATE, 1);
	if (applied) { return cal.getTime(); }
            return null;
        default:
            return null;
        }
    }
    return null;
}
 
Example 7
Source File: RecurrenceUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/** Returns a String from a Date object */
public static String formatDate(Date date) {
    String formatString = "";
    Calendar cal = Calendar.getInstance();

    cal.setTime(date);
    if (cal.isSet(Calendar.MINUTE)) {
        formatString = "yyyyMMdd'T'hhmmss";
    } else {
        formatString = "yyyyMMdd";
    }
    SimpleDateFormat formatter = new SimpleDateFormat(formatString);

    return formatter.format(date);
}
 
Example 8
Source File: DateTime.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
@Override
public Calendar toJavaCal() {
	if (date_ != null) {
		Calendar result = GregorianCalendar.getInstance();
		result.setTime(date_);
		return result;
	} else {
		return null;
	}
}
 
Example 9
Source File: DateTime.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
@Override
public void setLocalTime(final Date date) {
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	setLocalTime(cal);
	//		try {
	//			lotus.domino.DateTime worker = getWorker();
	//			worker.setLocalTime(date);
	//			workDone(worker, true);
	//		} catch (NotesException ne) {
	//			DominoUtils.handleException(ne);
	//		}
}
 
Example 10
Source File: DateTime.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isBeforeIgnoreDate(final org.openntf.domino.DateTime compareDate) {
	Calendar cal = calendar.get();
	cal.setTime(date_);
	cal.set(Calendar.DAY_OF_MONTH, 1);
	cal.set(Calendar.MONTH, 0);
	cal.set(Calendar.YEAR, 2000);
	Date d1 = cal.getTime();
	cal.setTime(compareDate.toJavaDate());
	cal.set(Calendar.DAY_OF_MONTH, 1);
	cal.set(Calendar.MONTH, 0);
	cal.set(Calendar.YEAR, 2000);
	Date d2 = cal.getTime();
	return d1.before(d2);
}
 
Example 11
Source File: DateTimeSpan.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param startDate
 *            A date object that represents the start of the span
 * @param endDate
 *            A date object that represents the end of the span
 * @return the number of years between two dates
 */
static int years( Date startDate, Date endDate )
{
	if ( startDate == null || endDate == null )
	{
		return 0;
	}
	
	if ( !validateDateArgus( startDate, endDate ) )
	{
		return -years( endDate, startDate );
	}
	
	Calendar startCal = Calendar.getInstance( );
	startCal.setTime( startDate );
	//Get the year of startDate
	int startYear = startCal.get( Calendar.YEAR ) - 1900;
	Calendar endCal = Calendar.getInstance( );
	endCal.setTime( endDate );
	//Get the year of endDate
	int endYear = endCal.get( Calendar.YEAR ) - 1900;
	assert ( endYear >= startYear );
	//Get the span of the endYear and the startYear, only thinking of the
	// year but month and day
	int spanYear = endYear - startYear;
	//startCal.set( Calendar.YEAR, endYear + 1900 );
	startCal.add( Calendar.YEAR, spanYear );
	startDate = startCal.getTime( );
	endDate = endCal.getTime( );
	/*
	 * the value 0 if the argument is a Date equal to this Date; a value
	 * less than 0 if the argument is a Date after this Date; and a value
	 * greater than 0 if the argument is a Date before this Date.
	 */
	if ( startDate.compareTo( endDate ) > 0 )
	{
		spanYear -= 1;
	}
	return spanYear;
}
 
Example 12
Source File: TemporalExpressions.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
protected Calendar prepareCal(Calendar cal) {
    // Performs a "sane" skip forward in time - avoids time consuming loops
    // like incrementing every second from Jan 1 2000 until today
    Calendar skip = (Calendar) cal.clone();
    skip.setTime(this.start);
    long deltaMillis = cal.getTimeInMillis() - this.start.getTime();
    if (deltaMillis < 1000) {
        return skip;
    }
    long divisor = deltaMillis;
    if (this.freqType == Calendar.DAY_OF_MONTH) {
        divisor = 86400000;
    } else if (this.freqType == Calendar.HOUR) {
        divisor = 3600000;
    } else if (this.freqType == Calendar.MINUTE) {
        divisor = 60000;
    } else if (this.freqType == Calendar.SECOND) {
        divisor = 1000;
    } else {
        return skip;
    }
    long units = deltaMillis / divisor;
    units -= units % this.freqCount;
    skip.add(this.freqType, (int)units);
    while (skip.after(cal)) {
        skip.add(this.freqType, -this.freqCount);
    }
    return skip;
}
 
Example 13
Source File: TimeMemberUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static Calendar getCalendar( Date d )
{
	if ( d == null )
		throw new java.lang.IllegalArgumentException( "date value is null!" );
	Calendar c = Calendar.getInstance( );
	c.setTime( d );
	return c;
}
 
Example 14
Source File: UtilDateTime.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public static int weekNumber(Timestamp input, int startOfWeek) {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(startOfWeek);

    if (startOfWeek == Calendar.MONDAY) {
       calendar.setMinimalDaysInFirstWeek(4);
    } else if (startOfWeek == Calendar.SUNDAY) {
       calendar.setMinimalDaysInFirstWeek(3);
    }

    calendar.setTime(new java.util.Date(input.getTime()));
    return calendar.get(Calendar.WEEK_OF_YEAR);
}
 
Example 15
Source File: DateTimeConverters.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public java.sql.Date convert(java.util.Date obj) throws ConversionException {
    Calendar cal = Calendar.getInstance();
    cal.setTime(obj);
    cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return new java.sql.Date(cal.getTimeInMillis());
}
 
Example 16
Source File: DateTimeSpan.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param startDate
 *            A date object that represents the start of the span
 * @param endDate
 *            A date object that represents the end of the span
 * @return the number of months between two dates
 */
static int months( Date startDate, Date endDate )
{
	if ( startDate == null || endDate == null )
	{
		return 0;
	}
	
	if ( !validateDateArgus( startDate, endDate ) )
	{
		return -months( endDate, startDate );
	}
	
	Calendar startCal = Calendar.getInstance( );
	startCal.setTime( startDate );
	
	Calendar endCal = Calendar.getInstance();
	endCal.setTime(endDate);
	
	int startMonth = startCal.get(Calendar.YEAR) * 12 + startCal.get(Calendar.MONTH);
	int endMonth =  endCal.get(Calendar.YEAR) * 12 + endCal.get(Calendar.MONTH);
	
	int spanMonth = endMonth - startMonth;
	
	startCal.add(Calendar.MONTH, spanMonth);
	
	if (startCal.getTime().compareTo(endCal.getTime()) > 0)
	{
		spanMonth --;
	}
	
	return spanMonth;
}
 
Example 17
Source File: RecurrenceRule.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
private Date getNextFreq(long startTime, long fromTime) {
    // Build a Calendar object
    Calendar cal = Calendar.getInstance();

    cal.setTime(new Date(startTime));

    long nextStartTime = startTime;

    while (nextStartTime < fromTime) {
        switch (getFrequency()) {
        case SECONDLY:
            cal.add(Calendar.SECOND, getIntervalInt());
            break;

        case MINUTELY:
            cal.add(Calendar.MINUTE, getIntervalInt());
            break;

        case HOURLY:
            cal.add(Calendar.HOUR_OF_DAY, getIntervalInt());
            break;

        case DAILY:
            cal.add(Calendar.DAY_OF_MONTH, getIntervalInt());
            break;

        case WEEKLY:
            cal.add(Calendar.WEEK_OF_YEAR, getIntervalInt());
            break;

        case MONTHLY:
            cal.add(Calendar.MONTH, getIntervalInt());
            break;

        case YEARLY:
            cal.add(Calendar.YEAR, getIntervalInt());
            break;

        default:
            return null; // should never happen
        }
        nextStartTime = cal.getTime().getTime();
    }
    return new Date(nextStartTime);
}
 
Example 18
Source File: InfoManagerITCase.java    From olat with Apache License 2.0 4 votes vote down vote up
@Test
public void testCount() {
    final String resName = UUID.randomUUID().toString();
    final String subPath = UUID.randomUUID().toString();
    final String businessPath = UUID.randomUUID().toString();
    final InfoOLATResourceable ores = new InfoOLATResourceable(7l, resName);

    final InfoMessage msg1 = infoMessageManager.createInfoMessage(ores, subPath, businessPath, id1);
    assertNotNull(msg1);
    infoMessageManager.saveInfoMessage(msg1);
    final Calendar cal = Calendar.getInstance();
    cal.setTime(msg1.getCreationDate());
    cal.add(Calendar.SECOND, -1);
    final Date after = cal.getTime();

    final InfoMessage msg2 = infoMessageManager.createInfoMessage(ores, subPath, businessPath, id1);
    assertNotNull(msg2);
    infoMessageManager.saveInfoMessage(msg2);

    final InfoMessage msg3 = infoMessageManager.createInfoMessage(ores, subPath, businessPath, id1);
    assertNotNull(msg3);
    infoMessageManager.saveInfoMessage(msg3);

    cal.setTime(msg3.getCreationDate());
    cal.add(Calendar.SECOND, +1);
    final Date before = cal.getTime();

    dbInstance.commitAndCloseSession();

    final int count1 = infoMessageManager.countInfoMessageByResource(ores, null, null, null, null);
    assertEquals(3, count1);

    final int count2 = infoMessageManager.countInfoMessageByResource(ores, subPath, null, null, null);
    assertEquals(3, count2);

    final int count3 = infoMessageManager.countInfoMessageByResource(ores, subPath, businessPath, null, null);
    assertEquals(3, count3);

    final int count4 = infoMessageManager.countInfoMessageByResource(ores, subPath, businessPath, after, null);
    assertEquals(3, count4);

    final int count5 = infoMessageManager.countInfoMessageByResource(ores, subPath, businessPath, after, before);
    assertEquals(3, count5);
}
 
Example 19
Source File: UtilHttp.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
/**
 * Given the prefix of a composite parameter, recomposes a single Object from
 * the composite according to compositeType. For example, consider the following
 * form widget field,
 *
 * <pre>
 * {@code
 * <field name="meetingDate">
 *     <date-time type="timestamp" input-method="time-dropdown">
 * </field>
 * }
 * </pre>
 *
 * The result in HTML is three input boxes to input the date, hour and minutes separately.
 * The parameter names are named meetingDate_c_date, meetingDate_c_hour, meetingDate_c_minutes.
 * Additionally, there will be a field named meetingDate_c_compositeType with a value of "Timestamp".
 * where _c_ is the COMPOSITE_DELIMITER. These parameters will then be recomposed into a Timestamp
 * object from the composite fields.
 *
 * @param request
 * @param prefix
 * @return Composite object from data or null if not supported or a parsing error occurred.
 */
public static Object makeParamValueFromComposite(HttpServletRequest request, String prefix, Locale locale) {
    String compositeType = request.getParameter(makeCompositeParam(prefix, "compositeType"));
    if (UtilValidate.isEmpty(compositeType)) {
        return null;
    }

    // collect the composite fields into a map
    Map<String, String> data = new HashMap<>();
    for (Enumeration<String> names = UtilGenerics.cast(request.getParameterNames()); names.hasMoreElements();) {
        String name = names.nextElement();
        if (!name.startsWith(prefix + COMPOSITE_DELIMITER)) {
            continue;
        }

        // extract the suffix of the composite name
        String suffix = name.substring(name.indexOf(COMPOSITE_DELIMITER) + COMPOSITE_DELIMITER_LENGTH);

        // and the value of this parameter
        String value = request.getParameter(name);

        // key = suffix, value = parameter data
        data.put(suffix, value);
    }
    if (Debug.verboseOn()) { Debug.logVerbose("Creating composite type with parameter data: " + data.toString(), module); }

    // handle recomposition of data into the compositeType
    if ("Timestamp".equals(compositeType)) {
        String date = data.get("date");
        String hour = data.get("hour");
        String minutes = data.get("minutes");
        String ampm = data.get("ampm");
        if (date == null || date.length() < 10) {
            return null;
        }
        if (UtilValidate.isEmpty(hour)) {
            return null;
        }
        if (UtilValidate.isEmpty(minutes)) {
            return null;
        }
        boolean isTwelveHour = UtilValidate.isNotEmpty(ampm);

        // create the timestamp from the data
        try {
            int h = Integer.parseInt(hour);
            Timestamp timestamp = Timestamp.valueOf(date.substring(0, 10) + " 00:00:00.000");
            Calendar cal = Calendar.getInstance(locale);
            cal.setTime(timestamp);
            if (isTwelveHour) {
                boolean isAM = ("AM".equals(ampm) ? true : false);
                if (isAM && h == 12) {
                    h = 0;
                }
                if (!isAM && h < 12) {
                    h += 12;
                }
            }
            cal.set(Calendar.HOUR_OF_DAY, h);
            cal.set(Calendar.MINUTE, Integer.parseInt(minutes));
            return new Timestamp(cal.getTimeInMillis());
        } catch (IllegalArgumentException e) {
            Debug.logWarning("User input for composite timestamp was invalid: " + e.getMessage(), module);
            return null;
        }
    }

    // we don't support any other compositeTypes (yet)
    return null;
}
 
Example 20
Source File: DominoUtils.java    From org.openntf.domino with Apache License 2.0 3 votes vote down vote up
/**
 * To java calendar safe.
 *
 * @param dt
 *            the dt
 * @return the calendar
 */
public static Calendar toJavaCalendarSafe(final lotus.domino.DateTime dt) {
	Date d = DominoUtils.toJavaDateSafe(dt);
	Calendar c = Calendar.getInstance(ULocale.getDefault());
	c.setTime(d);
	return c;
}