public static void testGetAge() { DateTime now = new DateTime(); DateTime oneYearsAgo = now.minusYears(2).plusDays(20); DateTime twoYearsAgo = now.minusYears(2); Assert.assertTrue(1 == DateUtils.getAge(oneYearsAgo)); Assert.assertTrue(2 == DateUtils.getAge(twoYearsAgo)); }
// TOOLS private DateTime getDateLimit(final boolean upper) { final Date date = DateUtils.truncate(new Date(), Calendar.DATE); final DateTime now = new DateTime(date); switch (type) { case YEAR: return upper ? now.plusYears(upperBoundary) : now.minusYears(lowerBoundary); case MONTH: return upper ? now.plusMonths(upperBoundary) : now.minusMonths(lowerBoundary); case DAY: return upper ? now.plusDays(upperBoundary) : now.minusDays(lowerBoundary); default: throw new IllegalArgumentException("Unsupported type of time unit: " + type); } }
public static void main(String[] args) throws Exception { // test day init and print to string DateTime fooDateTime = new DateTime(1978, 6, 1, 12, 10, 8, 0); System.out.println(fooDateTime.toString("yyyy-MM-dd HH:mm:ss")); // "1978-06-01 12:10:08" // test minus/plus and years/days between function DateTime now = new DateTime(); DateTime nowLater = now.plusHours(22); DateTime tomorrow = now.plusHours(25); System.out.println(isSameDay(now, nowLater)); // true System.out.println(isSameDay(now, tomorrow)); // false DateTime oneYearsAgo = now.minusYears(2).plusDays(20); DateTime twoYearsAgo = now.minusYears(2); System.out.println(getAge(oneYearsAgo)); // 1 System.out.println(getAge(twoYearsAgo)); // 2 }
public static Date getDateFromDateDesc(String leadTime) { DateTime dateTime = new DateTime(0, 12, 31, 0, 0, 0, 0); if (!Strings.isEmpty(leadTime)) { String time = "0"; if (leadTime.indexOf(".") > 0) { time = leadTime.substring(0, leadTime.indexOf(".")); } else { time = leadTime.substring(0, leadTime.length() - 1); } int timeVal = Integer.parseInt(time); if (leadTime.indexOf(DAY) >= 0) { if (timeVal > 0) { dateTime = dateTime.plusDays(timeVal); } else { dateTime = dateTime.minusDays(Math.abs(timeVal)); } } else if (leadTime.indexOf(WEEK) >= 0) { if (timeVal > 0) { dateTime = dateTime.plusWeeks(timeVal); } else { dateTime = dateTime.minusWeeks(Math.abs(timeVal)); } } else if (leadTime.indexOf(MONTH) >= 0) { if (timeVal > 0) { dateTime = dateTime.plusMonths(timeVal); } else { dateTime = dateTime.minusMonths(Math.abs(timeVal)); } } else if (leadTime.indexOf(QUARTER) >= 0) { if (timeVal > 0) { dateTime = dateTime.plusMonths(timeVal * 3); } else { dateTime = dateTime.minusMonths(Math.abs(timeVal * 3)); } } else if (leadTime.indexOf(YEAR) >= 0) { if (timeVal > 0) { dateTime = dateTime.plusYears(timeVal); } else { dateTime = dateTime.minusYears(Math.abs(timeVal)); } } return dateTime.toDate(); } return dateTime.toDate(); }
private void addProtectionOrderElements( Element rapSheetElement, PersonElementWrapper person, DateTime baseDate) { int orderCount = generatePoissonInt(.3, false); for (int i = 0; i < orderCount; i++) { Element orderElement = appendElement(rapSheetElement, OjbcNamespaceContext.NS_CH_EXT, "Order"); Element activityElement = appendElement(orderElement, OjbcNamespaceContext.NS_NC, "ActivityIdentification"); Element e = appendElement(activityElement, OjbcNamespaceContext.NS_NC, "IdentificationID"); e.setTextContent(generateRandomID("ORDER", 10)); e = appendElement(activityElement, OjbcNamespaceContext.NS_NC, "IdentificationCategoryText"); e.setTextContent("TRO DOCUMENT ID"); e = appendElement(orderElement, OjbcNamespaceContext.NS_NC, "ActivityCategoryText"); e.setTextContent("TRO"); e = appendElement(orderElement, OjbcNamespaceContext.NS_NC, "ActivityDate"); e = appendElement(e, OjbcNamespaceContext.NS_NC, "Date"); DateTime orderDate = generateUniformRandomDateBetween(baseDate, baseDate.minusYears(5)); e.setTextContent(DATE_FORMATTER_YYYY_MM_DD.print(orderDate)); e = appendElement(orderElement, OjbcNamespaceContext.NS_JXDM_41, "CourtOrderIssuingCourt"); e = appendElement(e, OjbcNamespaceContext.NS_NC, "OrganizationName"); e.setTextContent(getRandomCounty(person.state) + " District Court"); e = appendElement( orderElement, OjbcNamespaceContext.NS_JXDM_41, "CourtOrderServiceDescriptionText"); e.setTextContent(generateRandomCodeFromList("PENDING", "SERVED")); e = appendElement(orderElement, OjbcNamespaceContext.NS_CH_EXT, "CourtCase"); e = appendElement(e, OjbcNamespaceContext.NS_NC, "ActivityIdentification"); e = appendElement(e, OjbcNamespaceContext.NS_NC, "IdentificationID"); e.setTextContent("TRO-" + baseDate.getYear() + generateRandomID("-", 8)); e = appendElement( orderElement, OjbcNamespaceContext.NS_CH_EXT, "ProtectionOrderExpirationDate"); e = appendElement(e, OjbcNamespaceContext.NS_NC, "Date"); e.setTextContent( DATE_FORMATTER_YYYY_MM_DD.print(orderDate.plusDays(randomGenerator.nextInt(5, 365)))); } }
/** * 计算指定时间之前的日期 * * @param date 待计算时间 * @param random 待计算数 * @param timeType 时间类型枚举 * @see org.codingsills.modules.utils.DateKit.TimeType * @return Date */ public static Date preDate(Date date, int random, TimeType timeType) { Date preDate = null; if (date == null) { return preDate; } DateTime dateTime = fromDate(date); random = Math.abs(random); switch (timeType.getCode()) { case "Y": preDate = dateTime.minusYears(random).toDate(); break; case "M": preDate = dateTime.minusMonths(random).toDate(); break; case "W": preDate = dateTime.minusWeeks(random).toDate(); break; case "D": preDate = dateTime.minusDays(random).toDate(); break; case "H": preDate = dateTime.minusHours(random).toDate(); break; case "MIN": preDate = dateTime.minusMinutes(random).toDate(); break; case "S": preDate = dateTime.minusSeconds(random).toDate(); break; case "MS": preDate = dateTime.minusMillis(random).toDate(); break; default: break; } return preDate; }
public static Date minusYears(Date date, int numberOfYears) { if (date == null) date = new Date(); DateTime dt = new DateTime(date); return dt.minusYears(numberOfYears).toDate(); }
private Patient createPatient() { Person person = new Person(); // Add address PersonAddress address = new PersonAddress(); address.setAddress1(ViewUtils.getInput(edaddr1)); address.setAddress2(ViewUtils.getInput(edaddr2)); address.setCityVillage(ViewUtils.getInput(edcity)); address.setPostalCode(ViewUtils.getInput(edpostal)); address.setCountry(ViewUtils.getInput(edcountry)); address.setStateProvince(ViewUtils.getInput(edstate)); address.setPreferred(true); List<PersonAddress> addresses = new ArrayList<>(); addresses.add(address); person.setAddresses(addresses); // Add names PersonName name = new PersonName(); name.setFamilyName(ViewUtils.getInput(edlname)); name.setGivenName(ViewUtils.getInput(edfname)); name.setMiddleName(ViewUtils.getInput(edmname)); List<PersonName> names = new ArrayList<>(); names.add(name); person.setNames(names); // Add gender String[] genderChoices = {"M", "F"}; int index = gen.indexOfChild(getActivity().findViewById(gen.getCheckedRadioButtonId())); if (index != -1) { person.setGender(genderChoices[index]); } else { person.setGender(null); } // Add birthdate String birthdate = null; if (ViewUtils.isEmpty(eddob)) { if (!StringUtils.isBlank(ViewUtils.getInput(edyr)) || !StringUtils.isBlank(ViewUtils.getInput(edmonth))) { int yeardiff = ViewUtils.isEmpty(edyr) ? 0 : Integer.parseInt(edyr.getText().toString()); int mondiff = ViewUtils.isEmpty(edmonth) ? 0 : Integer.parseInt(edmonth.getText().toString()); LocalDate now = new LocalDate(); bdt = now.toDateTimeAtStartOfDay().toDateTime(); bdt = bdt.minusYears(yeardiff); bdt = bdt.minusMonths(mondiff); person.setBirthdateEstimated(true); birthdate = bdt.toString(); } } else { birthdate = bdt.toString(); } person.setBirthdate(birthdate); final Patient patient = new Patient(); patient.setPerson(person); patient.setUuid(" "); return patient; }
public Arrest(DateTime baseDate, PersonElementWrapper person) { date = person.birthdate; while (Years.yearsBetween(person.birthdate, date).getYears() < 14) { // make sure we don't arrest anyone younger than 14 date = generateUniformRandomDateBetween(baseDate.minusYears(8), baseDate); } id = generateRandomID( "A", 10); // could be a problem in the unlikely event you generate two arrests with the // same id (this is used as an xml id) recordId = generateRandomID("", 3) + generateRandomID("-", 7); int chargeCount = generatePoissonInt(1, true); int courtCaseLength = (int) randomGenerator.nextGaussian(180, 60); int daysSinceArrest = Days.daysBetween(date, baseDate).getDays(); arrestingAgency = new Agency(); arrestingAgency.name = getRandomCity(person.state).toUpperCase() + " PD"; if (courtCaseLength < daysSinceArrest) { dispoDate = date.plusDays(courtCaseLength); } int maxDaysInJail = 0; int maxDaysOfProbation = 0; felonyConviction = false; for (int i = 0; i < chargeCount; i++) { ArrestCharge arrestCharge = new ArrestCharge(person); charges.add(arrestCharge); if (arrestCharge.offense != null) { maxDaysInJail = Math.max(maxDaysInJail, arrestCharge.offense.daysInJail); maxDaysOfProbation = Math.max(maxDaysOfProbation, arrestCharge.offense.daysOfProbation); if ("F".equals(arrestCharge.severity.substring(0, 1))) { felonyConviction = true; } if (arrestCharge.offense.daysOfProbation > 0) { probationSupervisionAgency = arrestCharge.offense.supervisionAgency; } if (arrestCharge.offense.daysInJail > 0) { custodySupervisionAgency = arrestCharge.offense.supervisionAgency; } } } if (maxDaysInJail > 0 && dispoDate != null) { custodyEndDate = dispoDate.plusDays(maxDaysInJail); custodySupervisionId = id + "SC"; } if (maxDaysOfProbation > 0 && dispoDate != null) { probationEndDate = dispoDate.plusDays(maxDaysOfProbation); probationSupervisionId = id + "SP"; } if (coinFlip(.15)) { arrestingAgency.name = "State Police"; } }