Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #26295 > unrolled thread
| Started by | abhilash.androiddeveloper@gmail.com |
|---|---|
| First post | 2013-10-10 10:58 -0700 |
| Last post | 2013-10-19 22:23 -0400 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
How to display in HH:MM:SS... ? abhilash.androiddeveloper@gmail.com - 2013-10-10 10:58 -0700
Re: How to display in HH:MM:SS... ? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-10-13 09:57 +0200
Re: How to display in HH:MM:SS... ? Arne Vajhøj <arne@vajhoej.dk> - 2013-10-19 22:23 -0400
| From | abhilash.androiddeveloper@gmail.com |
|---|---|
| Date | 2013-10-10 10:58 -0700 |
| Subject | How to display in HH:MM:SS... ? |
| Message-ID | <c40d8b94-ebd9-43e4-be96-4bfbb8bb69eb@googlegroups.com> |
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...
[toc] | [next] | [standalone]
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
|---|---|
| Date | 2013-10-13 09:57 +0200 |
| Message-ID | <l3djq1$3s5$1@dont-email.me> |
| In reply to | #26295 |
On 2013-10-10 19:58, abhilash.androiddeveloper@gmail.com allegedly wrote:
> 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)
--
DF.
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2013-10-19 22:23 -0400 |
| Message-ID | <52633eb4$0$305$14726298@news.sunsite.dk> |
| In reply to | #26295 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web