Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. package pl2.tempo;
  2.  
  3. /**
  4.  * @author Pugs
  5.  */
  6. public class Tempo {
  7.  
  8.     private int h;
  9.     private int m;
  10.     private int s;
  11.     private static final int H_PREDEF = 0;
  12.     private static final int M_PREDEF = 0;
  13.     private static final int S_PREDEF = 0;
  14.  
  15.     public Tempo() {
  16.         h = H_PREDEF;
  17.         m = M_PREDEF;
  18.         s = S_PREDEF;
  19.     }
  20.  
  21.     public Tempo(int h, int m, int s) {
  22.         this.h = h;
  23.         this.m = m;
  24.         this.s = s;
  25.     }
  26.  
  27.     public int getSeconds() {
  28.         return s;
  29.     }
  30.  
  31.     public int getHours() {
  32.         return h;
  33.     }
  34.  
  35.     public int getMinutes() {
  36.         return m;
  37.     }
  38.  
  39.     public String toSring() {
  40.         return h + ":" + m + ":" + s;
  41.     }
  42.  
  43.     public String toSringTwelveHourFormat() {
  44.         if (h < 12) {
  45.             if (h == 0) {
  46.                 return 12 + ":" + m + ":" + s + " AM";
  47.             } else {
  48.                 return h + ":" + m + ":" + s + " AM";
  49.             }
  50.         } else {
  51.             if (h == 12) {
  52.                 return 12 + ":" + m + ":" + s + " PM";
  53.             } else {
  54.                 return (h - 12) + ":" + m + ":" + s + " PM";
  55.             }
  56.         }
  57.     }
  58.  
  59.     public void addSecond() {
  60.         int seconds = TempoToSeconds(this);
  61.         seconds++;
  62.         Tempo aux = SecondsToTempo(seconds);
  63.         h = aux.getHours();
  64.         m = aux.getMinutes();
  65.         s = aux.getSeconds();
  66.     }
  67.  
  68.     public boolean isMaior(Tempo t2) {
  69.         return TempoToSeconds(this) - TempoToSeconds(t2) > 0;
  70.     }
  71.  
  72.     public boolean isMaior(int h, int m, int s) {
  73.         return TempoToSeconds(this) - (h * 3600 + m * 60 + s) > 0;
  74.     }
  75.  
  76.     public int diferencaSegundos(Tempo t2) {
  77.         return TempoToSeconds(this) - TempoToSeconds(t2);
  78.     }
  79.  
  80.     public Tempo diferencaTempo(Tempo t2) {
  81.         int seconds = TempoToSeconds(this) - TempoToSeconds(t2);
  82.         return SecondsToTempo(seconds);
  83.     }
  84.  
  85.     public static int TempoToSeconds(Tempo t) {
  86.         return (t.getSeconds() + t.getMinutes() * 60 + t.getHours() * 3600);
  87.     }
  88.  
  89.     public static Tempo SecondsToTempo(int sec) {
  90.         int h = sec / 3600;
  91.         int m = (sec % 3600) / 60;
  92.         int s = ((sec % 3600) - (m * 60));
  93.         if(h>23) h=0;
  94.         Tempo Tempo = new Tempo(h, m, s);
  95.         return Tempo;
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement