Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl2.tempo;
- /**
- * @author Pugs
- */
- public class Tempo {
- private int h;
- private int m;
- private int s;
- private static final int H_PREDEF = 0;
- private static final int M_PREDEF = 0;
- private static final int S_PREDEF = 0;
- public Tempo() {
- h = H_PREDEF;
- m = M_PREDEF;
- s = S_PREDEF;
- }
- public Tempo(int h, int m, int s) {
- this.h = h;
- this.m = m;
- this.s = s;
- }
- public int getSeconds() {
- return s;
- }
- public int getHours() {
- return h;
- }
- public int getMinutes() {
- return m;
- }
- public String toSring() {
- return h + ":" + m + ":" + s;
- }
- public String toSringTwelveHourFormat() {
- if (h < 12) {
- if (h == 0) {
- return 12 + ":" + m + ":" + s + " AM";
- } else {
- return h + ":" + m + ":" + s + " AM";
- }
- } else {
- if (h == 12) {
- return 12 + ":" + m + ":" + s + " PM";
- } else {
- return (h - 12) + ":" + m + ":" + s + " PM";
- }
- }
- }
- public void addSecond() {
- int seconds = TempoToSeconds(this);
- seconds++;
- Tempo aux = SecondsToTempo(seconds);
- h = aux.getHours();
- m = aux.getMinutes();
- s = aux.getSeconds();
- }
- public boolean isMaior(Tempo t2) {
- return TempoToSeconds(this) - TempoToSeconds(t2) > 0;
- }
- public boolean isMaior(int h, int m, int s) {
- return TempoToSeconds(this) - (h * 3600 + m * 60 + s) > 0;
- }
- public int diferencaSegundos(Tempo t2) {
- return TempoToSeconds(this) - TempoToSeconds(t2);
- }
- public Tempo diferencaTempo(Tempo t2) {
- int seconds = TempoToSeconds(this) - TempoToSeconds(t2);
- return SecondsToTempo(seconds);
- }
- public static int TempoToSeconds(Tempo t) {
- return (t.getSeconds() + t.getMinutes() * 60 + t.getHours() * 3600);
- }
- public static Tempo SecondsToTempo(int sec) {
- int h = sec / 3600;
- int m = (sec % 3600) / 60;
- int s = ((sec % 3600) - (m * 60));
- if(h>23) h=0;
- Tempo Tempo = new Tempo(h, m, s);
- return Tempo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement