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

The following examples show how to use com.ibm.icu.util.Calendar#getInstance() . 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: BirtDateTime.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Return difference in number of days
 *
 * @param d1
 * @param d2
 * @return
 */
private static long diffDay( Date d1, Date d2 )
{
	Calendar c1 = Calendar.getInstance( threadTimeZone.get( ) );
	c1.setTime( d1 );
	Calendar c2 = Calendar.getInstance( threadTimeZone.get( ) );
	c2.setTime( d2 );
	if ( c1.after( c2 ) )
	{
		return -diffDay( c2, c1 ) ;
	}
	else
	{
		return diffDay( c1, c2 );
	}
}
 
Example 2
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 *
 * @param d
 * @return
 */
private static Calendar getCalendar( Date d )
{
	Calendar c = null;
	if ( d instanceof java.sql.Date )
	{
		c = Calendar.getInstance( TimeZone.getDefault( ),
				threadLocale.get( ) );
	}
	else
	{
		c = Calendar.getInstance( threadTimeZone.get( ),
				threadLocale.get( ) );
	}
	if ( d == null )
	{
		c.clear( );
		c.set( 1970, 0, 1 );
	}
	else
	{
		c.setTime( d );
	}
	return c;
}
 
Example 3
Source File: DeadlineController.java    From olat with Apache License 2.0 6 votes vote down vote up
@Override
protected void initForm(final FormItemContainer formLayout, final Controller listener, final UserRequest ureq) {
    setFormTitle("map.deadline.change.title");
    setFormDescription("map.deadline.change.description");

    deadlineChooser = uifactory.addDateChooser("map.deadline", "", formLayout);
    if (map.getDeadLine() == null) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, 7);
        deadlineChooser.setDate(cal.getTime());
    } else {
        deadlineChooser.setDate(map.getDeadLine());
    }
    deadlineChooser.setValidDateCheck("map.deadline.invalid");

    final FormLayoutContainer buttonLayout = FormLayoutContainer.createButtonLayout("ok-cancel", getTranslator());
    buttonLayout.setRootForm(mainForm);
    formLayout.add(buttonLayout);
    uifactory.addFormSubmitButton("ok", buttonLayout);
    uifactory.addFormCancelButton("cancel", buttonLayout, ureq, getWindowControl());
}
 
Example 4
Source File: DateFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a DateFormat with the given time and/or date style in the given
 * locale.
 * @param dateStyle a value from 0 to 3 indicating the time format,
 * or -1 to indicate no date
 * @param timeStyle a value from 0 to 3 indicating the time format,
 * or -1 to indicate no time
 * @param loc the locale for the format
 * @param cal the calendar to be used, or null
 */
private static DateFormat get(int dateStyle, int timeStyle, ULocale loc, Calendar cal) {
    if((timeStyle != DateFormat.NONE && (timeStyle & RELATIVE)>0) ||
       (dateStyle != DateFormat.NONE && (dateStyle & RELATIVE)>0)) {
        RelativeDateFormat r = new RelativeDateFormat(timeStyle, dateStyle /* offset? */, loc, cal);
        return r;
    }

    if (timeStyle < DateFormat.NONE || timeStyle > DateFormat.SHORT) {
        throw new IllegalArgumentException("Illegal time style " + timeStyle);
    }
    if (dateStyle < DateFormat.NONE || dateStyle > DateFormat.SHORT) {
        throw new IllegalArgumentException("Illegal date style " + dateStyle);
    }

    if (cal == null) {
        cal = Calendar.getInstance(loc);
    }

    try {
        DateFormat result = cal.getDateTimeFormat(dateStyle, timeStyle, loc);
        result.setLocale(cal.getLocale(ULocale.VALID_LOCALE),
             cal.getLocale(ULocale.ACTUAL_LOCALE));
        return result;
    } catch (MissingResourceException e) {
        ///CLOVER:OFF
        // coverage requires separate run with no data, so skip
        return new SimpleDateFormat("M/d/yy h:mm a");
        ///CLOVER:ON
    }
}
 
Example 5
Source File: TimeUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return Time instance of current time point
 */
public TimePoint getTimePoint( )
{
	Calendar calendar = Calendar.getInstance( );
	calendar.setTimeInMillis( System.currentTimeMillis( ) );
	return new TimePoint( calendar );
}
 
