Example usage for java.util GregorianCalendar setTime

List of usage examples for java.util GregorianCalendar setTime

Introduction

In this page you can find the example usage for java.util GregorianCalendar setTime.

Prototype

public final void setTime(Date date) 

Source Link

Document

Sets this Calendar's time with the given Date.

Usage

From source file:org.webical.ical.RecurrenceUtil.java

public static java.util.Date getLastOccurrenceDate(Event event) throws WebicalException {
    if (!(event != null && isRecurrent(event))) {
        throw new WebicalException("This event is not recurrent: " + event);
    }// ww w.  j a  v  a2s.  co  m

    Recurrence recurrence = getRecurrenceFromRecurrenceRuleSet(event.getrRule());
    if (recurrence != null) { // Check if the recurrence end date is set. Use this as last date value
        if (recurrence.getEndDay() != null) {
            return recurrence.getEndDay();
        }

        // Check for use of count to determine the end date
        if (recurrence.getCount() >= 1 && recurrence.getInterval() >= 1) {
            GregorianCalendar endDateCalendar = new GregorianCalendar();
            endDateCalendar.setTime(event.getDtStart());
            int amountToAdd = (recurrence.getCount() * recurrence.getInterval());
            endDateCalendar.add(getCalendarIdentifierForFrequency(recurrence.getFrequency()), amountToAdd);

            return endDateCalendar.getTime();
        }
    }
    return null;
}

From source file:org.odk.collect.android.utilities.WebUtils.java

private static void setOpenRosaHeaders(HttpRequest req) {
    req.setHeader(OPEN_ROSA_VERSION_HEADER, OPEN_ROSA_VERSION);
    GregorianCalendar g = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    g.setTime(new Date());
    req.setHeader(DATE_HEADER, DateFormat.format("E, dd MMM yyyy hh:mm:ss zz", g).toString());
}

From source file:org.webical.ical.RecurrenceUtil.java

public static java.util.Date getNextOccurrenceDate(Event event, java.util.Date occurrenceDate)
        throws WebicalException, ParseException {
    if (!(event != null && isRecurrent(event))) {
        throw new WebicalException("This event is not recurrent: " + event);
    }//from  w ww .  ja v  a 2 s  . c o m

    Recurrence recurrence = getRecurrenceFromRecurrenceRuleSet(event.getrRule());
    if (recurrence != null) { // Check for use of count to determine the end date
        if (recurrence.getCount() > 0 && recurrence.getInterval() > 0) {
            GregorianCalendar endDateCalendar = new GregorianCalendar();

            endDateCalendar.setTime(event.getDtStart());
            int amountToAdd = (recurrence.getCount() * recurrence.getInterval());
            endDateCalendar.add(getCalendarIdentifierForFrequency(recurrence.getFrequency()), amountToAdd);

            if (isApplicableOnDate(event, endDateCalendar.getTime())) {
                return endDateCalendar.getTime();
            }
            return null;
        }
    }
    return null;
}

From source file:com.kcs.core.utilities.Utility.java

public static XMLGregorianCalendar getXMLGregorianCalendarDate(Date date)
        throws DatatypeConfigurationException {
    if (Utility.isNotNull(date)) {
        GregorianCalendar arrgCtrDate = new GregorianCalendar();
        arrgCtrDate.setTime(date);
        DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
        XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendarDate(
                arrgCtrDate.get(Calendar.YEAR), arrgCtrDate.get(Calendar.MONTH) + 1,
                arrgCtrDate.get(Calendar.DAY_OF_MONTH), DatatypeConstants.FIELD_UNDEFINED);
        return xmlGregorianCalendar;
    }/*ww w  . j ava2 s .c o  m*/
    return null;
}

From source file:CalendarUtils.java

/**
 * See the other getCalendarIterator.  Works with a Date.
 *///from  w w  w  .j  ava 2 s.c o  m
public static Iterator getCalendarIterator(Date focus, int rangeStyle) {
    GregorianCalendar gval = new GregorianCalendar();
    gval.setTime(focus);
    return getCalendarIterator(gval, rangeStyle);
}

From source file:CalendarUtils.java

/**
 * See the other round method.  Works with a Date object.
 *//*w w  w  .j a  va 2s.co m*/
