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

The following examples show how to use com.ibm.icu.util.Calendar#getTime() . 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: DateTimeSpan.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @param startDate
 *            A date object that represent the bass date.
 * @param years
 *            The number of years to add to the date.
 * @param months
 *            The number of month to add to the date.
 * @param days
 *            The number of days to add to the date.
 * @return A date that results from adding the years, months, and days to
 *         the start date.
 */
static Date addDate( Date startDate, int years, int months, int days )
{
	Calendar startCal = Calendar.getInstance( );
	Date firstDate = startDate;
	startCal.setTime( firstDate );
	/*
	 * Add years first. Then, using the resulting date, add the months.
	 * Then, using the resulting date, add the days.
	 */
	startCal.add( Calendar.YEAR, years );
	startCal.add( Calendar.MONTH, months );
	startCal.add( Calendar.DATE, days );

	return startCal.getTime( );
}
 
Example 2
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
protected Object getValue( Object[] args, IScriptFunctionContext context ) throws BirtException
{
	if ( existNullValue( args ) )
	{
		return null;
	}
	Calendar current;
	if ( args[0] instanceof Number )
	{
		current = getFiscalYearStateDate( context, args );
		// Month starts with 1
		current.add( Calendar.MONTH,
				( (Number) args[0] ).intValue( ) - 1 );
	}
	else
	{
		current = getCalendar( DataTypeUtil.toDate( args[0] ) );
		Calendar start = getFiscalYearStateDate( context, args );
		adjustFiscalMonth( current, start );
		// Do not exceed the max days of current month
		current.set( Calendar.DATE,
				Math.min( start.get( Calendar.DATE ),
						current.getActualMaximum( Calendar.DATE ) ) );
	}
	return current.getTime( );
}
 
Example 3
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 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: 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 6
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected Object getValue( Object[] args ) throws BirtException
{
	if( existNullValue( args ) )
	{
		return null;
	}
	Calendar cal = getCalendar( DataTypeUtil.toDate(args[0] ) );
	cal.set( Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek( ) );
	return cal.getTime( );
}
 
Example 7
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Add num years
 *
 * @param date
 * @param num
 * @return
 */
private static Date addYear( Date date, int num )
{
	Calendar startCal = getCalendar( date );

	startCal.add( Calendar.YEAR, num );

	return startCal.getTime( );
}
 
Example 8
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns a timestamp value which is midnight of the current date.
 *
 * @return
 */
private static Date today( )
{
	Calendar calendar = Calendar.getInstance( threadTimeZone.get( ) );
	calendar.set( Calendar.HOUR_OF_DAY, 0 );
	calendar.clear( Calendar.MINUTE );
	calendar.clear( Calendar.SECOND );
	calendar.clear( Calendar.MILLISECOND );

	return calendar.getTime( );
}
 
Example 9
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static Date getDate( int year, int month, int day, int hours,
		int minutes, int seconds )
{
	Date newDate = new Date( );
	Calendar calendar = getCalendar( newDate );
	calendar.set( year, month, day, hours, minutes, seconds );
	calendar.set( Calendar.MILLISECOND, 0 );
	return calendar.getTime( );
}
 
Example 10
Source File: MonthDateFormatTest.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public void testFormat( )
{
	Calendar calendar = Calendar.getInstance( );
	calendar.set( Calendar.YEAR, 2013 );
	calendar.set( Calendar.MONTH, 5 );
	calendar.set( Calendar.DATE, 8 );
	Date date = calendar.getTime( );
	Hashtable<ULocale, String> locales = new Hashtable<ULocale, String>( );
	locales.put( ULocale.CANADA, "Jun 2013" ); //$NON-NLS-1$
	locales.put( ULocale.CHINA, "2013年6月" ); //$NON-NLS-1$
	locales.put( ULocale.ENGLISH, "Jun 2013" ); //$NON-NLS-1$
	locales.put( ULocale.FRANCE, "juin 2013" ); //$NON-NLS-1$
	locales.put( ULocale.GERMAN, "06.2013" ); //$NON-NLS-1$
	locales.put( ULocale.ITALY, "giu 2013" ); //$NON-NLS-1$
	locales.put( ULocale.JAPAN, "2013/06" ); //$NON-NLS-1$
	locales.put( ULocale.KOREA, "2013. 6" ); //$NON-NLS-1$
	locales.put( ULocale.SIMPLIFIED_CHINESE, "2013年6月" ); //$NON-NLS-1$
	locales.put( ULocale.TAIWAN, "2013年6月" ); //$NON-NLS-1$
	locales.put( ULocale.TRADITIONAL_CHINESE, "2013年6月" ); //$NON-NLS-1$
	locales.put( ULocale.UK, "Jun 2013" ); //$NON-NLS-1$

	for ( Iterator<ULocale> itr = locales.keySet( ).iterator( ); itr.hasNext( ); )
	{
		ULocale locale = itr.next( );
		IDateFormatWrapper formatter = DateFormatWrapperFactory.getPreferredDateFormat( Calendar.MONTH,
				locale,
				true );
		assertEquals( locales.get( locale ), formatter.format( date ) );
	}
}
 
