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

Discussion in 'Java' started by abhilash.androiddeveloper@gmail.com, Oct 10, 2013.

  1. Guest

    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...
     
    , Oct 10, 2013
    #1
    1. Advertising

  2. On 2013-10-10 19:58, 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.
     
    Daniele Futtorovic, Oct 13, 2013
    #2
    1. Advertising

  3. Arne Vajhøj Guest

    On 10/10/2013 1:58 PM, 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
     
    Arne Vajhøj, Oct 20, 2013
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Imran Aziz
    Replies:
    4
    Views:
    1,543
    Imran Aziz
    Aug 11, 2005
  2. Marc Twain

    Display and display model

    Marc Twain, Jan 13, 2004, in forum: Java
    Replies:
    1
    Views:
    395
    Matt Humphrey
    Jan 13, 2004
  3. SG
    Replies:
    6
    Views:
    30,770
    ricuzzo
    Jan 15, 2008
  4. blank101
    Replies:
    2
    Views:
    552
    blank101
    Feb 5, 2005
  5. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    How to: display page, post back, display again

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Jul 14, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    3,052
    Walter Wang [MSFT]
    Jul 14, 2006
Loading...

Share This Page