public static Date round(Date val, int field) {
    GregorianCalendar gval = new GregorianCalendar();
    gval.setTime(val);
    modify(gval, field, true);
    return gval.getTime();
}

From source file:CalendarUtils.java

/**
 * See the other trunc method.  Works with a Date.
 *///ww  w. java2 s  . co m
public static Date trunc(Date val, int field) {
    GregorianCalendar gval = new GregorianCalendar();
    gval.setTime(val);
    modify(gval, field, false);
    return gval.getTime();
}

From source file:gbt.ubt.tool.Orthorectifier.java

public static BoundedPropagator generateEphemeris(InputParameters params) throws IOException, OrekitException {
    /*//from   w  ww .  j a va 2s . c  o m
     This function generates the satellite ephemeris for the duration of the acquisition using the orbital
     state vector that is embedded in the product header (MPH). This state vector is normally the restituted
     orbit from the Flight Operations Segment.
     The open source Orekit library (https://www.orekit.org/) is used to perform the numerical propagation.
     The generated ephemeris compares well against the available DORIS Precise Orbit files (maximum 10 metre
     displacement vector magnitude during propagation duration).
     */
    if (params.orthorectify) {
        System.out.println("Orthorectification is selected, performing orbit propagation");

        /* Get the acquisition duration and state vector from the L1b product */
        DataProvidersManager.getInstance().addProvider(new ZipJarCrawler(new File("orekit-data.zip")));
        Product readProduct = ProductIO.readProduct(params.inputFileLocation);
        Date startTime = readProduct.getStartTime().getAsDate();
        Date stopTime = readProduct.getEndTime().getAsDate();
        MetadataElement metadataRoot = readProduct.getMetadataRoot();
        Date vectorTime = metadataRoot.getElementAt(0).getAttributeUTC("STATE_VECTOR_TIME").getAsDate();
        Double xPos = metadataRoot.getElementAt(0).getAttributeDouble("X_POSITION");
        Double yPos = metadataRoot.getElementAt(0).getAttributeDouble("Y_POSITION");
        Double zPos = metadataRoot.getElementAt(0).getAttributeDouble("Z_POSITION");
        Double xVel = metadataRoot.getElementAt(0).getAttributeDouble("X_VELOCITY");
        Double yVel = metadataRoot.getElementAt(0).getAttributeDouble("Y_VELOCITY");
        Double zVel = metadataRoot.getElementAt(0).getAttributeDouble("Z_VELOCITY");

        /* Product state vector in fixed frame and UTC
         Note state vector and product/acquisition start time are not coincident
         */
        Frame fixedFrame = FramesFactory.getITRF(IERSConventions.IERS_2010, false);
        TimeScale utc = TimeScalesFactory.getUTC();
        GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
        cal.setTime(vectorTime);
        AbsoluteDate initialDate = new AbsoluteDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1,
                cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
                cal.get(Calendar.SECOND), utc);
        Vector3D position = new Vector3D(xPos, yPos, zPos);
        Vector3D velocity = new Vector3D(xVel, yVel, zVel);

        /* Orekit can only propate orbits defined in inertial reference frame
         Convert state vector to J2000 (EME2000) frame.
         */
        Frame inertialFrame = FramesFactory.getEME2000();
        Transform frameTransform = fixedFrame.getTransformTo(inertialFrame, initialDate);
        PVCoordinates transformPVCoordinates = frameTransform
                .transformPVCoordinates(new PVCoordinates(position, velocity));

        /* Set initial spacecraft state */
        double mu = 3.986004415e+14; // Central attraction coefficient
        Orbit initialOrbit = new CartesianOrbit(transformPVCoordinates, inertialFrame, initialDate, mu);
        SpacecraftState initialState = new SpacecraftState(initialOrbit, 7892.0); // Orbital parameters and Envisat dry mass

        /* Set up numerical integrator for propagation */
        double minStep = 1;
        double maxStep = 1000;
        double initialStep = 60;
        OrbitType propagationType = OrbitType.CARTESIAN;
        double[][] tolerance = NumericalPropagator.tolerances(0.001, initialOrbit, propagationType);
        AdaptiveStepsizeIntegrator integrator = new DormandPrince853Integrator(minStep, maxStep, tolerance[0],
                tolerance[1]);
        integrator.setInitialStepSize(initialStep);
        NumericalPropagator propagator = new NumericalPropagator(integrator);
        propagator.setOrbitType(propagationType);

        /* Add force models to the propagator:
         Consider central body (Earth) gravity pertubation
         Consider third body (Sun & Moon) gravity pertubation
         Other pertubations are not considered (computation speed/accuracy trade-off)
         */
        NormalizedSphericalHarmonicsProvider provider = GravityFieldFactory.getNormalizedProvider(21, 21);
        ForceModel holmesFeatherstone = new HolmesFeatherstoneAttractionModel(
                FramesFactory.getITRF(IERSConventions.IERS_2010, true), provider);
        propagator.addForceModel(holmesFeatherstone);
        propagator.addForceModel(new ThirdBodyAttraction(CelestialBodyFactory.getSun()));
        propagator.addForceModel(new ThirdBodyAttraction(CelestialBodyFactory.getMoon()));

        /* Run  the propagator to generate the ephemeris */
        propagator.setInitialState(initialState);
        propagator.setEphemerisMode();
        cal.setTime(startTime);
        AbsoluteDate startDate = new AbsoluteDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1,
                cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
                cal.get(Calendar.SECOND), utc);
        cal.setTime(stopTime);
        AbsoluteDate stopDate = new AbsoluteDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1,
                cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
                cal.get(Calendar.SECOND), utc);
        SpacecraftState finalState = propagator.propagate(startDate.shiftedBy(-600.0),
                stopDate.shiftedBy(600.0));
        BoundedPropagator ephemeris = propagator.getGeneratedEphemeris();

        return ephemeris;
    } else {
        return null;
    }
}