Example 11
Source File: SizeOfUtilTest.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param strLen
 * @param byteLen
 * @param nullPos
 * @return
 */
private ResultObject getResultObjectWithNull( int strLen,
		int byteLen, int[] nullPos )
{
	Object[] objectArray = new Object[8];
	Calendar calendar = Calendar.getInstance( );
	// constant size
	objectArray[0] = new Integer( 10 );
	objectArray[1] = new Double( 10 );
	objectArray[2] = new BigDecimal( "1111111111111111111111111111" );
	
	calendar.clear( );
	calendar.set( 1919, 2, 2 );
	objectArray[3] =calendar.getTime( );
	
	calendar.clear( );
	calendar.set( 1970, 0, 1, 19, 2, 2 );
	objectArray[4] = new Time( calendar.getTimeInMillis( ) );
	
	calendar.clear( );
	calendar.set( 1919, 2, 2, 2, 2, 2 );
	objectArray[5] = new Timestamp( calendar.getTimeInMillis( ) );
	((Timestamp)objectArray[5]).setNanos( 2 );
	
	// variable size
	objectArray[6] = new byte[byteLen];
	objectArray[7] = SizeOfUtil.newString( strLen );
	
	// set null for object element
	for ( int i = 0; i < nullPos.length; i++ )
		objectArray[nullPos[i]] = null;

	ResultObject object = new ResultObject( resultClass, objectArray );

	return object;
}
 
Example 12
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected Object getValue( Object[] args ) throws BirtException
{
	if( existNullValue( args ) )
	{
		return null;
	}
	Date date = DataTypeUtil.toDate(args[0]);
	Calendar cal = getCalendar( date );
	int quarter = quarter( date );

	cal.set( Calendar.MONTH, ( quarter - 1 ) * 3 );
	cal.set( Calendar.DAY_OF_MONTH, 1 );
	return cal.getTime( );
}
 
Example 13
Source File: DataTypeUtilTest.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
@Test
   public void testToDate2( )
{
	String[] dateStrings = {
			"Jan 11, 2002", "Jan 11, 2002", "Feb 12, 1981 6:17 AM"
	};
	String[] timeZoneIDs = {
			"GMT+00:00", "GMT-02:00", "GMT+03:00"
	};
	Calendar calendar = Calendar.getInstance( );
	calendar.setTimeZone( TimeZone.getTimeZone( "GMT+00:00" ) );
	Date[] resultDates = new Date[3];
	calendar.clear( );
	calendar.set(2002,0,11,0,0,0);
	resultDates[0] = calendar.getTime( );
	calendar.clear( );
	calendar.set(2002,0,11,2,0,0);
	resultDates[1] = calendar.getTime( );
	calendar.clear( );
	calendar.set(1981,1,12,3,17,0 );
	resultDates[2] = calendar.getTime( );
	
	
	for ( int i = 0; i < dateStrings.length; i++ )
	{
		try
		{
			Date dateResult = DataTypeUtil.toDate( dateStrings[i],
					ULocale.US,
					TimeZone.getTimeZone( timeZoneIDs[i] ) );
			assertEquals( dateResult, resultDates[i] );
		}
		catch ( BirtException e )
		{
			fail( "Should not throw Exception." );
		}
	}
}
 
Example 14
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected Object getValue( Object[] args, IScriptFunctionContext context ) throws BirtException
{
	if ( existNullValue( args ) )
	{
		return null;
	}
	Calendar current = null;
	if ( args[0] instanceof Number )
	{
		current = getFiscalYearStateDate( context, args );
		current.set( Calendar.YEAR, ( (Number) args[0] ).intValue( ) );
		if ( current.get( Calendar.DAY_OF_YEAR ) > 1 )
		{
			current.add( Calendar.YEAR, -1 );
		}
	}
	else
	{
		current = getCalendar( DataTypeUtil.toDate( args[0] ) );
		Calendar start = getFiscalYearStateDate( context, args );
		adjustFiscalYear( current, start );
		current.set( Calendar.MONTH, start.get( Calendar.MONTH ) );
		// Do not exceed the max days of current month
		current.set( Calendar.DATE,
				Math.min( start.get( Calendar.DATE ),
						current.getActualMaximum(
								Calendar.DATE ) ) );
	}
	return current.getTime( );
}
 