Example 6
Source File: InvitationCleanupJob.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
*/
  @Override
  public void executeWithDB(final JobExecutionContext context) {
      try {
          log.info("Starting invitation clean up job");
          EPPolicyManager policyManager = (EPPolicyManager) CoreSpringFactory.getBean(EPPolicyManager.class);
          final Calendar cal = Calendar.getInstance();
          cal.add(Calendar.HOUR, -6);
          policyManager.cleanUpInvitations(cal.getTime());
      } catch (final Exception e) {
          // ups, something went completely wrong! We log this but continue next time
          log.error("Error while cleaning up invitation", e);
      }
      // db closed by JobWithDB class
  }
 
Example 7
Source File: TimeUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return string format of current time
 * 
 * @return current time
 */
public String getTime()
{
	Calendar calendar = Calendar.getInstance( );
	calendar.setTimeInMillis( System.currentTimeMillis( ) );
	return getTimeStr( calendar );
}
 
Example 8
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 9
Source File: UtilDateTime.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/** Returns timestamp without seconds and milliseconds (SCIPIO). */
public static Timestamp getMinuteBasedTimestamp(Timestamp stamp) {
    if (stamp == null) {
        return null;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(stamp.getTime());
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return new Timestamp(cal.getTimeInMillis());
}
 
Example 10
Source File: TimeDimensionUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
* 
* @param d
* @return
 */
private static Calendar getCalendar( Date d )
{
 Calendar c = null;
 if( timeZone == null )
  c = Calendar.getInstance( );
 else
  c = Calendar.getInstance( timeZone );
 c.setTime( d );
 return c;
}
 
Example 11
Source File: DataSetIterator.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
DateTimeAttributeProcessor( String timeType, ULocale locale, TimeZone zone )
{
	this.timeType = timeType;
	this.calendar = Calendar.getInstance( locale );
	if( zone!= null )
		this.calendar.setTimeZone( zone );
}
 
Example 12
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 13
Source File: GroupOnRowTest.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
@Before
   public void groupOnRowSetUp() throws Exception
{

	calendar = Calendar.getInstance( );
	calendar.clear( );
}
 
Example 14
Source File: ExpressionUiHelper.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/** Returns the last day of the week for the specified locale.
 * @param locale
 * @return The last day of the week for the specified locale
 */
public static int getLastDayOfWeek(Locale locale) {
    Calendar tempCal = Calendar.getInstance(locale);
    tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek());
    tempCal.roll(Calendar.DAY_OF_WEEK, -1);
    return tempCal.get(Calendar.DAY_OF_WEEK);
}
 
Example 15
Source File: RelativeDateFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * initializes fCalendar from parameters.  Returns fCalendar as a convenience.
 * @param zone  Zone to be adopted, or NULL for TimeZone::createDefault().
 * @param locale Locale of the calendar
 * @param status Error code
 * @return the newly constructed fCalendar
 */
private Calendar initializeCalendar(TimeZone zone, ULocale locale) {
    if (calendar == null) {
        if(zone == null) {
            calendar = Calendar.getInstance(locale);
        } else {
            calendar = Calendar.getInstance(zone, locale);
        }
    }
    return calendar;
}
 
Example 16
Source File: InfoDisplayController.java    From olat with Apache License 2.0 5 votes vote down vote up
public InfoDisplayController(final UserRequest ureq, final WindowControl wControl, final ModuleConfiguration config, final InfoSecurityCallback secCallback,
          final OLATResourceable ores, final String resSubPath, final String businessPath) {
      super(ureq, wControl, "display");
      this.secCallback = secCallback;
      this.ores = ores;
      this.resSubPath = resSubPath;
      this.businessPath = businessPath;

      infoMessageManager = (InfoMessageFrontendManager) CoreSpringFactory.getBean(InfoMessageFrontendManager.class);
      maxResults = maxResultsConfig = getConfigValue(config, InfoCourseNodeConfiguration.CONFIG_LENGTH, 10);
      duration = getConfigValue(config, InfoCourseNodeConfiguration.CONFIG_DURATION, 90);

      if (duration > 0) {
          final Calendar cal = Calendar.getInstance();
          cal.setTime(new Date());
          cal.add(Calendar.DATE, -duration);
          after = afterConfig = cal.getTime();
      }

      initForm(ureq);

// OLAT-6302 when a specific message is shown display the page that
// contains the message. Jump in e.g. from portlet
ContextEntry ce = wControl.getBusinessControl().popLauncherContextEntry();
if (ce != null) { // a context path is left for me
	OLATResourceable businessPathResource = ce.getOLATResourceable();
	String typeName = businessPathResource.getResourceableTypeName();
	if ("InfoMessage".equals(typeName)) {
		Long messageId = businessPathResource.getResourceableId();
		if (messageId != null && messageId.longValue() > 0) {
			// currently no pageing is implemented, just page with all entries
			maxResults = -1;
			after = null;
		}
	}
}

// now load with configuration
      loadMessages();
  }
 
