Java tutorial
/* * This file is part of CraftoPlugin, licensed under the MIT License (MIT). * * Copyright (c) 2017 CraftolutionDE <https://craftolution.de> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * Website: http://craftolution.de/ * Contact: support@craftolution.de */ package de.craftolution.craftoplugin4.utils; import com.google.common.collect.Lists; import de.craftolution.craftolibrary.Check; import java.time.Duration; import java.util.List; import java.util.Optional; public class DurationUtils { /** TODO: Documentation */ public static String parseDuration(final Duration duration) { Check.notNull(duration, "The duration cannot be null!"); final long totalSeconds = duration.getSeconds(); final long totalMinutes = duration.toMinutes(); final long totalHours = duration.toHours(); final long totalDays = duration.toDays(); final long totalWeeks = duration.toDays() / 7; log("totSecs: " + totalSeconds); log("totalMinutes: " + totalMinutes); log("totalHours: " + totalHours); log("totalDays: " + totalDays); log("totalWeeks: " + totalWeeks); long partWeeks = totalSeconds / 604800L; long partDays = totalSeconds % 604800L / 86400L; long partHours = totalSeconds % 86400L / 3600L; long partMinutes = totalSeconds % 3600L / 60L; long partSecs = totalSeconds % 60L; log("partWeeks: " + partWeeks); log("partDays: " + partDays); log("partHours: " + partHours); log("partMinutes: " + partMinutes); log("partSecs: " + partSecs); if (totalSeconds < 1) { return "ein paar Sekunden"; } List<String> parts = Lists.newArrayListWithCapacity(5); if (partWeeks > 1) { parts.add(partWeeks + " Wochen"); } if (partWeeks == 1) { partDays += 7; } if (partDays > 1) { parts.add(partDays + " Tage"); } if (partDays == 1) { partHours += 24; } if (partHours > 1) { parts.add(partHours + " Stunden"); } if (partHours == 1) { partMinutes += 60; } if (partMinutes > 1) { parts.add(partMinutes + " Minuten"); } if (partMinutes == 1) { partSecs += 60; } if (partSecs > 1) { parts.add(partSecs + " Sekunden"); } if (partSecs == 1) { parts.add("eine Sekunde"); } if (parts.size() > 1) { String firstPart = parts.get(0); String secondPart = parts.get(1); return firstPart + " und " + secondPart; } else { return parts.get(0); } } /** TODO: Documentation */ public static Optional<Duration> parseDuration(final String string) { try { Duration dur = Duration.ofMillis(TickDuration.of(string).toMillis()); return Optional.of(dur); } catch (IllegalArgumentException e) { return Optional.empty(); } } /** * TODO: Documentation * * Pareses the given duration with variables: %h and %m * @param duration * @param format * @return */ public static String parseDuration(final Duration duration, final String format) { final long hours = duration.getSeconds() / 60; final long minutes = duration.getSeconds() - hours * 60; return format.replaceFirst("%h", String.valueOf(hours)).replaceFirst("%m", String.valueOf(minutes)); } /** TODO: Documentation */ public static String parseDurationWithHoursAndMinutes(final Duration duration) { final long stunden = duration.toMinutes() / 60; final long minuten = duration.toMinutes() - stunden * 60; return stunden + " Stunden und " + minuten + " Minuten."; } private static void log(String msg) { //System.out.println(msg); } }