How to display in HH:MM:SS... ?

  • Thread starter abhilash.androiddeveloper
  • Start date
A

abhilash.androiddeveloper

Hi,

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...
 
D

Daniele Futtorovic

Hi,

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...

public static String computeDiffTimeAsString( Date d1, Date d2 ){
if( d1.after( d2 ) ) throw new IllegalArgumentException(
String.format( "Start date %tc is after end date %tc", d1, d2 ) );

long secs = (d2.getTime() - d1.getTime()) / 1000;
return String.format( "%2d:%2d:%2d", secs / 3600, secs % 3600 / 60,
secs % 60 );
}

(Caveat: neither compiled nor tested)
 
A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top