List of usage examples for java.util Calendar clone
@Override
public Object clone()
From source file:info.raack.appliancedetection.evaluation.model.appliance.SimulatedAppliance.java
public final int getPowerAtTimestep(Calendar cal, int timestep) { int powerDraw = 0; // am I on?/*from ww w . ja v a2 s . c o m*/ if (!on) { // should I turn on? if (cycleSeconds == cycleSecondsElapsed) { cycleSecondsElapsed = 0; int newCycleSeconds = 0; // check the level of appliance on concurrency if (simulation.canTurnOn()) { // train if we need to and if we can if (trainingSessionsRemaining > 0 && simulation.canTrain()) { trainingSessionsRemaining--; inTraining = true; currentOnOffPair = new ApplianceStateTransition[2]; currentOnOffPair[0] = new GenericStateTransition(-1, null, 0, true, getCurrentSimulatedTime(timestep) - 5 * 1000); } // the simulator says that we can turn on on = true; // determine how long the appliance will be on newCycleSeconds = (int) (Math.random() * (float) maxOnPeriod()); logger.debug("at " + cal.getTime() + " " + getName() + " turning on for " + newCycleSeconds + " seconds"); addStateTransition(timestep, on); } else { // create another off cycle newCycleSeconds = (int) (Math.random() * (float) maxOffPeriod()); logger.debug("at " + cal.getTime() + " " + getName() + " cannot turn on yet so sleeping " + newCycleSeconds + " longer"); } cycleSeconds = newCycleSeconds; } else { cycleSecondsElapsed++; } } if (on) { // how much power am I using? powerDraw = powerDrawAtOnCycleTimestep(cycleSeconds, cycleSecondsElapsed, timestep); // add some noise of up to 4w in either direction try { powerDraw += (int) (noiseDistribution.sample()); } catch (MathException e) { // don't care if noise is problematic } // don't go below 0 powerDraw = Math.max(powerDraw, 0); // should I turn off? if (cycleSeconds == cycleSecondsElapsed + 1) { cycleSecondsElapsed = 0; on = false; if (isTraining()) { inTraining = false; currentOnOffPair[1] = new GenericStateTransition(-1, null, 0, false, getCurrentSimulatedTime(timestep) + 5 * 1000); allOnOffPairs.add(currentOnOffPair); } int newCycleSeconds = (int) (Math.random() * (float) maxOffPeriod()); logger.debug("at " + cal.getTime() + " " + getName() + " turning off for " + newCycleSeconds + " + seconds"); cycleSeconds = newCycleSeconds; addStateTransition(timestep, false); } else { cycleSecondsElapsed++; } } // calcuate watt-seconds = watts * 1 second energyConsumedDuringTimestep += (double) powerDraw; // if the timestep duration has passed (enough for a new timestamp) - create one if ((timestep + 1) % TIMESTEP_DURATION == 0) { if (energyConsumedDuringTimestep > 0) { EnergyTimestep currentTimestep = new EnergyTimestep(); currentTimestep.setEnergyConsumed(energyConsumedDuringTimestep); Calendar startTime = new GregorianCalendar(); startTime.setTime(simulation.getStartTime()); startTime.add(Calendar.SECOND, timestep + 1 - TIMESTEP_DURATION); currentTimestep.setStartTime(startTime.getTime()); Calendar endTime = (Calendar) startTime.clone(); endTime.add(Calendar.SECOND, TIMESTEP_DURATION - 1); currentTimestep.setEndTime(endTime.getTime()); energyTimesteps.add(currentTimestep); } energyConsumedDuringTimestep = 0; } return powerDraw; }
From source file:org.jfree.chart.demo.SegmentedHighLowChartDemo.java
/** * A demonstration application showing a high-low-open-close chart using a * segmented or non-segmented axis./*from w ww .j a v a2 s .c o m*/ * * @param title the frame title. * @param useSegmentedAxis use a segmented axis for this demo? * @param timelineType Type of timeline to use: 1=Monday through Friday, 2=Intraday */ public SegmentedHighLowChartDemo(final String title, final boolean useSegmentedAxis, final int timelineType) { super(title); System.out.println("\nMaking SegmentedHighLowChartDemo(" + title + ")"); // create a Calendar object with today's date at midnight final Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); // create a timeline for the demo SegmentedTimeline timeline = null; switch (timelineType) { case 1: timeline = SegmentedTimeline.newMondayThroughFridayTimeline(); break; case 2: timeline = SegmentedTimeline.newFifteenMinuteTimeline(); final Calendar cal2 = (Calendar) cal.clone(); cal2.add(Calendar.YEAR, 1); // add 1 year of baseTimeline's excluded segments (Saturdays and Sundays) as // exceptions of the intraday timeline timeline.addBaseTimelineExclusions(cal.getTime().getTime(), cal2.getTime().getTime()); break; default: System.out.println("Invalid timelineType."); System.exit(1); } // create a data set that has data for trading days (Monday through Friday). final DefaultHighLowDataset dataset = DemoDatasetFactory.createSegmentedHighLowDataset(timeline, cal.getTime()); final JFreeChart chart; if (useSegmentedAxis) { chart = ChartFactory.createHighLowChart(title, "Time", "Value", dataset, timeline, true); } else { chart = ChartFactory.createHighLowChart(title, "Time", "Value", dataset, true); } final DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis(); axis.setAutoRange(true); final TickUnits units = new TickUnits(); units.add(new DateTickUnit(DateTickUnit.DAY, 1, DateTickUnit.HOUR, 1, new SimpleDateFormat("d-MMM"))); units.add(new DateTickUnit(DateTickUnit.DAY, 2, DateTickUnit.HOUR, 1, new SimpleDateFormat("d-MMM"))); units.add(new DateTickUnit(DateTickUnit.DAY, 7, DateTickUnit.DAY, 1, new SimpleDateFormat("d-MMM"))); units.add(new DateTickUnit(DateTickUnit.DAY, 15, DateTickUnit.DAY, 1, new SimpleDateFormat("d-MMM"))); units.add(new DateTickUnit(DateTickUnit.DAY, 30, DateTickUnit.DAY, 1, new SimpleDateFormat("d-MMM"))); axis.setStandardTickUnits(units); final NumberAxis vaxis = (NumberAxis) chart.getXYPlot().getRangeAxis(); vaxis.setAutoRangeIncludesZero(false); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); setContentPane(chartPanel); }
From source file:net.rrm.ehour.ui.timesheet.panel.TimesheetRowList.java
private void addInputCells(ListItem<TimesheetRow> item, final TimesheetRow row) { Calendar currentDate = (Calendar) row.getFirstDayOfWeekDate().clone(); ProjectAssignment assignment = row.getProjectAssignment(); final DateRange range = new DateRange(assignment.getDateStart(), assignment.getDateEnd()); final List<Calendar> dates = Lists.newArrayList(); for (int i = 1; i <= 7; i++, currentDate.add(Calendar.DAY_OF_YEAR, 1)) { dates.add((Calendar) currentDate.clone()); }/*from w w w. java2s . co m*/ item.add(new ListView<Calendar>("days", dates) { @Override protected void populateItem(ListItem<Calendar> item) { Calendar currentDate = item.getModelObject(); int index = currentDate.get(Calendar.DAY_OF_WEEK) - 1; TimesheetCell timesheetCell = row.getTimesheetCells()[index]; DayStatus dayStatus = determineDayStatus(timesheetCell, currentDate, range); Component day; switch (dayStatus) { case HIDDEN: default: day = createEmptyTimesheetEntry(DAY_ID); item.add(new WebMarkupContainer(DAY_OPTIONS_ID)); break; case OPEN: day = createInputTimesheetEntry(DAY_ID, row, index); item.add(renderOptions(DAY_OPTIONS_ID, timesheetCell, dayStatus)); break; case LOCKED: day = createLockedTimesheetEntry(DAY_ID, row, index); item.add(renderOptions(DAY_OPTIONS_ID, timesheetCell, dayStatus)); item.add(AttributeModifier.append("class", "locked")); break; } String cssClass = "weekday"; if (dayStatus == DayStatus.LOCKED) { cssClass += " lockedCell"; } else if (item.getIndex() == 0) { cssClass = "sunday"; } else if (item.getIndex() == 6) { cssClass = "saturday"; } day.add(AttributeModifier.replace("class", cssClass)); item.add(day); } }); }
From source file:org.nuxeo.cm.demo.UpdateDemoData.java
private Calendar _buildDate(Calendar inDate, int inDays) { Calendar d = (Calendar) inDate.clone(); d.add(Calendar.DATE, inDays); if (d.after(_today)) { d = (Calendar) _today.clone(); }//from ww w . jav a2s.com return d; }
From source file:nl.strohalm.cyclos.services.accounts.cards.CardServiceImpl.java
private Card buildNewCard(final Member member) { final Calendar now = Calendar.getInstance(); final Card newCard = new Card(); final CardType cardType = member.getMemberGroup().getCardType(); newCard.setCardType(cardType);// ww w . j a v a 2 s . co m newCard.setOwner(member); newCard.setCreationDate(now); newCard.setStatus(Status.PENDING); final Calendar expirationDate = (Calendar) now.clone(); expirationDate.add(cardType.getDefaultExpiration().getField().getValue(), cardType.getDefaultExpiration().getNumber()); if (cardType.isIgnoreDayInExpirationDate()) { expirationDate.set(Calendar.DAY_OF_MONTH, expirationDate.getActualMaximum(Calendar.DAY_OF_MONTH)); } newCard.setExpirationDate(expirationDate); newCard.setCardNumber(buildCardNumber(cardType.getCardFormatNumber())); if (cardType.getCardSecurityCode() == CardType.CardSecurityCode.AUTOMATIC) { newCard.setCardSecurityCode(buildCardSecurityCode(cardType.getCardSecurityCodeLength().getMax())); } return newCard; }
From source file:TimeSpan.java
/** * Returns {@code true} if the end date occurs after the start date during * the period of time represented by this time span. *///from www . jav a 2s .co m public boolean insideRange(Calendar startDate, Calendar endDate) { // make a copy of the start time so that it is safe to modify it without // affecting the input parameter Calendar mutableStartDate = (Calendar) (startDate.clone()); return isInRange(mutableStartDate, endDate); }
From source file:org.cds06.speleograph.data.fileio.ReefnetFileReader.java
/** * Read an entry from a Reefnet File.//from w ww . j a v a2 s.com * * @param line The line extracted from the file (length must be 12 or 13) * @param pressureSeries The series where add pressure data * @param temperatureSeries The series where add temperature data * @param seriesId The ReefNet's Series ID * @param calendar The calendar which contains the start date of the current series. * @return The modified ReefNet's Series ID. * @throws FileReadingError When can not parse the date. * @see #readDate(String[], java.util.Calendar) * @see #readFile(java.io.File) */ private String readReefnetEntry(String[] line, Series pressureSeries, Series temperatureSeries, String seriesId, Calendar calendar) throws FileReadingError { double temperature = 0; if (line.length == 12) { temperature = Double.parseDouble(line[11]) - 273.15; } else if (line.length == 13) { temperature = Double.parseDouble(line[11] + '.' + line[12]) - 273.15; } int pressure = Integer.parseInt(line[10]); if (!seriesId.equals(line[2])) { seriesId = readDate(line, calendar); } Calendar clone = (Calendar) calendar.clone(); clone.add(Calendar.SECOND, Integer.parseInt(line[9])); temperatureSeries.add(new Item(temperatureSeries, clone.getTime(), temperature)); pressureSeries.add(new Item(pressureSeries, clone.getTime(), pressure)); return seriesId; }
From source file:gbc.jtimecalc.AbstractTimeDifferenceCalculatorTest.java
public void shouldReturn9Months0Days0Hours0Minutes0seconds() { Calendar end = prepareCalendar(1980, Calendar.JANUARY, 0, 0, 0, 0, 0); // 1.01.1980 00:00:00.0 setEndTime(end.getTimeInMillis());// w w w .ja va2s.c om Calendar start = (Calendar) end.clone(); start.add(Calendar.MONTH, -9); setStartTime(start.getTimeInMillis()); expectedValue = messages.get("9Months2Days"); }
From source file:gbc.jtimecalc.AbstractTimeDifferenceCalculatorTest.java
public void shouldReturn1BusinessMonth20BusinessDaysWithoutTailingZeroes() { Calendar end = prepareCalendar(2011, Calendar.JULY, 31, 0, 0, 0, 0); // 31.07.2011 00:00:00.0 setEndTime(end.getTimeInMillis());/*from ww w. j av a2s .com*/ Calendar start = (Calendar) end.clone(); start.set(Calendar.MONTH, Calendar.JUNE); start.set(Calendar.DAY_OF_MONTH, 10); setStartTime(start.getTimeInMillis()); expectedValue = messages.get("1BusinessMonth20BusinessDays"); }
From source file:gbc.jtimecalc.AbstractTimeDifferenceCalculatorTest.java
public void shouldReturn30DaysWithoutTailingZeroes() { Calendar end = prepareCalendar(2006, Calendar.FEBRUARY, 1, 1, 0, 0, 0); // 1.02.2006 01:00:00.0 setEndTime(end.getTimeInMillis());//from w ww . ja v a 2 s. c om Calendar start = (Calendar) end.clone(); start.add(Calendar.DATE, -30); setStartTime(start.getTimeInMillis()); // 2.01.2006 00:00:00.0 expectedValue = messages.get("30DaysWithoutTailingZeroes"); }