List of usage examples for java.time Duration getSeconds
public long getSeconds()
From source file:Main.java
public static void main(String[] args) { Instant firstInstant = Instant.ofEpochSecond(1294881180); Instant secondInstant = Instant.ofEpochSecond(1294708260); Duration between = Duration.between(firstInstant, secondInstant); System.out.println(between);// w ww. ja va 2 s .c om long seconds = between.getSeconds(); long absoluteResult = between.abs().toMinutes(); }
From source file:mServer.tool.MserverDatumZeit.java
public static long getSecondsUntilNextDay() { // now/*from www .ja va2 s . com*/ LocalDateTime now = LocalDateTime.now(); // tomorrow 0:00 LocalDateTime future = LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT).plusDays(1L); Duration duration = Duration.between(now, future); return duration.getSeconds(); }
From source file:de.bytefish.fcmjava.client.interceptors.response.utils.RetryHeaderUtils.java
private static boolean tryGetFromDate(String dateAsString, OutParameter<Duration> result) { // Try to convert the String to a RFC1123-compliant Zoned DateTime OutParameter<ZonedDateTime> resultDate = new OutParameter<ZonedDateTime>(); if (!tryToConvertToDate(dateAsString, resultDate)) { return false; }/* ww w .j a va2s. c o m*/ // Get the UTC Now DateTime and the Retry DateTime in UTC Time Zone: ZonedDateTime utcNowDateTime = DateUtils.getUtcNow(); ZonedDateTime nextRetryDateTime = resultDate.get().withZoneSameInstant(ZoneOffset.UTC); // Calculate Duration between both as the Retry Delay: Duration durationToNextRetryTime = Duration.between(utcNowDateTime, nextRetryDateTime); // Negative Retry Delays should not be allowed: if (durationToNextRetryTime.getSeconds() < 0) { durationToNextRetryTime = Duration.ofSeconds(0); } // Set it as Result: result.set(durationToNextRetryTime); // And return success: return true; }
From source file:de.bytefish.fcmjava.client.http.apache.utils.RetryHeaderUtils.java
private static boolean tryGetFromDate(String dateAsString, OutParameter<Duration> result) { // Try to convert the String to a RFC1123-compliant Zoned DateTime OutParameter<ZonedDateTime> resultDate = new OutParameter<>(); if (!tryToConvertToDate(dateAsString, resultDate)) { return false; }/*from w w w . j ava 2 s . c o m*/ // Get the UTC Now DateTime and the Retry DateTime in UTC Time Zone: ZonedDateTime utcNowDateTime = DateUtils.getUtcNow(); ZonedDateTime nextRetryDateTime = resultDate.get().withZoneSameInstant(ZoneOffset.UTC); // Calculate Duration between both as the Retry Delay: Duration durationToNextRetryTime = Duration.between(utcNowDateTime, nextRetryDateTime); // Negative Retry Delays should not be allowed: if (durationToNextRetryTime.getSeconds() < 0) { durationToNextRetryTime = Duration.ofSeconds(0); } // Set it as Result: result.set(durationToNextRetryTime); // And return success: return true; }
From source file:com.querydsl.webhooks.GithubReviewWindow.java
@VisibleForTesting protected static String makeHumanReadable(Duration duration) { StringBuilder output = new StringBuilder(); duration = truncateAndAppend(duration, duration.toDays(), ChronoUnit.DAYS, "day", output); duration = truncateAndAppend(duration, duration.toHours(), ChronoUnit.HOURS, "hour", output); duration = truncateAndAppend(duration, duration.toMinutes(), ChronoUnit.MINUTES, "minute", output); duration = truncateAndAppend(duration, duration.getSeconds(), ChronoUnit.SECONDS, "second", output); return output.toString().trim(); }
From source file:me.rojo8399.placeholderapi.impl.utils.TypeUtils.java
public static String formatDuration(Duration duration) { long seconds = duration.getSeconds(); long absSeconds = Math.abs(seconds); String positive = String.format("%d h %d m %d s", absSeconds / 3600, (absSeconds % 3600) / 60, absSeconds % 60);/*from w w w.j av a2s. c o m*/ return seconds < 0 ? "-" + positive : positive; }
From source file:io.appium.java_client.MobileCommand.java
/** * This method forms a {@link java.util.Map} of parameters for the * device locking./* w w w.ja va 2s . c o m*/ * * @param duration for how long to lock the screen for. Minimum time resolution is one second * @return a key-value pair. The key is the command name. The value is a * {@link java.util.Map} command arguments. */ public static Map.Entry<String, Map<String, ?>> lockDeviceCommand(Duration duration) { return new AbstractMap.SimpleEntry<>(LOCK, prepareArguments("seconds", duration.getSeconds())); }
From source file:com.example.IdGenerator.java
public long generateLong() { final LocalDateTime now = LocalDateTime.now(clock); final Duration duration = Duration.between(BASE, now); final int high = (int) duration.getSeconds(); final int low = duration.getNano(); final byte[] hbs = ByteBuffer.allocate(4).putInt(high).array(); final byte[] lbs = ByteBuffer.allocate(4).putInt(low).array(); final byte[] bytes = new byte[8]; System.arraycopy(hbs, 0, bytes, 0, 4); System.arraycopy(lbs, 0, bytes, 4, 4); final ByteBuffer buffer = ByteBuffer.allocate(8).put(bytes, 0, 8); buffer.flip();//from www. j ava2 s . c o m return buffer.getLong(); }
From source file:tibano.service.ParkService.java
@RequestMapping(path = "/getPaymentInfo") PaymentInfo getPaymentInfo(@RequestParam(name = "areaId") Long areaId, @RequestParam(name = "licensePlate") String licensePlate) { ParkingTransaction pt = ptRepository.findOpenTransactionByAreaAndLicensePlate(areaId, licensePlate); if (pt != null) { Duration duration = Duration.between(pt.getStart(), LocalDateTime.now()); Double amount = duration.getSeconds() * SEC_TARIF; BigDecimal bd = new BigDecimal(amount); bd = bd.setScale(2, RoundingMode.HALF_UP); Integer loyaltyPoints = 5 + Integer.valueOf(Double.valueOf(bd.doubleValue()).intValue()); return new PaymentInfo(pt.getEnd(), amount, duration, loyaltyPoints); }/*from w w w . j av a2s. com*/ return new PaymentInfo(null, Double.valueOf(0), Duration.ZERO, Integer.valueOf(0)); }
From source file:com.peso.Workout.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from ww w.ja va 2s.co m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean bError = false; String mensajeError = ""; String nombre = ""; String peso = ""; String workout = ""; LocalDateTime ultimaLlamada = null; LocalDateTime ahora = null; LocalDateTime lesionado = null; //Errores if (request.getSession().getAttribute("nombre") == null) { mensajeError = "no hay nombre resgistrado"; bError = true; } else { workout = request.getParameter("workout"); if (workout == null) { mensajeError = "falta el parametro workout"; bError = true; } else if (!StringUtils.isNumeric(workout)) { mensajeError = "workout debe de ser numrico"; bError = true; } } if (!bError) { //Registrar el usuario nombre = (String) request.getSession().getAttribute("nombre"); peso = (String) request.getSession().getAttribute("peso"); ahora = LocalDateTime.now(); ultimaLlamada = (LocalDateTime) request.getSession().getAttribute("ultimaLlamada"); if (ultimaLlamada == null) { ultimaLlamada = LocalDateTime.now(); } //engordar cada 10 segundos, tiempo entre llamadas. Duration segundos = Duration.between(ultimaLlamada, ahora); int iPeso = NumberUtils.toInt(peso); iPeso += (segundos.getSeconds() / 10); //mirar si lesion lesionado = (LocalDateTime) request.getSession().getAttribute("lesionado"); Duration segundosLesion = null; // si hay lesion esperar tiempo if (lesionado != null) { segundosLesion = Duration.between(lesionado, ahora); if (segundosLesion.getSeconds() >= 30) { // se le quita la lesion request.getSession().setAttribute("lesionado", null); } request.setAttribute("lesionado", (30 - segundosLesion.getSeconds()) + ""); } else//si han pasado menos de 5 seg se lesiona { if (segundos.getSeconds() <= 5) { request.getSession().setAttribute("lesionado", ahora); } else { //si no la hay adelgaza el tiempo requerido. int kilosAdelgazar = (NumberUtils.toInt(workout) / 30); kilosAdelgazar *= (int) (0.2 * iPeso); iPeso -= kilosAdelgazar; } } peso = iPeso + ""; request.getSession().setAttribute("peso", peso); request.getSession().setAttribute("ultimaLlamada", ahora); request.setAttribute("nombre", nombre); request.setAttribute("peso", peso); request.getRequestDispatcher("/workout.jsp").forward(request, response); } else { request.setAttribute(Constantes.ATRIBUTO_ERROR, mensajeError); request.getRequestDispatcher(Constantes.PAGINA_ERROR).forward(request, response); } }