List of usage examples for org.joda.time Period getHours
public int getHours()
From source file:com.reclabs.recomendar.common.helpers.types.DateHelper.java
License:Open Source License
/** * Dada una fecha de referencia y una fecha a comparar obtenemos la diferencia de la primera con la segunda, el * formato depender del rango de la diferencia, es decir, si hay menos de * un mes de diferencia obtendremos la diferencia en das. * @param referenceDate The date of reference * @param compareDate The date to compare * @return Diferencia entre las fechas//w w w.j a va 2s. c o m */ public static String getDifToCompareDateInUnitTimeLowRound(final Date referenceDate, final Date compareDate) { Period period = new Period(compareDate.getTime(), referenceDate.getTime(), PeriodType.yearMonthDayTime().withSecondsRemoved().withMillisRemoved()); if ((period.getYears() > 0) || (period.getMonths() > 0) || (period.getDays() > 0)) { return REDUCED_DATE_FORMAT.format(compareDate); } else if (period.getHours() > 0) { return period.getHours() + " Hora" + (period.getHours() > 1 ? "s" : ""); } else if (period.getMinutes() > 0) { return period.getMinutes() + " Minuto" + (period.getMinutes() > 1 ? "s" : ""); } else { return "Ahora"; } }
From source file:com.sap.dirigible.runtime.metrics.TimeUtils.java
License:Open Source License
private static DateTime dateTimeCeiling(DateTime dt, Period p) { if (p.getYears() != 0) { return dt.yearOfEra().roundCeilingCopy().minusYears(dt.getYearOfEra() % p.getYears()); } else if (p.getMonths() != 0) { return dt.monthOfYear().roundCeilingCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths()); } else if (p.getWeeks() != 0) { return dt.weekOfWeekyear().roundCeilingCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks()); } else if (p.getDays() != 0) { return dt.dayOfMonth().roundCeilingCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays()); } else if (p.getHours() != 0) { return dt.hourOfDay().roundCeilingCopy().minusHours(dt.getHourOfDay() % p.getHours()); } else if (p.getMinutes() != 0) { return dt.minuteOfHour().roundCeilingCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes()); } else if (p.getSeconds() != 0) { return dt.secondOfMinute().roundCeilingCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds()); }//www. java 2 s. c o m return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis()); }
From source file:com.spotify.helios.cli.Output.java
License:Apache License
public static String humanDuration(final Duration dur) { final Period p = dur.toPeriod().normalizedStandard(); if (dur.getStandardSeconds() == 0) { return "0 seconds"; } else if (dur.getStandardSeconds() < 60) { return format("%d second%s", p.getSeconds(), p.getSeconds() > 1 ? "s" : ""); } else if (dur.getStandardMinutes() < 60) { return format("%d minute%s", p.getMinutes(), p.getMinutes() > 1 ? "s" : ""); } else if (dur.getStandardHours() < 24) { return format("%d hour%s", p.getHours(), p.getHours() > 1 ? "s" : ""); } else {/*ww w. j av a 2 s.c o m*/ return format("%d day%s", dur.getStandardDays(), dur.getStandardDays() > 1 ? "s" : ""); } }
From source file:com.thinkbiganalytics.scheduler.util.TimerToCronExpression.java
License:Apache License
private static String getHoursCron(Period p) { Integer hrs = p.getHours(); Hours h = p.toStandardHours();/*from www . j a v a 2 s.c om*/ Integer hours = h.getHours(); String str = "0" + (hrs > 0 ? "/" + hrs : ""); if (hours > 24) { str = hrs + ""; } return str; }
From source file:control.EmprestimoController.java
private static TableModel UpdateTablemodel(TableModel tb) { LocalDateTime hoje = new LocalDateTime(System.currentTimeMillis()); for (int i = 0; i < tb.getRowCount(); i++) { String in = tb.getValueAt(i, 3).toString(); String out = tb.getValueAt(i, 4).toString(); LocalDateTime inicio = new LocalDateTime(in); LocalDateTime fim = new LocalDateTime(out); tb.setValueAt("" + inicio.getDayOfMonth() + "/" + inicio.getMonthOfYear() + "/" + inicio.getYear() + "", i, 3);/*from w ww . j a v a 2 s. c o m*/ tb.setValueAt("" + fim.getDayOfMonth() + "/" + fim.getMonthOfYear() + "/" + fim.getYear() + "", i, 4); Period p = new Period(hoje, fim); int dias = p.getDays(); int horas = p.getHours(); int month = p.getMonths(); //System.out.println(dias+":"+horas+" :: ENTRE AS DATAS "+fim.toString()+" :: "+hoje.toString()); if (dias < 1) { if (dias == 0) { tb.setValueAt("Atraso " + horas * -1 + "h", i, 5); if (horas > 0) tb.setValueAt("Restam " + horas + "h", i, 5); } else tb.setValueAt("Atraso " + dias * -1 + "d:" + horas * -1 + "h", i, 5); } /*else if (month >= 0) tb.setValueAt("Restam "+dias+"d:"+horas+"h", i, 5); else tb.setValueAt("Restam "+month+" meses", i, 5); */ } return tb; }
From source file:elw.web.FormatTool.java
License:Open Source License
public String formatDuration(final long timeMillis) { final Period period = new Period(); final Period periodNorm = period.plusMillis((int) Math.abs(timeMillis)).normalizedStandard(); if (periodNorm.getYears() > 0) { return lookupPeriodFormatter("y").print(periodNorm); } else if (periodNorm.getMonths() > 0) { return lookupPeriodFormatter("M").print(periodNorm); } else if (periodNorm.getDays() > 0) { return lookupPeriodFormatter("d").print(periodNorm); } else if (periodNorm.getHours() > 0) { return lookupPeriodFormatter("H").print(periodNorm); } else if (periodNorm.getMinutes() > 0) { return lookupPeriodFormatter("m").print(periodNorm); } else {//from w w w.j a v a2 s . co m // LATER sometimes durations less than one second occur return lookupPeriodFormatter("s").print(periodNorm); } }
From source file:eu.vranckaert.worktime.activities.reporting.ReportingExportActivity.java
License:Apache License
/** * Create a {@link Date} instance based on a calculated {@link Period} (aka time-duration) that Excel can handle to * display a date.<br/>/* w w w .java 2 s .c om*/ * Basically this means that a {@link Date} will be generated for time zone GMT+0 and the default date will be: * <b>31/11/1899 00:00:00,00000</b>. To this date the hours, minutes and seconds of the {@link Period} will be * added. * * @param period The period (or time duration) that will need to be converted to an Excel date. * @return The Excel date that will be x hours, x minutes and x seconds after 31/11/1899 00:00:00,00000. */ private Date getExcelTimeFromPeriod(Period period) { Log.d(getApplicationContext(), LOG_TAG, "Creating excel calendar date-time for period " + period); /* * Specifying the GMT+0 time zone for the Excel calendar fixes the issue that for each excel time displayed a * certain amount of hours (always the same amount of hours for all the calculated excel times) is missing. * The reason for this is that the Calendar takes the device default time zone (for the emulator being GMT+0 by * default). For your own device that could be GMT+01:00. However Excel only works with GMT+0 time zones, if * that's not the case it translates the time to the GMT+0 time zone and you loose one hour for every generated * Excel time. */ Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+00:00")); cal.set(Calendar.YEAR, 1899); cal.set(Calendar.MONTH, 11); cal.set(Calendar.DAY_OF_MONTH, 31); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); //cal.add(Calendar.HOUR, -36); Log.d(getApplicationContext(), LOG_TAG, "Default excel calendar before adding time: " + cal.getTime()); int hours = period.getHours(); int minutes = period.getMinutes(); int seconds = period.getSeconds(); if (hours > 0) { Log.d(getApplicationContext(), LOG_TAG, "About to add hours to the excel calendar..."); cal.add(Calendar.HOUR, hours); Log.d(getApplicationContext(), LOG_TAG, "Default excel calendar after adding hours: " + cal.getTime()); } if (minutes > 0) { Log.d(getApplicationContext(), LOG_TAG, "About to add minutes to the excel calendar..."); cal.set(Calendar.MINUTE, minutes); Log.d(getApplicationContext(), LOG_TAG, "Default excel calendar after adding minutes: " + cal.getTime()); } if (seconds > 0) { Log.d(getApplicationContext(), LOG_TAG, "About to add seconds to the excel calendar..."); cal.set(Calendar.SECOND, seconds); Log.d(getApplicationContext(), LOG_TAG, "Default excel calendar after adding seconds: " + cal.getTime()); } return cal.getTime(); }
From source file:factory.utils.Utils.java
public static String getHoursBetweenDays(Date startDate, Date endDate) throws Exception { if (startDate == null) { startDate = endDate;/*from ww w. j a v a 2 s . com*/ } DateTime dt1 = new DateTime(startDate.getTime()); DateTime dt2 = new DateTime(endDate.getTime()); Period p = new Period(dt1, dt2); long hours = p.getDays() * 24 + p.getHours(); long minutes = p.getMinutes(); String format = String.format("%%0%dd", 2); return String.format(format, hours) + ":" + String.format(format, minutes); }
From source file:gov.nih.nci.cadsr.cadsrpasswordchange.core.MainServlet.java
License:BSD License
/** * Method to detect/handle account lock condition. * /*from w w w . ja va 2 s . c o m*/ * @param username * @param password * @param session * @param req * @param resp * @param redictedUrl * @return account status * @throws Exception */ private String doValidateAccountStatus(String username, HttpSession session, HttpServletRequest req, HttpServletResponse resp, String redictedUrl) throws Exception { String retVal = ""; logger.debug("doValidateAccountStatus: entered"); //check locked state here String action = (String) session.getAttribute(Constants.ACTION_TOKEN); if (action != null && !action.equals(Constants.UNLOCK_TOKEN)) { //CADSRPASSW-29 connect(); PasswordChangeDAO dao = new PasswordChangeDAO(datasource); List arr = dao.getAccountStatus(username); if (arr == null || arr.size() != 2) { throw new Exception("Not able to check account status."); } retVal = (String) arr.get(PasswordChangeDAO.ACCOUNT_STATUS); //begin CADSRPASSW-55 - unlock manually as the "password_lock_time 60/1440" does not work // String status = (String)arr.get(PasswordChangeDAO.ACCOUNT_STATUS); Date lockedDate = (Date) arr.get(PasswordChangeDAO.LOCK_DATE); logger.debug("LockedDate [" + lockedDate + "] Status [" + retVal + "]"); Period period = null; boolean doUnlock = false; if (lockedDate != null && retVal != null && retVal.indexOf(Constants.LOCKED_STATUS) > -1) { DateTime now = new DateTime(); period = new Period(new DateTime(lockedDate), now); if (period.getHours() >= 1) { doUnlock = true; } } if (doUnlock) { connect(); PasswordChangeDAO dao1 = new PasswordChangeDAO(datasource); dao1.unlockAccount(username); logger.info("Over 1 hour, password lock release (" + period.getMinutes() + " minutes has passed)."); //logger.debug("Getting the account status again ..."); //retVal = (String)arr.get(PasswordChangeDAO.ACCOUNT_STATUS); retVal = Constants.OPEN_STATUS; logger.debug("Account status is [" + retVal + "] now"); } //end CADSRPASSW-55 - unlock manually as the "password_lock_time 60/1440" does not work else if (retVal != null && retVal.indexOf(Constants.LOCKED_STATUS) > -1) { String tmp = "NOT ABLE TO CALCULATE PERIOD DUE to NULL LOCKED_DATE"; if (period != null) { tmp = String.valueOf(period.getMinutes()) + " minutes has passed)."; } logger.info("Less than 1 hour, password lock stays (" + tmp + ")"); //CADSRPASSW-87 session.setAttribute(ERROR_MESSAGE_SESSION_ATTRIBUTE, Messages.getString("PasswordChangeHelper.103")); logger.debug("Redirecting to '" + redictedUrl + "'"); resp.sendRedirect(redictedUrl); } } logger.debug("doValidateAccountStatus: exiting with retVal [" + retVal + "] ..."); return retVal; }
From source file:gov.nih.nci.cadsr.cadsrpasswordchange.core.MainServlet.java
License:BSD License
private boolean isAnswerLockPeriodOver(String userID) throws Exception { boolean retVal = false; logger.debug("isAnswerLockExpired:entered"); connect();/*from www . j a va 2s . c om*/ PasswordChangeDAO dao = new PasswordChangeDAO(datasource); logger.debug("isAnswerLockExpired:before dao.findByPrimaryKey userid [" + userID + "]"); UserSecurityQuestion qna = dao.findByPrimaryKey(userID); logger.debug("isAnswerLockExpired:qna [" + qna + "]"); if (qna != null) { logger.debug("isAnswerLockExpired:qna not null [" + qna.toString() + "]"); if (qna.getDateModified() == null) { throw new Exception("Security questions date modified is NULL or empty."); } DateTime now = new DateTime(); logger.debug( "isAnswerLockExpired:last modified date for user '" + userID + "' is " + qna.getDateModified()); Period period = new Period(new DateTime(qna.getDateModified()), now); if (period.getHours() >= 1) { //CADSRPASSW-51 retVal = true; logger.info("isAnswerLockExpired:Over 1 hour for user '" + userID + "', answer limit count reset (" + period.getMinutes() + " minutes has passed)."); } else { logger.debug("isAnswerLockExpired:Not over 1 hour yet for user '" + userID + "', nothing is done (" + period.getMinutes() + " minutes has passed)."); } } logger.debug("isAnswerLockExpired:exiting ..."); return retVal; }