Example 17
Source File: TemporalExpressions.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public Set<Integer> getHourRangeAsSet() {
    Set<Integer> rangeSet = new TreeSet<>();
    if (this.start == this.end) {
        rangeSet.add(this.start);
    } else {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, this.start);
        while (cal.get(Calendar.HOUR_OF_DAY) != this.end) {
            rangeSet.add(cal.get(Calendar.HOUR_OF_DAY));
            cal.add(Calendar.HOUR_OF_DAY, 1);
        }
    }
    return rangeSet;
}
 
Example 18
Source File: AggregationTest.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
@Test
   public void test11( ) throws Exception
{
	QueryDefinition query = newReportQuery( );
	
	Calendar c = Calendar.getInstance( );
	c.clear( );
	// 3 grouping levels: CITY, STORE, SALE_DATE(by month)
	GroupDefinition g1 = new GroupDefinition( "G1" );
	g1.setKeyExpression( "row.e1" );
	
	SortDefinition sortDefn = new SortDefinition();
	sortDefn.setExpression( "row.e5" );
	sortDefn.setSortDirection( ISortDefinition.SORT_DESC );
	g1.addSort( sortDefn );
	FilterDefinition filter = new FilterDefinition( new ScriptExpression( "row.e7 > 100" ) );
	g1.addFilter( filter );
	
	query.addGroup( g1 );

	GroupDefinition g2 = new GroupDefinition( "G2" );
	g2.setKeyExpression( "row.e2" );
	
	query.addGroup( g2 );

	GroupDefinition g3 = new GroupDefinition( "G3" );
	g3.setKeyExpression( "row.e3" );
	g3.setInterval( GroupDefinition.MONTH_INTERVAL );
	g3.setIntervalRange( 1 );
	
	c.set( 2004, 9, 1 );
	g3.setIntervalStart( c.getTime( ) );
	query.addGroup( g3 );
	
	SortDefinition sort = new SortDefinition( );
	sort.setExpression( "row.e3" );
	sort.setSortDirection( ISortDefinition.SORT_ASC );
	query.addSort( sort );
	
	query.addBinding( new Binding( "e1", new ScriptExpression( "dataSetRow.CITY" ) ) );
	query.addBinding( new Binding( "e2", new ScriptExpression( "dataSetRow.STORE" ) ) );
	query.addBinding( new Binding( "e3", new ScriptExpression( "dataSetRow.SALE_DATE" ) ) );
	query.addBinding( new Binding( "e4", new ScriptExpression( "dataSetRow.SKU" ) ) );
	query.addBinding( new Binding( "e10", new ScriptExpression( "dataSetRow.PRICE" ) ) );
	query.addBinding( new Binding( "e11", new ScriptExpression( "dataSetRow.QUANTITY" ) ) );
	
	// Aggregate: count at city level
	IBinding e5 = new Binding( "e5" );
	e5.setAggrFunction( "COUNT" );
	e5.addAggregateOn( "G1" );
	query.addBinding( e5 );

	// Aggregate: count at city level but added to Store group
	IBinding e6 = new Binding( "e6");
	e6.setAggrFunction( "COUNT" );
	e6.addAggregateOn( "G1" );
	query.addBinding( e6 );

	// Aggregate: day total sales
	IBinding e7 = new Binding( "e7", new ScriptExpression("dataSetRow.PRICE*dataSetRow.QUANTITY"));
	e7.setAggrFunction( "SUM" );
	e7.addAggregateOn( "G3" );
	query.addBinding( e7 );
	
	IBinding e81 = new Binding( "e81", new ScriptExpression("dataSetRow.PRICE*dataSetRow.QUANTITY"));
	e81.setAggrFunction( "SUM" );
	query.addBinding( e81 );
	
	// Aggregate: Percent of grand total
	IBinding e8 = new Binding( "e8", new ScriptExpression( "dataSetRow.PRICE * dataSetRow.QUANTITY / row.e81" ));
	query.addBinding( e8 );

	// Aggregate: a moving ave with a filtering condition
	IBinding e9 = new Binding( "e9", new ScriptExpression( "dataSetRow.PRICE"));
	e9.setAggrFunction( "MOVINGAVE" );
	e9.setFilter( new ScriptExpression ("dataSetRow.QUANTITY>1")  );
	e9.addArgument( new ScriptExpression( "3") );
	query.addBinding( e9 );
	String[] exprs = new String[]{
			"e1", "e2", "e3", "e4", "e10", "e11", "e5", "e6", "e7", "e8", "e9"
	};

	outputQueryResult( executeQuery( query ), exprs );
	checkOutputFile();
}
 