From source file:org.sonar.db.purge.period.Interval.java

static List<Interval> group(List<PurgeableAnalysisDto> snapshots, Date start, Date end, int calendarField) {
    List<Interval> intervals = Lists.newArrayList();

    GregorianCalendar calendar = new GregorianCalendar();
    int lastYear = -1;
    int lastFieldValue = -1;
    Interval currentInterval = null;//www.j a  va  2  s . com

    for (PurgeableAnalysisDto snapshot : snapshots) {
        if (!DateUtils.isSameDay(start, snapshot.getDate()) && snapshot.getDate().after(start)
                && (snapshot.getDate().before(end) || DateUtils.isSameDay(end, snapshot.getDate()))) {
            calendar.setTime(snapshot.getDate());
            int currentFieldValue = calendar.get(calendarField);
            int currentYear = calendar.get(Calendar.YEAR);
            if (lastYear != currentYear || lastFieldValue != currentFieldValue) {
                currentInterval = new Interval();
                intervals.add(currentInterval);
            }
            lastFieldValue = currentFieldValue;
            lastYear = currentYear;
            if (currentInterval != null) {
                currentInterval.add(snapshot);
            }
        }
    }
    return intervals;
}

From source file:org.sonar.core.computation.dbcleaner.period.Interval.java

static List<Interval> group(List<PurgeableSnapshotDto> snapshots, Date start, Date end, int calendarField) {
    List<Interval> intervals = Lists.newArrayList();

    GregorianCalendar calendar = new GregorianCalendar();
    int lastYear = -1;
    int lastFieldValue = -1;
    Interval currentInterval = null;//www.  j  a  v a  2  s. com

    for (PurgeableSnapshotDto snapshot : snapshots) {
        if (!DateUtils.isSameDay(start, snapshot.getDate()) && snapshot.getDate().after(start)
                && (snapshot.getDate().before(end) || DateUtils.isSameDay(end, snapshot.getDate()))) {
            calendar.setTime(snapshot.getDate());
            int currentFieldValue = calendar.get(calendarField);
            int currentYear = calendar.get(Calendar.YEAR);
            if (lastYear != currentYear || lastFieldValue != currentFieldValue) {
                currentInterval = new Interval();
                intervals.add(currentInterval);
            }
            lastFieldValue = currentFieldValue;
            lastYear = currentYear;
            if (currentInterval != null) {
                currentInterval.add(snapshot);
            }
        }
    }
    return intervals;
}