Re: How to display in HH:MM:SS... ?
On 10/10/2013 1:58 PM, abhilash.androiddeveloper@gmail.com wrote:
I am having two dates with times for example:
String dateStart = "01/14/2012 09:30:55";
String dateStop = "01/15/2012 18:25:54";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d1 = null;
Date d2 = null;
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
When I subtract: d2 - d1, I want to show the ela[sed time in between the above two dates in the format in HH:MM:SS: like i want the output something like:
33:05:01. How can I get in that format..? If anybody is having any idea please help me out...
There are several ways.
Demo:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.TimeZone;
import org.apache.commons.lang3.time.DurationFormatUtils;
public class TimeSub {
private static final int MS_PER_D = 24 * 60 * 60 * 1000;
private static final int MS_PER_H = 60 * 60 * 1000;
private static final int MS_PER_M = 60 * 1000;
private static final int MS_PER_S = 1000;
public static String theDIYWay(Date d1, Date d2) {
long delta = d2.getTime() - d1.getTime();
long h = delta / MS_PER_H;
long m = (delta % MS_PER_H) / MS_PER_M;
long s = (delta % MS_PER_M) / MS_PER_S;
return String.format("%02d:%02d:%02d", h, m , s);
}
public static String theHackishWay(Date d1, Date d2) {
long delta = d2.getTime() - d1.getTime();
if(delta >= MS_PER_D) throw new
IllegalArgumentException("Period not less than 1 day");
DateFormat format = new SimpleDateFormat("HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
return format.format(new Date(delta));
}
public static String theJakartaWay(Date d1, Date d2) {
long delta = d2.getTime() - d1.getTime();
return DurationFormatUtils.formatDuration(delta, "HH:mm:ss");
}
public static String theJava8Way(LocalDateTime d1, LocalDateTime d2) {
Duration dur = Duration.ofMillis(ChronoUnit.MILLIS.between(d1,
d2));
long delta = dur.toMillis();
long h = delta / MS_PER_H;
long m = (delta % MS_PER_H) / MS_PER_M;
long s = (delta % MS_PER_M) / MS_PER_S;
return String.format("%02d:%02d:%02d", h, m , s);
}
public static void test(String dateStart, String dateStop) throws
ParseException {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date start = df.parse(dateStart);
Date stop = df.parse(dateStop);
System.out.println(theDIYWay(start, stop));
try {
System.out.println(theHackishWay(start, stop));
} catch(IllegalArgumentException ex) {
System.out.println("**** Error ****");
}
System.out.println(theJakartaWay(start, stop));
DateTimeFormatter df2 = DateTimeFormatter.ofPattern("MM/dd/yyyy
HH:mm:ss");
LocalDateTime start2 = df2.parse(dateStart, LocalDateTime::from);
LocalDateTime stop2 = df2.parse(dateStop, LocalDateTime::from);
System.out.println(theJava8Way(start2, stop2));
}
public static void main(String[] args) throws ParseException {
test("01/14/2012 09:30:55", "01/14/2012 18:25:54");
test("01/14/2012 09:30:55", "01/15/2012 18:25:54");
}
}
Arne