/** * @author lautturi.com * Java example: how to compare two times in java */ import java.util.*; import java.text.ParseException; import java.text.SimpleDateFormat; public class Lautturi { public static void main(String[] args) throws ParseException { String startTime = "9:19"; String endTime = "9:20"; SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); Date d1 = sdf.parse(startTime); Date d2 = sdf.parse(endTime); long elapsed = d2.getTime() - d1.getTime(); System.out.println(elapsed); } }
output:
60second = 60000 millisecond
60000