Example usage for java.util Calendar clone

List of usage examples for java.util Calendar clone

Introduction

In this page you can find the example usage for java.util Calendar clone.

Prototype

@Override
public Object clone() 

Source Link

Document

Creates and returns a copy of this object.

Usage

From source file:org.apache.cocoon.generation.CalendarGenerator.java

/**
 * Generate XML data.//from  w ww .  j  a v a 2s  .  c  o  m
 *
 * @throws  SAXException if an error occurs while outputting the document
 */
public void generate() throws SAXException, ProcessingException {
    Calendar start = Calendar.getInstance(locale);
    start.clear();
    start.set(Calendar.YEAR, this.year);
    start.set(Calendar.MONTH, this.month);
    start.set(Calendar.DAY_OF_MONTH, 1);
    Calendar end = (Calendar) start.clone();
    end.add(Calendar.MONTH, 1);

    // Determine previous and next months
    Calendar prevMonth = (Calendar) start.clone();
    prevMonth.add(Calendar.MONTH, -1);

    this.contentHandler.startDocument();
    this.contentHandler.startPrefixMapping(PREFIX, URI);
    attributes.clear();
    attributes.addAttribute("", YEAR_ATTR_NAME, YEAR_ATTR_NAME, "CDATA", String.valueOf(year));
    attributes.addAttribute("", MONTH_ATTR_NAME, MONTH_ATTR_NAME, "CDATA",
            monthFormatter.format(start.getTime()));

    // Add previous and next month
    attributes.addAttribute("", PREV_YEAR_ATTR_NAME, PREV_YEAR_ATTR_NAME, "CDATA",
            String.valueOf(prevMonth.get(Calendar.YEAR)));
    attributes.addAttribute("", PREV_MONTH_ATTR_NAME, PREV_MONTH_ATTR_NAME, "CDATA",
            monthNumberFormatter.format(prevMonth.get(Calendar.MONTH) + 1));
    attributes.addAttribute("", NEXT_YEAR_ATTR_NAME, NEXT_YEAR_ATTR_NAME, "CDATA",
            String.valueOf(end.get(Calendar.YEAR)));
    attributes.addAttribute("", NEXT_MONTH_ATTR_NAME, NEXT_MONTH_ATTR_NAME, "CDATA",
            monthNumberFormatter.format(end.get(Calendar.MONTH) + 1));

    this.contentHandler.startElement(URI, CALENDAR_NODE_NAME, PREFIX + ':' + CALENDAR_NODE_NAME, attributes);
    int weekNo = start.get(Calendar.WEEK_OF_MONTH);
    int firstDay = start.getFirstDayOfWeek();
    if (start.get(Calendar.DAY_OF_WEEK) != firstDay) {
        attributes.clear();
        attributes.addAttribute("", NUMBER_ATTR_NAME, NUMBER_ATTR_NAME, "CDATA", String.valueOf(weekNo));
        this.contentHandler.startElement(URI, WEEK_NODE_NAME, PREFIX + ':' + WEEK_NODE_NAME, attributes);
        if (padWeeks) {
            Calendar previous = (Calendar) start.clone();
            while (previous.get(Calendar.DAY_OF_WEEK) != firstDay) {
                previous.add(Calendar.DAY_OF_MONTH, -1);
            }
            while (previous.before(start)) {
                attributes.clear();
                attributes.addAttribute("", NUMBER_ATTR_NAME, NUMBER_ATTR_NAME, "CDATA",
                        String.valueOf(previous.get(Calendar.DAY_OF_MONTH)));
                attributes.addAttribute("", WEEKDAY_ATTR_NAME, WEEKDAY_ATTR_NAME, "CDATA",
                        weekdays[previous.get(Calendar.DAY_OF_WEEK)]);
                attributes.addAttribute("", DATE_ATTR_NAME, DATE_ATTR_NAME, "CDATA",
                        dateFormatter.format(previous.getTime()));
                this.contentHandler.startElement(URI, DAY_NODE_NAME, PREFIX + ':' + DAY_NODE_NAME, attributes);
                addContent(previous, locale);
                this.contentHandler.endElement(URI, DAY_NODE_NAME, PREFIX + ':' + DAY_NODE_NAME);
                previous.add(Calendar.DAY_OF_MONTH, 1);
            }
        }
    }
    while (start.before(end)) {
        if (start.get(Calendar.DAY_OF_WEEK) == firstDay) {
            weekNo = start.get(Calendar.WEEK_OF_MONTH);
            attributes.clear();
            attributes.addAttribute("", NUMBER_ATTR_NAME, NUMBER_ATTR_NAME, "CDATA", String.valueOf(weekNo));
            this.contentHandler.startElement(URI, WEEK_NODE_NAME, PREFIX + ':' + WEEK_NODE_NAME, attributes);
        }
        attributes.clear();
        attributes.addAttribute("", NUMBER_ATTR_NAME, NUMBER_ATTR_NAME, "CDATA",
                String.valueOf(start.get(Calendar.DAY_OF_MONTH)));
        attributes.addAttribute("", WEEKDAY_ATTR_NAME, WEEKDAY_ATTR_NAME, "CDATA",
                weekdays[start.get(Calendar.DAY_OF_WEEK)]);
        attributes.addAttribute("", DATE_ATTR_NAME, DATE_ATTR_NAME, "CDATA",
                dateFormatter.format(start.getTime()));
        this.contentHandler.startElement(URI, DAY_NODE_NAME, PREFIX + ':' + DAY_NODE_NAME, attributes);
        addContent(start, locale);
        this.contentHandler.endElement(URI, DAY_NODE_NAME, PREFIX + ':' + DAY_NODE_NAME);
        start.add(Calendar.DAY_OF_MONTH, 1);
        if (start.get(Calendar.DAY_OF_WEEK) == firstDay || (!padWeeks && !start.before(end))) {
            this.contentHandler.endElement(URI, WEEK_NODE_NAME, PREFIX + ':' + WEEK_NODE_NAME);
        }
    }

    if (padWeeks) {
        while (firstDay != end.get(Calendar.DAY_OF_WEEK)) {
            attributes.clear();
            attributes.addAttribute("", NUMBER_ATTR_NAME, NUMBER_ATTR_NAME, "CDATA",
                    String.valueOf(end.get(Calendar.DAY_OF_MONTH)));
            attributes.addAttribute("", WEEKDAY_ATTR_NAME, WEEKDAY_ATTR_NAME, "CDATA",
                    weekdays[end.get(Calendar.DAY_OF_WEEK)]);
            attributes.addAttribute("", DATE_ATTR_NAME, DATE_ATTR_NAME, "CDATA",
                    dateFormatter.format(end.getTime()));
            this.contentHandler.startElement(URI, DAY_NODE_NAME, PREFIX + ':' + DAY_NODE_NAME, attributes);
            addContent(end, locale);
            this.contentHandler.endElement(URI, DAY_NODE_NAME, PREFIX + ':' + DAY_NODE_NAME);
            end.add(Calendar.DAY_OF_MONTH, 1);
            if (firstDay == end.get(Calendar.DAY_OF_WEEK)) {
                this.contentHandler.endElement(URI, WEEK_NODE_NAME, PREFIX + ':' + WEEK_NODE_NAME);
            }
        }
    }
    this.contentHandler.endElement(URI, CALENDAR_NODE_NAME, PREFIX + ':' + CALENDAR_NODE_NAME);
    this.contentHandler.endPrefixMapping(PREFIX);
    this.contentHandler.endDocument();
}

