Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.AsylumDevs.AsylumMines.Utils;
- import java.util.concurrent.TimeUnit;
- public class TimeAPI {
- private static long DAYS_IN_WEEK = 7L;
- private static long DAYS_IN_MONTH = 30L;
- private static long DAYS_IN_YEAR = 365L;
- private static long DAYS_IN_DECADE = 3650L;
- private long seconds;
- public TimeAPI(String time) {
- reparse(time);
- }
- public TimeAPI(int seconds) {
- this.seconds = seconds;
- }
- public TimeAPI reparse(String time) {
- this.seconds = 0L;
- TimeScanner scanner = new TimeScanner(time.replace(" ", "").replace("and", "").replace(",", "").toLowerCase());
- while (scanner.hasNext()) {
- long next = scanner.nextLong();
- switch (scanner.nextString()) {
- case "s":
- case "sec":
- case "secs":
- case "second":
- case "seconds":
- this.seconds += next;
- break;
- case "m":
- case "min":
- case "mins":
- case "minute":
- case "minutes":
- this.seconds += TimeUnit.MINUTES.toSeconds(next);
- break;
- case "h":
- case "hr":
- case "hrs":
- case "hour":
- case "hours":
- this.seconds += TimeUnit.HOURS.toSeconds(next);
- break;
- case "d":
- case "dy":
- case "dys":
- case "day":
- case "days":
- this.seconds += TimeUnit.DAYS.toSeconds(next);
- break;
- case "w":
- case "week":
- case "weeks":
- this.seconds += TimeUnit.DAYS.toSeconds(next * DAYS_IN_WEEK);
- break;
- case "mo":
- case "mon":
- case "mnth":
- case "month":
- case "months":
- this.seconds += TimeUnit.DAYS.toSeconds(next * DAYS_IN_MONTH);
- break;
- case "y":
- case "yr":
- case "yrs":
- case "year":
- case "years":
- this.seconds += TimeUnit.DAYS.toSeconds(next * DAYS_IN_YEAR);
- break;
- default:
- throw new IllegalArgumentException();
- }
- }
- return this;
- }
- public double getNanoseconds() {
- return TimeUnit.SECONDS.toNanos(this.seconds);
- }
- public double getMicroseconds() {
- return TimeUnit.SECONDS.toMicros(this.seconds);
- }
- public double getMilliseconds() {
- return TimeUnit.SECONDS.toMillis(this.seconds);
- }
- public long getSeconds() {
- return this.seconds;
- }
- public long getMinutes() {
- return (long) (this.seconds / 60.0D);
- }
- public double getHours() {
- return this.seconds / 3600.0D;
- }
- public double getDays() {
- return this.seconds / 86400.0D;
- }
- public double getWeeks() {
- return getDays() / DAYS_IN_WEEK;
- }
- public double getMonths() {
- return getDays() / DAYS_IN_MONTH;
- }
- public double getYears() {
- return getDays() / DAYS_IN_YEAR;
- }
- public double getDecades() {
- return getDays() / DAYS_IN_DECADE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement