Util TimeUtils

Discussion in 'Resources' started by Caderape2, Apr 26, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Caderape2

    Hello, i made a class for manage the format of the time, and i tought it would be nice to share.

    Code:
    import java.sql.Timestamp;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    public class TimeUtils 
    {
    
        private long time = 0;
       
       
       
        public TimeUtils milliSecond(long milliSeconds)
        {
            time += TimeUnit.MILLISECONDS.toSeconds(milliSeconds);
            return this;
        }
       
        public TimeUtils second(long second)
        {
            this.time += second;
            return this;
        }
       
        public TimeUtils minuts(long minuts)
        {
            time += TimeUnit.MINUTES.toSeconds(minuts);
            return this;
        }
       
        public TimeUtils hours(long hours)
        {
            time += TimeUnit.HOURS.toSeconds(hours);
            return this;
        }
       
        public TimeUtils days(long days)
        {
            time += TimeUnit.DAYS.toSeconds(days);
            return this;
        }
       
        public TimeUtils month(long month)
        {
            return days(month * 30);
        }
       
        public TimeUtils timestamp(Timestamp stamp)
        {
            return second(TimeUnit.MILLISECONDS.toSeconds(stamp.getTime()));
        }   
       
        public TimeUtils now()
        {
            return second(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
        }
       
       
        /**
        * <h1> Get an instance from a shortcut </h1>
        * ex: "10h50s" = 10 hours and 50 seconds
        * <br>
        * s = second, m = minut, h = hour, d = day, M = month
        */
        public TimeUtils shortcut(String format)
        {
            Pattern p = Pattern.compile("[0-9]{1,3}[smhdM]");
            Matcher m = p.matcher(format);
           
            while (m.find())
            {
                String actual = m.group();
                actual = actual.substring(0, actual.length()-1);
               
                if (m.group().endsWith("s"))
                {               
                    if (isLong(actual)) time += Long.parseLong(actual);
                }
                else if (m.group().endsWith("m"))
                {               
                    if (isLong(actual)) time += TimeUnit.MINUTES.toSeconds(Long.parseLong(actual));
                }
                else if (m.group().endsWith("h"))
                {               
                    if (isLong(actual)) time += TimeUnit.HOURS.toSeconds(Long.parseLong(actual));
                }
                else if (m.group().endsWith("d"))
                {               
                    if (isLong(actual)) time += TimeUnit.DAYS.toSeconds(Long.parseLong(actual));
                }
                else if (m.group().endsWith("M"))
                {               
                    if (isLong(actual)) time += TimeUnit.DAYS.toSeconds(Long.parseLong(actual) * 30);
                }
            }
           
            if (isLong(format))
            {
                time += Long.parseLong(format);
            }
           
            return this;
        }
       
       
       
        public TimeUtils substract(long secondSubstract)
        {
            this.time -= secondSubstract;
            return this;
        }
       
       
       
       
        public void clear()
        {
            this.time = 0;
        }
       
       
       
       
        public long toMilliSecond()
        {
            return TimeUnit.SECONDS.toMillis(time);   
        }
       
        public long toSecond()
        {       
            return time;
        }
    
        public long toMinuts()
        {
            return TimeUnit.SECONDS.toMinutes(time);
        }
    
        public long toHours()
        {
            return TimeUnit.SECONDS.toHours(time);
        }
    
        public long toDays()
        {
            return TimeUnit.SECONDS.toDays(time);
        }
    
        public long toMonth()
        {
            return toDays() / 30;
        }
    
        public Timestamp toTimeStamp()
        {
            return new Timestamp(toMilliSecond());
        }
       
        /**
        * <h1> format the time in a date </h1>
        * years-month-days hours:minuts:seconds
        * <br>
        * @return the date in a string format
        */
        public String toDate()
        {
            String date = toTimeStamp().toString();
            date = date.substring(0, date.lastIndexOf("."));
            return date;
        }
       
        /**
        * <h1> format the time, in second, in a string </h1>
        * x month, x days, x hours, x minuts and x seconds
        * <br>
        * @return the time in a string format
        */
        public String toStringFormat()
        {
            long i = toSecond();
           
            long mois = 0;long j = 0;long h = 0; long m = 0;long s = 0;
           
            if (i >= 2592000)
            {
                mois = i/2592000;
                i -= mois * 2592000;
            }
            if (i >= 86400)
            {
                j = i/86400;
                i -= j * 86400;
            }
            if (i >= 3600)
            {
                h = i/3600;
                i -= h * 3600;
            }
            if (i >=60)
            {
                m = i/60;
                i -= m * 60;
            }
           
            s = i;
           
            String toSend = "";
           
            if (mois != 0) toSend += ", " + mois + " month";
            if (j != 0) toSend += ", " + j + " days";
            if (h != 0) toSend += ", " + h + " hours";
            if (m != 0) toSend += ", " + m + " minuts";
            if (s > 0) toSend += ", " + s + " seconds";
           
            if (toSend.startsWith(","))
            {
                toSend = toSend.substring(1);
                return toSend;
            }
           
            return toString();
        }
    
       
       
       
        @Override
        public String toString()
        {
            return String.valueOf(time);
        }   
       
        public TimeUtils fromString(String time) throws Exception
        {
            if (isLong(time))
            {
                this.time = Long.parseLong(time);
                return this;
            }
            throw new Exception("Invalid number");
        }
       
       
       
        /**
        * <h1> check if a shortcut is valid </h1>
        * 
        * @return true if the string is valid
        */
        public boolean isValidShortcut(String format)
        {
            Pattern p = Pattern.compile("[0-9]{1,3}[smhdM]");
            Matcher m = p.matcher(format);
           
            if (m.find()) return true;
           
            return isLong(format);
        }
       
       
    
       
        public boolean isLong(String c)
        {
            try {
                Long.parseLong(c);
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    }
    
    How to use (open)


    TimeUtils format = new TimeUtils().days(60).hours(40).second(5);

    format.toDays() : 61
    format.toStringFormat() : 2 month, 1 days, 16 hours, 0 minuts and 5 seconds
    format.shortcut("20h5m1s")
    format.toStringFormat() : 2 month, 2 days, 12 hours, 5 minuts and 6 seconds

    format.clear();

    format.now();
    format.toDate() : 2017-04-26 17:34:44
    format.days(5).hours(3);
    format.toDate() : 2017-05-01 20:34:44

     
    Last edited: Apr 26, 2017
  2. Offline

    Zombie_Striker

    @Caderape2
    Instead of doing those calculations, you could use Java's TimeUnit class:
     
  3. Offline

    Caderape2

Thread Status:
Not open for further replies.

Share This Page