List of usage examples for org.joda.time Months getMonths
public int getMonths()
From source file:com.cisco.dvbu.ps.utils.date.DateDiffDate.java
License:Open Source License
/** * Called to invoke the stored procedure. Will only be called a * single time per instance. Can throw CustomProcedureException or * SQLException if there is an error during invoke. *///from w ww . j a v a 2 s. c o m public void invoke(Object[] inputValues) throws CustomProcedureException, SQLException { java.util.Date startDate = null; java.util.Date endDate = null; Calendar startCal = null; Calendar endCal = null; DateTime startDateTime = null; DateTime endDateTime = null; String datePart = null; long dateLength = 0; try { result = null; if (inputValues[0] == null) { result = new Long(dateLength); return; } if (inputValues[1] == null) { result = new Long(dateLength); return; } if (inputValues[2] == null) { result = new Long(dateLength); return; } datePart = (String) inputValues[0]; startDate = (java.util.Date) inputValues[1]; startCal = Calendar.getInstance(); startCal.setTime(startDate); endDate = (java.util.Date) inputValues[2]; endCal = Calendar.getInstance(); endCal.setTime(endDate); startDateTime = new DateTime(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH) + 1, startCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0, 0); endDateTime = new DateTime(endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH) + 1, endCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0, 0); if (datePart.equalsIgnoreCase("second")) { Seconds seconds = Seconds.secondsBetween(startDateTime, endDateTime); dateLength = seconds.getSeconds(); } if (datePart.equalsIgnoreCase("minute")) { Minutes minutes = Minutes.minutesBetween(startDateTime, endDateTime); dateLength = minutes.getMinutes(); } if (datePart.equalsIgnoreCase("hour")) { Hours hours = Hours.hoursBetween(startDateTime, endDateTime); dateLength = hours.getHours(); } if (datePart.equalsIgnoreCase("day")) { Days days = Days.daysBetween(startDateTime, endDateTime); dateLength = days.getDays(); } if (datePart.equalsIgnoreCase("week")) { Weeks weeks = Weeks.weeksBetween(startDateTime, endDateTime); dateLength = weeks.getWeeks(); } if (datePart.equalsIgnoreCase("month")) { Months months = Months.monthsBetween(startDateTime, endDateTime); dateLength = months.getMonths(); } if (datePart.equalsIgnoreCase("year")) { Years years = Years.yearsBetween(startDateTime, endDateTime); dateLength = years.getYears(); } result = new Long(dateLength); } catch (Throwable t) { throw new CustomProcedureException(t); } }
From source file:com.cisco.dvbu.ps.utils.date.DateDiffTimestamp.java
License:Open Source License
/** * Called to invoke the stored procedure. Will only be called a * single time per instance. Can throw CustomProcedureException or * SQLException if there is an error during invoke. *//*ww w . j a v a 2 s . c om*/ public void invoke(Object[] inputValues) throws CustomProcedureException, SQLException { Timestamp startTimestamp = null; Timestamp endTimestamp = null; Calendar startCal = null; Calendar endCal = null; DateTime startDateTime = null; DateTime endDateTime = null; String datePart = null; long dateLength = 0; try { result = null; if (inputValues[0] == null) { result = new Long(dateLength); return; } if (inputValues[1] == null) { result = new Long(dateLength); return; } if (inputValues[2] == null) { result = new Long(dateLength); return; } datePart = (String) inputValues[0]; startTimestamp = (Timestamp) inputValues[1]; // long startMilliseconds = startTimestamp.getTime() + // (startTimestamp.getNanos() / 1000000); long startMilliseconds = startTimestamp.getTime() + (startTimestamp.getNanos() % 1000000L >= 500000L ? 1 : 0); startCal = Calendar.getInstance(); startCal.setTimeInMillis(startMilliseconds); endTimestamp = (Timestamp) inputValues[2]; // long endMilliseconds = endTimestamp.getTime() + // (endTimestamp.getNanos() / 1000000); long endMilliseconds = endTimestamp.getTime() + (endTimestamp.getNanos() % 1000000L >= 500000L ? 1 : 0); endCal = Calendar.getInstance(); endCal.setTimeInMillis(endMilliseconds); startDateTime = new DateTime(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH) + 1, startCal.get(Calendar.DAY_OF_MONTH), startCal.get(Calendar.HOUR_OF_DAY), startCal.get(Calendar.MINUTE), startCal.get(Calendar.SECOND), startCal.get(Calendar.MILLISECOND)); endDateTime = new DateTime(endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH) + 1, endCal.get(Calendar.DAY_OF_MONTH), endCal.get(Calendar.HOUR_OF_DAY), endCal.get(Calendar.MINUTE), endCal.get(Calendar.SECOND), endCal.get(Calendar.MILLISECOND)); Interval interval = new Interval(startDateTime, endDateTime); if (datePart.equalsIgnoreCase("second") || datePart.equalsIgnoreCase("ss")) { Seconds seconds = Seconds.secondsIn(interval); dateLength = seconds.getSeconds(); } else if (datePart.equalsIgnoreCase("minute") || datePart.equalsIgnoreCase("mi")) { Minutes minutes = Minutes.minutesIn(interval); dateLength = minutes.getMinutes(); } else if (datePart.equalsIgnoreCase("hour") || datePart.equalsIgnoreCase("hh")) { Hours hours = Hours.hoursIn(interval); dateLength = hours.getHours(); } else if (datePart.equalsIgnoreCase("day") || datePart.equalsIgnoreCase("dd")) { Days days = Days.daysIn(interval); dateLength = days.getDays(); } else if (datePart.equalsIgnoreCase("week") || datePart.equalsIgnoreCase("wk")) { Weeks weeks = Weeks.weeksIn(interval); dateLength = weeks.getWeeks(); } else if (datePart.equalsIgnoreCase("month") || datePart.equalsIgnoreCase("mm")) { Months months = Months.monthsIn(interval); dateLength = months.getMonths(); } else if (datePart.equalsIgnoreCase("year") || datePart.equalsIgnoreCase("yy")) { Years years = Years.yearsIn(interval); dateLength = years.getYears(); } else if (datePart.equalsIgnoreCase("millisecond") || datePart.equalsIgnoreCase("ms")) { dateLength = (endTimestamp.getTime() - startTimestamp.getTime()); // millis } else if (datePart.equalsIgnoreCase("microsecond") || datePart.equalsIgnoreCase("mcs")) { dateLength = ((endTimestamp.getTime() - startTimestamp.getTime()) / 1000) // seconds * 1000000L // micros + (endTimestamp.getNanos() - startTimestamp.getNanos()) / 1000; // nanos/1000 } else if (datePart.equalsIgnoreCase("nanosecond") || datePart.equalsIgnoreCase("ns")) { dateLength = ((endTimestamp.getTime() - startTimestamp.getTime()) / 1000) // seconds * 1000000000L // nanos + (endTimestamp.getNanos() - startTimestamp.getNanos()); // nanos } else { throw new IllegalArgumentException(datePart); } result = new Long(dateLength); } catch (Throwable t) { throw new CustomProcedureException(t); } }
From source file:com.github.trohovsky.jira.analyzer.IssuesPerMonthStrategy.java
License:Apache License
@Override public void analyze(String jqlQueryTemplate, List<String> queryParameters) { String jqlQuery = String.format(jqlQueryTemplate, queryParameters.toArray()); // adjust the query to correctly order the results, it's required due to creation date int indexOfOrderBy = jqlQuery.indexOf("ORDER BY"); if (indexOfOrderBy != -1) { jqlQuery = jqlQuery.substring(0, indexOfOrderBy - 1); }/* w ww. j a v a 2 s. c om*/ jqlQuery += " ORDER BY created ASC"; // TODO JRJC-205 Problem when searching issues with 'fields' parameter set final SearchResult searchResult = searchRestClient.searchJql(jqlQuery, 1, 0, null).claim(); DateTime firstIssueCreationDate = null; if (searchResult.getIssues().iterator().hasNext()) { final Issue firstIssue = searchResult.getIssues().iterator().next(); firstIssueCreationDate = firstIssue.getCreationDate(); } int monthDiff = 0; if (firstIssueCreationDate != null) { final DateTime today = new DateTime(); final Months mt = Months.monthsBetween(firstIssueCreationDate, today); monthDiff = mt.getMonths(); } final float issuesPerMonth = (float) searchResult.getTotal() / monthDiff; final SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); String firstIssueCreationDateString = null; if (firstIssueCreationDate != null) { firstIssueCreationDateString = formatter.format(firstIssueCreationDate.toDate()); } else { firstIssueCreationDateString = "None"; } System.out.println(String.format("%s %s %s %s", String.join(" ", queryParameters), searchResult.getTotal(), firstIssueCreationDateString, issuesPerMonth)); }
From source file:com.inkubator.common.util.DateTimeUtil.java
/** * get total months difference, between two date type * * @return Integer/*from w w w .j a v a 2s . co m*/ * @param date1 Date reference * @param date2 Date reference */ public static Integer getTotalMonthDifference(Date date1, Date date2) { Months months = Months.monthsBetween(new DateMidnight(date1), new DateMidnight(date2)); return months.getMonths(); }
From source file:com.planyourexchange.fragments.result.ResultCalculations.java
License:Open Source License
public void setNumberOfWeeks(Integer numberOfWeeks) { this.numberOfWeeks = new BigDecimal(numberOfWeeks); LocalDate localDate = LocalDate.now(); Months months = Months.monthsBetween(localDate, localDate.plusWeeks(numberOfWeeks)); this.months = new BigDecimal(months.getMonths()); }
From source file:com.rapidminer.operator.GenerateDateSeries.java
License:Open Source License
private long calculateNumberOfExample(DateTime startTime, DateTime endTime) { // TODO Auto-generated method stub long valuetoReturn = 0; try {//from www.j a va2 s. c o m //getParameterAsString(key) switch (getParameterAsString(PARAMETER_INTERVALTYPE)) { case "YEAR": Years year = Years.yearsBetween(startTime, endTime); valuetoReturn = year.getYears() / getParameterAsInt(INTERVAL); break; case "MONTH": Months month = Months.monthsBetween(startTime, endTime); valuetoReturn = month.getMonths() / getParameterAsInt(INTERVAL); break; case "DAY": Days days = Days.daysBetween(startTime, endTime); valuetoReturn = days.getDays() / getParameterAsInt(INTERVAL); break; case "HOUR": Hours hours = Hours.hoursBetween(startTime, endTime); valuetoReturn = hours.getHours() / getParameterAsInt(INTERVAL); break; case "MINUTE": Minutes minute = Minutes.minutesBetween(startTime, endTime); valuetoReturn = minute.getMinutes() / getParameterAsInt(INTERVAL); break; case "SECOND": Seconds second = Seconds.secondsBetween(startTime, endTime); valuetoReturn = second.getSeconds() / getParameterAsInt(INTERVAL); break; case "MILLISECOND": // Milliseconds millisecond = milli long milli = endTime.getMillis() - startTime.getMillis(); valuetoReturn = milli / getParameterAsInt(INTERVAL); break; default: valuetoReturn = 0; } } catch (Exception e) { valuetoReturn = 0; } return valuetoReturn; }
From source file:com.simopuve.helper.POIHelper.java
public static Integer getMonthNumber(Date currentDate) { Calendar cal = Calendar.getInstance(); cal.setTime(currentDate);//w ww.ja v a 2 s . c o m org.joda.time.format.DateTimeFormatter fmt = DateTimeFormat.forPattern("dd-MM-yyy"); DateTime nowDate = fmt.parseLocalDate( cal.get(Calendar.DAY_OF_MONTH) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.YEAR)) .toDateTimeAtCurrentTime(); Months m = Months.monthsBetween(initDate, nowDate); return m.getMonths(); }
From source file:femr.util.calculations.dateUtils.java
License:Open Source License
/** * Gets the integer age of a patient, then appends "YO" or "MO" * depending on if the patient age is in years(adult) or months(baby) * * @param born the birthdate of the patient * @return a string with the patient's age */// w w w. j a va 2s.com public static String getAge(Date born) { LocalDate birthdate = new LocalDate(born); LocalDate now = new LocalDate(); Months months = Months.monthsBetween(birthdate, now); int monthsInt = months.getMonths(); if (monthsInt < 24) return Integer.toString(monthsInt) + " MO"; else return Integer.toString(monthsInt / 12) + " YO"; }
From source file:femr.util.calculations.dateUtils.java
License:Open Source License
/** * Gets the patient's age in years. If the patient is an infant with less than 12 years of life, * 0 will be returned.//from w ww . j a v a 2 s.c o m * * @param born the birthdate of the patient * @return an Integer that represents the number of years the patient has been alive. Returns null * if an error occured OR if the patient does not have an age (just an age classification). */ public static Integer getYearsInteger(Date born) { if (born == null) { return null; } LocalDate birthdate = new LocalDate(born); LocalDate now = new LocalDate(); Integer age = 0; Months months = Months.monthsBetween(birthdate, now); int monthsInt = months.getMonths(); if (monthsInt >= 12) { double temp = Math.floor(monthsInt / 12); try { age = (int) Math.round(temp); } catch (Exception ex) { age = null; Logger.error("a patient's age could not be handled as an int"); } } return age; }
From source file:femr.util.calculations.dateUtils.java
License:Open Source License
/** * Gets the patient's age in months./*from www.ja v a 2s .c o m*/ * * @param born the birthdate of the patient * @return an Integer that represents the number of months the patient has been alive. Returns null * if an error occured OR if the patient does not have an age (just an age classification). */ public static Integer getMonthsInteger(Date born) { if (born == null) { return null; } LocalDate birthdate = new LocalDate(born); LocalDate now = new LocalDate(); Months months = Months.monthsBetween(birthdate, now); return months.getMonths(); }