package it.acxent.videoj; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; public class Timer { private Timestamp tStart = null; private Timestamp tStop = null; public void start() { this.tStart = new Timestamp(System.currentTimeMillis()); this.tStop = null; } public void stop() { this.tStop = new Timestamp(System.currentTimeMillis()); } public String getDurataSec() { return String.valueOf(getDurataSecLong()); } public long getDurataSecLong() { long secs = 0L; if (this.tStop == null) { secs = getTimeDiff(new Time(getTStart().getTime()), new Time(System.currentTimeMillis())); } else { secs = getTimeDiff(new Time(getTStart().getTime()), new Time(this.tStop.getTime())); } return secs; } public String getDurataHourMin() { String temp = secToTempoHourMin(getDurataSecLong()); return temp; } public static String secToTempoHourMin(long sec) { long h = sec / 3600L; long min = (sec - h * 3600L) / 60L; long secF = sec - h * 3600L - min * 60L; return "" + h + " h " + h + " min " + min + " sec"; } public Timestamp getTStart() { if (this.tStart == null) this.tStart = new Timestamp(System.currentTimeMillis()); return this.tStart; } public Timestamp getTStop() { return this.tStop; } public String getDurata() { String temp = "\n\n--------------------------------------------------\nPartenza: " + String.valueOf(getTStart()) + "\nFine: " + String.valueOf(this.tStop) + "\nDurata: " + secToTempoHourMin(getDurataSecLong()); return temp; } public long getDurataMilliSec() { long msecs = 0L; if (this.tStop == null) { msecs = System.currentTimeMillis() - getTStart().getTime(); } else { msecs = this.tStop.getTime() - getTStart().getTime(); } return msecs; } public static long getTimeDiff(Time time1, Time time2) { long c1, c2; if (time1 == null) { c1 = 0L; } else { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time1.getTime()); c1 = (long)(cal.get(11) * 3600 + cal.get(12) * 60 + cal.get(13)); } if (time2 == null) { c2 = 0L; } else { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time2.getTime()); c2 = (long)(cal.get(11) * 3600 + cal.get(12) * 60 + cal.get(13)); } long difInSecs = c2 - c1; return difInSecs; } }