From source file:com.example.ffmpegtest.HWRecorderActivity.java

public void startRecording() {
    try {//from   w ww .ja  va 2s. co m

        // delete existing files in outputPath

        if (outputPath == null) { // in case of 2 cam
            File folder = getBaseOutputFolder();
            initPaths(folder);
            hlsServer.setWwwroot(folder);
        }

        /*
        Log.d(TAG, "Cleaning existing files in " + outputPath);
        String[] files = outputPath.list();
        for (String eachFile : files) {
        File file = new File(outputPath, eachFile);
        Log.d(TAG, "Deleting " + file.getAbsolutePath());
        file.delete();
        }
        */

        liveRecorder = new LiveHLSRecorder(getApplicationContext(), settings, VIDEO_STREAMS_COUNT,
                RECORD_AUDIO_STREAM);
        liveRecorder.setExternalCallback(new HLSFileObserver.HLSCallback() {
            @Override
            public void onSegmentComplete(String path) {
                Calendar finished = GregorianCalendar.getInstance();
                Calendar started = (Calendar) finished.clone();
                started.add(Calendar.SECOND, -settings.getHlsSegmentLength());

                Log.i(CameraHttpServer.TAG, "segment added " + path);

                motorolaApiServer.getControlService().notifySegment(started.getTime(), finished.getTime(),
                        path);
            }

            @Override
            public void onManifestUpdated(String path) {

            }
        });

        if (VIDEO_STREAMS_COUNT == 1)
            liveRecorder.setStreamer(new TcpStreamer(SOCKET_PORT)); // streaming to socket (1 camera)
        else
            liveRecorder.setStreamer(new TsStreamer(SOCKET_PORT, VIDEO_STREAMS_COUNT, null)); // streaming to socket // (2 cameras)

        liveRecorder.startRecording(outputPath.getAbsolutePath());

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                button.setText("Stop Recording");
            }
        });

        recording = true;
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
}

