Guest User

Conversor de tempo em Java

a guest
Aug 17th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. package br.com.mileniospvp.types;
  2.  
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. public class TimeProfile {
  6.    
  7.     private final long milliseconds;
  8.     private int seconds;
  9.     private int minutes;
  10.     private int hours;
  11.     private int days;
  12.     private int weeks;
  13.     private int months;
  14.     private int years;
  15.     public static String[] formats = new String[] { "ms", "seconds", "minutes", "hours", "days", "weeks", "months", "years" };
  16.        
  17.     public TimeProfile(long milliseconds) {
  18.         this.milliseconds = milliseconds;
  19.        
  20.         this.seconds = (int) TimeUnit.MILLISECONDS.toSeconds(this.milliseconds);
  21.         this.minutes = (int) TimeUnit.MILLISECONDS.toMinutes(this.milliseconds);
  22.         this.hours = (int) TimeUnit.MILLISECONDS.toHours(this.milliseconds);
  23.         this.days = (int) TimeUnit.MILLISECONDS.toDays(this.milliseconds);
  24.        
  25.         this.years = this.days / 365;
  26.         this.days = this.days % 365;
  27.         this.months = this.days / 30;
  28.         this.days = this.days % 30;
  29.         this.weeks = this.days / 7;
  30.         this.days = this.days % 7;
  31.     }
  32.  
  33.     public long getMilliseconds() {
  34.         return milliseconds;
  35.     }
  36.  
  37.     public int getSeconds() {
  38.         return seconds;
  39.     }
  40.  
  41.     public int getMinutes() {
  42.         return minutes;
  43.     }
  44.  
  45.     public int getHours() {
  46.         return hours;
  47.     }
  48.  
  49.     public int getDays() {
  50.         return days;
  51.     }
  52.  
  53.     public int getWeeks() {
  54.         return weeks;
  55.     }
  56.  
  57.     public int getMonths() {
  58.         return months;
  59.     }
  60.  
  61.     public int getYears() {
  62.         return years;
  63.     }
  64.  
  65.     public void setSeconds(int seconds) {
  66.         this.seconds = seconds;
  67.     }
  68.  
  69.     public void setMinutes(int minutes) {
  70.         this.minutes = minutes;
  71.     }
  72.  
  73.     public void setHours(int hours) {
  74.         this.hours = hours;
  75.     }
  76.  
  77.     public void setDays(int days) {
  78.         this.days = days;
  79.     }
  80.  
  81.     public void setWeeks(int weeks) {
  82.         this.weeks = weeks;
  83.     }
  84.  
  85.     public void setMonths(int months) {
  86.         this.months = months;
  87.     }
  88.  
  89.     public void setYears(int years) {
  90.         this.years = years;
  91.     }
  92.    
  93.     public String formatToMilliseconds() {
  94.         return milliseconds + " " + formated("milliseconds");
  95.     }
  96.    
  97.     public String formatToSeconds() {
  98.         return seconds + " " + formated("seconds");
  99.     }
  100.    
  101.     public String formatToMinutes() {
  102.         return minutes + " " + formated("minutes");
  103.     }
  104.    
  105.     public String formatToHours() {
  106.         return hours + " " + formated("hours");
  107.     }
  108.    
  109.     public String formatToDays() {
  110.         return days + " " + formated("days");
  111.     }
  112.    
  113.     public String formatToWeeks() {
  114.         return weeks + " " + formated("weeks");
  115.     }
  116.    
  117.     public String formatToMonths() {
  118.         return months + " " + formated("months");
  119.     }
  120.    
  121.     public String formatToYears() {
  122.         return years + " " + formated("years");
  123.     }
  124.      
  125.     public String format() {
  126.         return formatToYears() +
  127.                 " " + formatToMonths() +
  128.                 " " + formatToWeeks() +
  129.                 " " + formatToDays() +
  130.                 " " + formatToHours() +
  131.                 " " + formatToMinutes() +
  132.                 " " + formatToSeconds() +
  133.                 " (" + formatToMilliseconds() + ")";
  134.     }
  135.    
  136.     private String formated(String unit) {
  137.         if(unit.equalsIgnoreCase("milliseconds")) {
  138.             return formats[0];
  139.         } else if(unit.equalsIgnoreCase("seconds")) {
  140.             return formats[1];
  141.         } else if(unit.equalsIgnoreCase("minutes")) {
  142.             return formats[2];
  143.         } else if(unit.equalsIgnoreCase("hours")) {
  144.             return formats[3];
  145.         } else if(unit.equalsIgnoreCase("days")) {
  146.             return formats[4];
  147.         } else if(unit.equalsIgnoreCase("weeks")) {
  148.             return formats[5];
  149.         } else if(unit.equalsIgnoreCase("months")) {
  150.             return formats[6];
  151.         } else if(unit.equalsIgnoreCase("years")) {
  152.             return formats[7];
  153.         }
  154.        
  155.         return formats[0];
  156.     }
  157.    
  158.     public String toString() {
  159.         return "TimeProfile [milliseconds=" + this.milliseconds + ", seconds=" + this.seconds + ", minutes=" + this.minutes + ", hours=" + this.hours + ", days=" + this.days + ", weeks=" + this.weeks + ", months=" + this.months + ", years=" + this.years + "]";
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment