Exemplo n.º 1
0
  public static String getTimeUntilNow(String time) {
    LocalDateTime now = LocalDateTime.now();
    now = now.plusMinutes(1);

    int hours = Integer.parseInt(time.split(":")[0]);
    int minutes = Integer.parseInt(time.split(":")[1]);

    LocalDateTime tocheck =
        LocalDateTime.of(
            now.getYear(),
            now.getMonth(),
            now.getDayOfMonth(),
            hours,
            minutes,
            now.getSecond(),
            now.getNano());

    if (tocheck.isBefore(now)) {
      tocheck = tocheck.plusDays(1);
    }

    Duration remaining = Duration.between(now, tocheck);

    int rh = (int) remaining.toHours();
    int rm = (int) remaining.toMinutes();
    rm %= 60;

    int ht = rh / 10;
    int hs = rh % 10;
    int mt = rm / 10;
    int ms = rm % 10;

    return ht + "" + hs + ":" + mt + "" + ms;
  }
 public static void main(String[] args) {
   int startTime, endTime;
   final int TIMES = 200_000;
   final int FACTOR = 1_000_000;
   int x;
   StringBuilder string1 = new StringBuilder("");
   StringBuilder string2 = new StringBuilder(TIMES * 4);
   LocalDateTime now;
   now = LocalDateTime.now();
   startTime = now.getNano();
   for (x = 0; x < TIMES; ++x) string1.append("Java");
   now = LocalDateTime.now();
   endTime = now.getNano();
   System.out.println(
       "Time with empty StringBuilder: " + ((endTime - startTime) / FACTOR + " milliseconds"));
   now = LocalDateTime.now();
   startTime = now.getNano();
   for (x = 0; x < TIMES; ++x) string2.append("Java");
   now = LocalDateTime.now();
   endTime = now.getNano();
   System.out.println(
       "Time with empty StringBuilder: " + ((endTime - startTime) / FACTOR + " milliseconds"));
 }