/** * Converts a date/time value to a string, using the format dd-mmm-yyyy hh:mm. Because we cannot * determine the absence of a time from a time of 24:00, we must assume a time of 24:00 means * that no time is present and strip that from the return value. * * @param date Date value to convert. * @return Formatted string representation of the specified date, or an empty string if date is * null. */ public static String formatDate(Date date) { return formatDate(date, false, false); }
/** * Sets the end date. * * @param endDate The end date. */ private void setEndDate(String endDate) { this.rawEndDate = endDate; this.endDate = DateUtil.parseDate(endDate); }
private String formatDateAndAge(Date date) { return date == null ? null : DateUtil.formatDate(date) + " (" + DateUtil.formatAge(date) + ")"; }
/** * Returns the current date in standard format. * * @return Timestamp for current date. */ public String getTimestamp() { return DateUtil.formatDate(DateUtil.stripTime(new Date())); }
private void testDate(Date date, boolean showTimezone, boolean ignoreTime) { String text = DateUtil.formatDate(date, showTimezone, ignoreTime); print(text); Date date2 = DateUtil.parseDate(text); String text2 = DateUtil.formatDate(date2, showTimezone, ignoreTime); assertEquals(text, text2); }
/** * Returns true if the date has an associated time. * * @param date Date value to check. * @return True if the date has a time component. */ public static boolean hasTime(Date date) { if (date == null) { return false; } long time1 = date.getTime(); long time2 = stripTime(date).getTime(); return time1 != time2; // Do not use "Date.equals" since date may be of type Timestamp. }
@Test public void testDateUtil() { testDate(now()); testDate(today()); testDate("T", today(), 0); testDate("N", now(), 100); testDate("T+30", DateUtil.addDays(today(), 30, false), 0); testDate("N+30", DateUtil.addDays(now(), 30, false), 100); testDate("T-4", DateUtil.addDays(today(), -4, false), 0); testDate("T-50s", new Date(today().getTime() - 50000), 0); testDate("N-50s", new Date(now().getTime() - 50000), 100); testDate("T-50h", new Date(today().getTime() - 50 * 60 * 60 * 1000), 0); testDate("T-50n", new Date(today().getTime() - 50 * 60 * 1000), 0); Date date = DateUtil.parseDate("19880302"); testDate(date.toString(), date, 0); }
@Test public void testAge() { Date dob = DateUtil.toDate(27, 7, 1958); Date ref = DateUtil.toDate(1, 1, 2013); assertEquals("54 yrs", DateUtil.formatAge(dob, true, ref)); assertEquals("54 yr", DateUtil.formatAge(dob, false, ref)); dob = DateUtil.toDate(15, 12, 2012); assertEquals("17 days", DateUtil.formatAge(dob, true, ref)); dob = DateUtil.toDate(30, 10, 2012); assertEquals("2 mos", DateUtil.formatAge(dob, true, ref)); }
/** * <p> * Returns age as a formatted string expressed in days, months, or years, depending on whether * person is an infant (< 2 mos), toddler (> 2 mos, < 2 yrs), or more than 2 years old. * </p> * * @param dob Date of person's birth * @return the age display string */ public static String formatAge(Date dob) { return formatAge(dob, true, null); }
/** * Formats a duration in ms. * * @param duration Duration in ms. * @return Formatted duration. */ public static String formatDuration(long duration) { return formatDuration(duration, null); }
@Test public void testElapsed() { assertEquals("0.1 seconds", DateUtil.formatElapsed(100.0, TimeUnit.SECONDS)); assertEquals("1 second", DateUtil.formatElapsed(1000.0)); assertEquals("1 minute", DateUtil.formatElapsed(60000.0)); assertEquals("3.59 days", DateUtil.formatElapsed(309898934.0)); assertEquals("98.2 years", DateUtil.formatElapsed(3098989343984.0)); assertEquals("-98.2 years", DateUtil.formatElapsed(-3098989343984.0)); assertEquals(100.0, DateUtil.parseElapsed("0.1 seconds"), 0.0); assertEquals(1000.0, DateUtil.parseElapsed("1 second"), 0.0); assertEquals(60000.0, DateUtil.parseElapsed("1 minute"), 0.0); assertEquals(310176000.0, DateUtil.parseElapsed("3.59 days"), 0.0); assertEquals(3098956320000.0, DateUtil.parseElapsed("98.2 years"), 0.0); assertEquals(-3098956320000.0, DateUtil.parseElapsed("-98.2 years"), 0.0); assertEquals(98.2, DateUtil.parseElapsed("98.2 years", TimeUnit.YEARS), 0.0); }
/** * Return elapsed time in ms to displayable format with units. * * @param elapsed Elapsed time in ms. * @param pluralize If true, pluralize units when appropriate. * @param abbreviated If true, use abbreviated form of units. * @param round If true, round result to an integer. * @return Elapsed time in displayable format. */ public static String formatElapsed(double elapsed, boolean pluralize, boolean abbreviated, boolean round) { return formatElapsed(elapsed, pluralize, abbreviated, round, null); }
/** * Strips the time component from a date. * * @param date Original date. * @return Date without the time component. */ public static Date stripTime(Date date) { return addDays(date, 0, true); }
/** * Update the datebox with the new value. * * @param date New date value. */ private void updateDatebox(Date date) { datebox.setValue(DateUtil.stripTime(date)); }
addDemographic(root, "gender", patient.getGender()); addDemographic(root, "age", DateUtil.formatAge(patient.getBirthDate())); addDemographic(root, "dob", patient.getBirthDate()); addDemographic(root, "dod", patient.getDeceased());
/** * Formats a duration in ms to the specified accuracy. * * @param duration Duration in ms. * @param accuracy Accuracy of output. * @return Formatted duration. */ public static String formatDuration(long duration, TimeUnit accuracy) { return formatDuration(duration, accuracy, true, false); }
/** * Return elapsed time in ms to displayable format with units. * * @param elapsed Elapsed time in ms. * @return Elapsed time in displayable format. */ public static String formatElapsed(double elapsed) { return formatElapsed(elapsed, true, false, false); }
/** * Converts a date/time value to a string, using the format dd-mmm-yyyy hh:mm. Because we cannot * determine the absence of a time from a time of 24:00, we must assume a time of 24:00 means * that no time is present and strip that from the return value. * * @param date Date value to convert. * @param showTimezone If true, time zone information is also appended. * @return Formatted string representation of the specified date, or an empty string if date is * null. */ public static String formatDate(Date date, boolean showTimezone) { return formatDate(date, showTimezone, false); }
/** * Retrieves a formatted header for the current patient. * * @return Formatted header. */ public String getPatientInfo() { Patient patient = PatientContext.getActivePatient(); String text; if (patient == null) { text = "No Patient Selected"; } else { Identifier mrn = FhirUtil.getMRN(patient); // May be null! text = FhirUtil.formatName(patient.getName()); if (mrn != null) { text += " #" + mrn.getValue(); } String gender = patient.hasGender() ? patient.getGender().getDisplay() : ""; if (!StringUtils.isEmpty(gender)) { text += " (" + gender + ")"; } Date deceased = patient.getDeceased() instanceof DateType ? ((DateType) patient.getDeceased()).getValue() : null; String age = DateUtil.formatAge(patient.getBirthDate(), true, deceased); text += " Age: " + age; if (deceased != null) { text += " Died: " + DateUtil.formatDate(deceased); } } return text; }