List of usage examples for java.util GregorianCalendar get
public int get(int field)
From source file:DateUtility.java
/** * Gets the last millsecond of the month, according to the timestamp and * timezone arguments.//from w w w . j a v a 2 s. c om * @param timestamp * @param tz * @return long */ static public long getLastMilliOfMonth(long timestamp, TimeZone tz) { timestamp = getFirstMilliOfMonth(timestamp, tz); GregorianCalendar cal = new GregorianCalendar(); cal.setTimeZone(tz); cal.setTime(new Date(timestamp)); // now we'll roll the calendar forward one month. // in the case of december, we need to roll forward the year too if (cal.get(GregorianCalendar.MONTH) == GregorianCalendar.DECEMBER) { cal.roll(GregorianCalendar.YEAR, true); } cal.roll(GregorianCalendar.MONTH, true); long date = cal.getTime().getTime(); date = date - 1L; return date; }
From source file:org.openvpms.archetype.rules.util.DateRules.java
/** * Adds a date and time./*w ww . j a va2s. c o m*/ * * @param date the date part * @param time the time to add * @return the date+time */ public static Date addDateTime(Date date, Date time) { GregorianCalendar dateCal = new GregorianCalendar(); dateCal.setTime(date); GregorianCalendar timeCal = new GregorianCalendar(); timeCal.setTime(time); dateCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY)); dateCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE)); dateCal.set(Calendar.SECOND, timeCal.get(Calendar.SECOND)); return dateCal.getTime(); }
From source file:org.openhie.openempi.openpixpdq.v3.util.HL7DataTransformHelper.java
public static TSExplicit CreationTimeFactory() { String timestamp = ""; TSExplicit creationTime = new TSExplicit(); try {//from ww w .j a v a2s. c o m GregorianCalendar today = new GregorianCalendar(TimeZone.getTimeZone("GMT")); timestamp = String.valueOf(today.get(GregorianCalendar.YEAR)) + String.valueOf(today.get(GregorianCalendar.MONTH) + 1) + String.valueOf(today.get(GregorianCalendar.DAY_OF_MONTH)) + String.valueOf(today.get(GregorianCalendar.HOUR_OF_DAY)) + String.valueOf(today.get(GregorianCalendar.MINUTE)) + String.valueOf(today.get(GregorianCalendar.SECOND)); } catch (Exception e) { log.error("Exception when creating XMLGregorian Date"); log.error(" message: " + e.getMessage()); } if (Utilities.isNotNullish(timestamp)) { log.debug("Setting the creation timestamp to " + timestamp); creationTime.setValue(timestamp); } return creationTime; }
From source file:org.mifos.calendar.CalendarUtils.java
public static DateTime getFirstDayForMonthUsingWeekRankAndWeekday(final DateTime startDate, final int calendarWeekOfMonth, final int dayOfWeek) { /*//from w w w. j a va2s . c o m * In Joda time MONDAY=1 and SUNDAY=7, so shift these to SUNDAY=1, SATURDAY=7 to match this class */ int calendarDayOfWeek = (dayOfWeek % 7) + 1; final GregorianCalendar gc = new GregorianCalendar(); gc.setTime(startDate.toDate()); DateTime scheduleDate = null; // if current weekday is after the weekday on which schedule has to // lie, move to next week if (gc.get(Calendar.DAY_OF_WEEK) > calendarDayOfWeek) { gc.add(Calendar.WEEK_OF_MONTH, 1); } // set the weekday on which schedule has to lie gc.set(Calendar.DAY_OF_WEEK, calendarDayOfWeek); // if week rank is First, Second, Third or Fourth, Set the // respective week. // if current week rank is after the weekrank on which schedule has // to lie, move to next month if (!RankOfDay.getRankOfDay(calendarWeekOfMonth).equals(RankOfDay.LAST)) { if (gc.get(Calendar.DAY_OF_WEEK_IN_MONTH) > calendarWeekOfMonth) { gc.add(GregorianCalendar.MONTH, 1); gc.set(GregorianCalendar.DATE, 1); } // set the weekrank on which schedule has to lie gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, calendarWeekOfMonth); scheduleDate = new DateTime(gc.getTime().getTime()); } else {// scheduleData.getWeekRank()=Last int M1 = gc.get(GregorianCalendar.MONTH); // assumption: there are 5 weekdays in the month gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 5); int M2 = gc.get(GregorianCalendar.MONTH); // if assumption fails, it means there exists 4 weekdays in a // month, return last weekday date // if M1==M2, means there exists 5 weekdays otherwise 4 weekdays // in a month if (M1 != M2) { gc.set(GregorianCalendar.MONTH, gc.get(GregorianCalendar.MONTH) - 1); gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 4); } scheduleDate = new DateTime(gc.getTime().getTime()); } return scheduleDate; }
From source file:org.openhie.openempi.nhinadapter.hl7.Utilities.java
public static TSExplicit generateCreationTime() { String timestamp = ""; TSExplicit creationTime = new TSExplicit(); try {//from www. j a v a 2 s.c om GregorianCalendar today = new GregorianCalendar(TimeZone.getTimeZone("GMT")); timestamp = String.valueOf(today.get(GregorianCalendar.YEAR)) + String.valueOf(today.get(GregorianCalendar.MONTH) + 1) + String.valueOf(today.get(GregorianCalendar.DAY_OF_MONTH)) + String.valueOf(today.get(GregorianCalendar.HOUR_OF_DAY)) + String.valueOf(today.get(GregorianCalendar.MINUTE)) + String.valueOf(today.get(GregorianCalendar.SECOND)); } catch (Exception e) { log.error("Exception when creating Gregorian Date"); log.error(" message: " + e.getMessage()); } if (isNotNullish(timestamp)) { log.debug("Setting the creation timestamp to " + timestamp); creationTime.setValue(timestamp); } return creationTime; }
From source file:oscar.MyDateFormat.java
public static int getAge(int year, int month, int date) { GregorianCalendar now = new GregorianCalendar(); int curYear = now.get(Calendar.YEAR); int curMonth = (now.get(Calendar.MONTH) + 1); int curDay = now.get(Calendar.DAY_OF_MONTH); int age = 0;// w w w .j av a 2 s . c om if (curMonth > month || (curMonth == month && curDay >= date)) { age = curYear - year; } else { age = curYear - year - 1; } return age; }
From source file:Main.java
public static Duration subtract(XMLGregorianCalendar x1, XMLGregorianCalendar x2) { boolean positive = x1.compare(x2) >= 0; if (!positive) { XMLGregorianCalendar temp = x1; x1 = x2;/*from w w w . j av a 2 s . co m*/ x2 = temp; } BigDecimal s1 = getSeconds(x1); BigDecimal s2 = getSeconds(x2); BigDecimal seconds = s1.subtract(s2); if (seconds.compareTo(BigDecimal.ZERO) < 0) seconds = seconds.add(BigDecimal.valueOf(60)); GregorianCalendar g1 = x1.toGregorianCalendar(); GregorianCalendar g2 = x2.toGregorianCalendar(); int year = 0; for (int f : reverseFields) { if (f == Calendar.YEAR) { int year1 = g1.get(f); int year2 = g2.get(f); year = year1 - year2; } else { subtractField(g1, g2, f); } } return FACTORY.newDuration(positive, BigInteger.valueOf(year), BigInteger.valueOf(g1.get(Calendar.MONTH)), BigInteger.valueOf(g1.get(Calendar.DAY_OF_MONTH) - 1), BigInteger.valueOf(g1.get(Calendar.HOUR_OF_DAY)), BigInteger.valueOf(g1.get(Calendar.MINUTE)), seconds); }
From source file:util.android.util.DateUtils.java
public static int getAge(Date dateOfBirth) { GregorianCalendar cal = new GregorianCalendar(); int y, m, d, a; y = cal.get(Calendar.YEAR); m = cal.get(Calendar.MONTH);/*from w w w.j a va2 s . com*/ d = cal.get(Calendar.DAY_OF_MONTH); cal.setTime(dateOfBirth); a = y - cal.get(Calendar.YEAR); if ((m < cal.get(Calendar.MONTH)) || ((m == cal.get(Calendar.MONTH)) && (d < cal.get(Calendar.DAY_OF_MONTH)))) { --a; } if (a < 0) throw new IllegalArgumentException("Age < 0"); return a; }
From source file:gbt.ubt.tool.Orthorectifier.java
public static BoundedPropagator generateEphemeris(InputParameters params) throws IOException, OrekitException { /*/*from w ww . jav a 2 s .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;//ww w. ja v a 2s . 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; }