From source file:org.jfree.data.time.Week.java

/**
 * Returns the first millisecond of the week, evaluated using the supplied
 * calendar (which determines the time zone).
 *
 * @param calendar  the calendar (<code>null</code> not permitted).
 *
 * @return The first millisecond of the week.
 *
 * @throws NullPointerException if <code>calendar</code> is
 *     <code>null</code>.//w  ww  .  ja v a  2  s. c o m
 */
@Override
public long getFirstMillisecond(Calendar calendar) {
    Calendar c = (Calendar) calendar.clone();
    c.clear();
    c.set(Calendar.YEAR, this.year);
    c.set(Calendar.WEEK_OF_YEAR, this.week);
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTimeInMillis();
}

From source file:org.jfree.data.time.Week.java

/**
 * Returns the last millisecond of the week, evaluated using the supplied
 * calendar (which determines the time zone).
 *
 * @param calendar  the calendar (<code>null</code> not permitted).
 *
 * @return The last millisecond of the week.
 *
 * @throws NullPointerException if <code>calendar</code> is
 *     <code>null</code>./*from   www  .  ja v  a2 s .  c o m*/
 */
@Override
public long getLastMillisecond(Calendar calendar) {
    Calendar c = (Calendar) calendar.clone();
    c.clear();
    c.set(Calendar.YEAR, this.year);
    c.set(Calendar.WEEK_OF_YEAR, this.week + 1);
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTimeInMillis() - 1;
}

From source file:br.com.transport.report.ManagerReportBean.java

/**
 * Retorna lista com os dias da semana//  w  w  w .j a  v a2s .  c o m
 * @return {@link List}< {@link Calendar} >
 */
private List<Calendar> createDayOfTheWeek() {

    Calendar firstDay = GregorianCalendar.getInstance();
    firstDay.setFirstDayOfWeek(Calendar.SUNDAY);
    firstDay.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    firstDay.set(Calendar.HOUR_OF_DAY, 0);
    firstDay.set(Calendar.MINUTE, 0);
    firstDay.set(Calendar.SECOND, 0);

    List<Calendar> daysOfTheWeek = new LinkedList<Calendar>();

    for (int count = 0; count < 7; count++) {

        Calendar temp = (Calendar) firstDay.clone();
        temp.roll(Calendar.DAY_OF_WEEK, count);
        temp.setTime(temp.getTime());
        daysOfTheWeek.add(temp);

    }

    return daysOfTheWeek;
}

From source file:org.betaconceptframework.astroboa.portal.utility.CalendarUtils.java

public Calendar[] getCurrentWeekPeriod(TimeZone zone, Locale locale) {
    Calendar weekStart = GregorianCalendar.getInstance(zone, locale);
    Calendar weekEnd;// w  ww.  j a va2 s. co  m
    // find Monday the first week day
    while (weekStart.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        weekStart.add(Calendar.DAY_OF_MONTH, -1);
    }

    // then to find SUNDAY we add 6 days
    weekEnd = (GregorianCalendar) weekStart.clone();
    weekEnd.add(Calendar.DAY_OF_MONTH, 6);

    setCalendarToStartOfDay(weekStart);
    setCalendarToEndOfDay(weekEnd);

    Calendar[] period = { weekStart, weekEnd };
    return period;
}

From source file:gbc.jtimecalc.AbstractTimeDifferenceCalculatorTest.java

public void shouldReturnOneHour() {
    Calendar end = prepareCalendar(2006, Calendar.FEBRUARY, 1, 11, 0, 0, 0);
    Calendar start = (Calendar) end.clone();
    start.add(Calendar.HOUR_OF_DAY, -1);
    setEndTime(end.getTimeInMillis());//  ww w  . j  a v  a 2  s . c o m
    setStartTime(start.getTimeInMillis());
    expectedValue = messages.get("1Hour");
}

From source file:org.eclipse.ecr.core.api.impl.DocumentModelImpl.java