Example 15
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Add num days
 *
 * @param date
 * @param num
 * @return
 */
private static Date addDay( Date date, int num )
{
	Calendar startCal = getCalendar( date );

	startCal.add( Calendar.DATE, num );

	return startCal.getTime( );
}
 
Example 16
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Add num hours
 *
 * @param date
 * @param num
 * @return
 */
private static Date addHour( Date date, int num )
{
	Calendar startCal = getCalendar( date );

	startCal.add( Calendar.HOUR_OF_DAY, num );

	return startCal.getTime( );
}
 
Example 17
Source File: UtilDateTime.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public static Date getEarliestDate() {
    Calendar cal = getCalendarInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
    cal.set(Calendar.YEAR, cal.getActualMinimum(Calendar.YEAR));
    cal.set(Calendar.MONTH, cal.getActualMinimum(Calendar.MONTH));
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
}
 
Example 18
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Add num minutes
 *
 * @param date
 * @param num
 * @return
 */
private static Date addMinute( Date date, int num )
{
	Calendar startCal = getCalendar( date );

	startCal.add( Calendar.MINUTE, num );

	return startCal.getTime( );
}
 
Example 19
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
protected Object getValue( Object[] args ) throws BirtException
{
	if( existNullValue( args ) )
	{
		return null;
	}
	Calendar cal = getCalendar( DataTypeUtil.toDate(args[0] ) );
	cal.set( Calendar.DAY_OF_YEAR, 1 );
	return cal.getTime( );
}
 
Example 20
Source File: FormatterImpl.java    From org.openntf.domino with Apache License 2.0 4 votes vote down vote up
public Calendar parseDateToCalWithFormat(final String image, final String format, final boolean[] noDT, final boolean parseLenient) {
	Calendar ret = getInstance(iLocale);
	ret.setLenient(false);
	ParsePosition p = new ParsePosition(0);
	ret.clear();
	SimpleDateFormat sdf = new SimpleDateFormat(format, iLocale);
	sdf.parse(image, ret, p);
	boolean contDate = ret.isSet(YEAR) || ret.isSet(MONTH) || ret.isSet(DAY_OF_MONTH);
	boolean contTime = ret.isSet(HOUR_OF_DAY) || ret.isSet(HOUR) || ret.isSet(MINUTE) || ret.isSet(SECOND);
	boolean illegalDateString = !contDate && !contTime;
	if (!illegalDateString && !parseLenient) {
		int lh = image.length();
		int errInd = p.getErrorIndex();
		illegalDateString = (errInd < 0 && p.getIndex() < lh) || (errInd >= 0 && errInd < lh);
	}
	if (illegalDateString)
		throw new IllegalArgumentException("Illegal date string '" + image + "' for format '" + format + "'");
	//		System.out.println("Y=" + ret.isSet(YEAR) + " M=" + ret.isSet(MONTH) + " D=" + ret.isSet(DAY_OF_MONTH)
	//				+ " H=" + ret.isSet(HOUR_OF_DAY) + "m=" + ret.isSet(MINUTE) + " S=" + ret.isSet(SECOND));
	if (!ret.isSet(YEAR))
		ret.set(YEAR, 1970);
	if (!ret.isSet(MONTH))
		ret.set(MONTH, 0);
	if (!ret.isSet(DAY_OF_MONTH))
		ret.set(DAY_OF_MONTH, 1);
	if (!ret.isSet(HOUR_OF_DAY) && !ret.isSet(HOUR))
		ret.set(HOUR_OF_DAY, 0);
	if (!ret.isSet(MINUTE))
		ret.set(MINUTE, 0);
	if (!ret.isSet(SECOND))
		ret.set(SECOND, 0);
	try {
		ret.getTime();
	} catch (IllegalArgumentException e) {
		throw new IllegalArgumentException("Parsing '" + image + "' against '" + format + "' gives Calendar exception: "
				+ e.getMessage());
	}
	noDT[0] = !contDate;
	noDT[1] = !contTime;
	return ret;
}