Example 19
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 20
Source File: AggregationTest.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
@Test
   public void test6( ) throws Exception
{
	QueryDefinition query = newReportQuery( );
	
	Calendar c = Calendar.getInstance( );
	c.clear( );
	// 3 grouping levels: CITY, STORE, SALE_DATE(by month)
	GroupDefinition g1 = new GroupDefinition( "G1" );
	g1.setKeyExpression( "row.e1" );
	query.addGroup( g1 );

	GroupDefinition g2 = new GroupDefinition( "G2" );
	g2.setKeyExpression( "row.e2" );
	
	query.addGroup( g2 );

	GroupDefinition g3 = new GroupDefinition( "G3" );
	g3.setKeyExpression( "row.e3" );
	g3.setInterval( GroupDefinition.MONTH_INTERVAL );
	g3.setIntervalRange( 1 );
	
	c.set( 2004, 9, 1 );
	g3.setIntervalStart( c.getTime( ) );
	query.addGroup( g3 );
	
	SortDefinition sort = new SortDefinition( );
	sort.setExpression( "row.e3" );
	sort.setSortDirection( ISortDefinition.SORT_ASC );
	query.addSort( sort );
	
	query.addBinding( new Binding( "e1", new ScriptExpression( "dataSetRow.CITY" ) ) );
	query.addBinding( new Binding( "e2", new ScriptExpression( "dataSetRow.STORE" ) ) );
	query.addBinding( new Binding( "e3", new ScriptExpression( "dataSetRow.SALE_DATE" ) ) );
	query.addBinding( new Binding( "e4", new ScriptExpression( "dataSetRow.SKU" ) ) );
	query.addBinding( new Binding( "e10", new ScriptExpression( "dataSetRow.PRICE" ) ) );
	query.addBinding( new Binding( "e11", new ScriptExpression( "dataSetRow.QUANTITY" ) ) );
	
	// Aggregate: count at city level
	IBinding e5 = new Binding( "e5" );
	e5.setAggrFunction( "COUNT" );
	e5.addAggregateOn( "G1" );
	query.addBinding( e5 );

	// Aggregate: count at city level but added to Store group
	IBinding e6 = new Binding( "e6");
	e6.setAggrFunction( "COUNT" );
	e6.addAggregateOn( "G1" );
	query.addBinding( e6 );

	// Aggregate: day total sales
	IBinding e7 = new Binding( "e7", new ScriptExpression("dataSetRow.PRICE*dataSetRow.QUANTITY"));
	e7.setAggrFunction( "SUM" );
	e7.addAggregateOn( "G3" );
	query.addBinding( e7 );
	
	FilterDefinition f1 = new FilterDefinition( new ConditionalExpression( "row[\"e7\"]" , IConditionalExpression.OP_TOP_N, "1" ) );
	query.addFilter( f1 );
	
	FilterDefinition f2 = new FilterDefinition( new ScriptExpression( "row[\"e10\"] < 200" ) );
	query.addFilter( f2 );
	
	IBinding e81 = new Binding( "e81", new ScriptExpression("dataSetRow.PRICE*dataSetRow.QUANTITY"));
	e81.setAggrFunction( "SUM" );
	query.addBinding( e81 );
	
	// Aggregate: Percent of grand total
	IBinding e8 = new Binding( "e8", new ScriptExpression( "dataSetRow.PRICE * dataSetRow.QUANTITY / row.e81" ));
	query.addBinding( e8 );

	// Aggregate: a moving ave with a filtering condition
	IBinding e9 = new Binding( "e9", new ScriptExpression( "dataSetRow.PRICE"));
	e9.setAggrFunction( "MOVINGAVE" );
	e9.setFilter( new ScriptExpression ("dataSetRow.QUANTITY>1")  );
	e9.addArgument( new ScriptExpression( "3") );
	query.addBinding( e9 );
	String[] exprs = new String[]{
			"e1", "e2", "e3", "e4", "e10", "e11", "e5", "e6", "e7", "e8", "e9"
	};

	outputQueryResult( executeQuery( query ), exprs );
	checkOutputFile();
}