@SuppressWarnings("unchecked")
public static Object cloneField(Field field, String key, Object value) {
    // key is unused
    Object clone;/*from  w w w . j a  v  a 2  s. co  m*/
    Type type = field.getType();
    if (type.isSimpleType()) {
        // CLONE TODO
        if (value instanceof Calendar) {
            Calendar newValue = (Calendar) value;
            clone = newValue.clone();
        } else {
            clone = value;
        }
    } else if (type.isListType()) {
        ListType ltype = (ListType) type;
        Field lfield = ltype.getField();
        Type ftype = lfield.getType();
        List<Object> list;
        if (value instanceof Object[]) { // these are stored as arrays
            list = Arrays.asList((Object[]) value);
        } else {
            list = (List<Object>) value;
        }
        if (ftype.isComplexType()) {
            List<Object> clonedList = new ArrayList<Object>(list.size());
            for (Object o : list) {
                clonedList.add(cloneField(lfield, null, o));
            }
            clone = clonedList;
        } else {
            Class<?> klass = JavaTypes.getClass(ftype);
            if (klass.isPrimitive()) {
                clone = PrimitiveArrays.toPrimitiveArray(list, klass);
            } else {
                clone = list.toArray((Object[]) Array.newInstance(klass, list.size()));
            }
        }
    } else {
        // complex type
        ComplexType ctype = (ComplexType) type;
        if (TypeConstants.isContentType(ctype)) { // if a blob
            Blob blob = (Blob) value; // TODO
            clone = blob;
        } else {
            // a map, regular complex type
            Map<String, Object> map = (Map<String, Object>) value;
            Map<String, Object> clonedMap = new HashMap<String, Object>();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                Object v = entry.getValue();
                String k = entry.getKey();
                if (v == null) {
                    continue;
                }
                clonedMap.put(k, cloneField(ctype.getField(k), k, v));
            }
            clone = clonedMap;
        }
    }
    return clone;
}

From source file:org.psystems.dicom.browser.server.stat.StatClientRequestsChartServlet.java

/**
 * Returns a sample dataset./* w  w w  . ja  v a 2  s. c om*/
 * 
 * @return The dataset.
 */
private CategoryDataset createDataset() {

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    Calendar calendarEnd = Calendar.getInstance();
    int tzoffset = calendarEnd.getTimeZone().getOffset(calendarEnd.getTimeInMillis());

    long time = calendarEnd.getTimeInMillis();
    time = time - (time % (60 * 60 * 24 * 1000)) - tzoffset;
    calendarEnd.setTimeInMillis(time);

    try {
        Connection connection = Util.getConnection("main", getServletContext());

        Calendar calendarBegin = (Calendar) calendarEnd.clone();
        calendarBegin.add(Calendar.DAY_OF_MONTH, -7);

        getMetrics(connection, " ?", "CLIENT_CONNECTIONS",
                calendarBegin.getTimeInMillis(), calendarEnd.getTimeInMillis(), dataset);

    } catch (SQLException e) {
        logger.error(e);
        e.printStackTrace();
    }
    return dataset;
}

From source file:org.psystems.dicom.browser.server.stat.StatDailyLoadChartServlet.java

/**
 * Returns a sample dataset./*from   w  w  w.  j ava2  s  .c  o m*/
 * 
 * @return The dataset.
 */
private CategoryDataset createDataset() {

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    Calendar calendarEnd = Calendar.getInstance();
    int tzoffset = calendarEnd.getTimeZone().getOffset(calendarEnd.getTimeInMillis());

    long time = calendarEnd.getTimeInMillis();
    time = time - (time % (60 * 60 * 24 * 1000)) - tzoffset;
    calendarEnd.setTimeInMillis(time);

    try {
        Connection connection = Util.getConnection("main", getServletContext());

        Calendar calendarBegin = (Calendar) calendarEnd.clone();
        calendarBegin.add(Calendar.DAY_OF_MONTH, -7);

        getMetrics(connection, "??? (DCM-)", "ALL_DCM_SIZE",
                calendarBegin.getTimeInMillis(), calendarEnd.getTimeInMillis(), dataset);

        getMetrics(connection, "? (JPG-)", "ALL_IMAGE_SIZE",
                calendarBegin.getTimeInMillis(), calendarEnd.getTimeInMillis(), dataset);

    } catch (SQLException e) {
        logger.error(e);
        e.printStackTrace();
    }
    return dataset;
}