Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8824753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:42:44+00:00 2026-06-14T06:42:44+00:00

I am on a project to capture the interval time in hh:mm. I have

  • 0

I am on a project to capture the interval time in hh:mm.
I have 2 buttons named btnTimeOut & btnTimeIn both capturing the system time when clicked.
The requirement is to get the interval between the btnTimeOut & btnTime in hh:mm, etc. 12:30 – 10:00 = 02:30 (hh:mm).

Currently I used the following codes for the interval but it returns as minutes, etc. 12:30 – 10:00 = 150 minutes.

  String timeOut = lblTimeOut.getText();
  String timeIn = lblTimeIn2.getText();

  SimpleDateFormat format = new SimpleDateFormat("hh:mm");

  Date d1 = null;
  Date d2 = null;

  try {
      d1 = format.parse(timeOut);
      d2 = format.parse(timeIn);
  } 
  catch (Exception e){
      e.printStackTrace();
  }

  long diff = d2.getTime() - d1.getTime();
  long diffMinutes = diff / (60 * 1000);         
  long diffHours = diff / (60 * 60 * 1000);  

  lblSurface.setText(String.valueOf(diffMinutes)); 

How to get the duration in the form hh:mm?

I used Joda time and return with Invalid format: “12:19” is malformed at “:19”.
As for my other buttons which trigger the display time.

DateFormat timeFormat = new SimpleDateFormat("hh:mm");
Date date = new Date();  
String time = timeFormat.format(date);  
lblTimeIn2.setText(time);

Timer timer = new Timer(1000, timerListener);  
    // to make sure it doesn't wait one second at the start  
timer.setInitialDelay(0);  
timer.start();   
}         

I’ve no idea what is wrong, do I need to use joda time for displaying time for my other label too?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:42:45+00:00Added an answer on June 14, 2026 at 6:42 am

    I, personally, would use JodaTime as it takes into account things like difference between days (ie the difference between 23:30-02:30) and has nice inbuilt formatters

    public class TestJodaTime {
    
        public static void main(String[] args) {
    
            DateTime start = new DateTime(2012, 11, 11, 23, 30, 0, 0);
            DateTime end = new DateTime(2012, 11, 12, 1, 30, 0, 0);
            Interval interval = new Interval(start, end);
            Period toPeriod = interval.toPeriod();
    
            PeriodFormatter dateFormat = new PeriodFormatterBuilder()
                            .printZeroAlways().minimumPrintedDigits(2)
                .appendHours().minimumPrintedDigits(2)
                .appendSeparator(":")
                .appendMinutes().minimumPrintedDigits(2)
                .toFormatter();        
            System.out.println(toPeriod.toString(dateFormat));
        }
    }
    

    Which will output 02:00

    Extended example

    enter image description here

    public class TestJodaTime {
    
        public static void main(String[] args) {
            new TestJodaTime();
        }
    
        public TestJodaTime() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JodaPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class JodaPane extends JPanel {
    
            private JTextField startHour;
            private JTextField startMin;
            private JTextField endHour;
            private JTextField endMin;
            private JButton diffButton;
            private JLabel lblDiff;
            private JButton markStart;
            private JButton markEnd;
            private Timer timer;
            private JLabel realTime;
    
            public JodaPane() {
    
                markStart = new JButton("Mark");
                markEnd = new JButton("Mark");
    
                startHour = new JTextField(2);
                startMin = new JTextField(2);
                endHour = new JTextField(2);
                endMin = new JTextField(2);
                diffButton = new JButton("=");
                lblDiff = new JLabel("00:00");
                realTime = new JLabel("00:00.00");
    
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.anchor = GridBagConstraints.WEST;
                add(new JLabel("From"), gbc);
                gbc.gridx++;
                add(startHour, gbc);
                gbc.gridx++;
                add(new JLabel(":"), gbc);
                gbc.gridx++;
                add(startMin, gbc);
                gbc.gridx++;
                add(markStart, gbc);
                gbc.gridx++;
                add(new JLabel(" to "), gbc);
                gbc.gridx++;
                add(endHour, gbc);
                gbc.gridx++;
                add(new JLabel(":"), gbc);
                gbc.gridx++;
                add(endMin, gbc);
                gbc.gridx++;
                add(markEnd, gbc);
                gbc.gridx++;
                add(diffButton, gbc);
                gbc.gridx++;
                add(lblDiff, gbc);
    
                gbc.gridy++;
                add(realTime, gbc);
    
                diffButton.addActionListener(new ActionListener() {
                    public boolean isValid(JTextField field) {
                        return field.getText() != null && field.getText().length() > 0;
                    }
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        if (isValid(startHour) && isValid(startMin)
                                        && isValid(endHour) && isValid(endMin)) {
                            int hourStart = Integer.parseInt(startHour.getText());
                            int minStart = Integer.parseInt(startMin.getText());
                            int hourEnd = Integer.parseInt(endHour.getText());
                            int minEnd = Integer.parseInt(endMin.getText());
    
                            String prefix = "";
                            if (hourEnd < hourStart) {
                                int tmp = hourStart;
                                hourStart = hourEnd;
                                hourEnd = tmp;
                                prefix = "-";
                            }
    
                            System.out.println("Start = " + hourStart + ":" + minStart);
                            System.out.println("End = " + hourEnd + ":" + minEnd);
    
                            DateTime start = new DateTime(0, 1, 1, hourStart, minStart, 0, 0);
                            DateTime end = new DateTime(0, 1, 1, hourEnd, minEnd, 0, 0);
                            Interval interval = new Interval(start, end);
                            Period toPeriod = interval.toPeriod();
    
                            PeriodFormatter dateFormat = new PeriodFormatterBuilder()
                                            .printZeroAlways().minimumPrintedDigits(2)
                                            .appendHours().minimumPrintedDigits(2)
                                            .appendSeparator(":")
                                            .appendMinutes().minimumPrintedDigits(2)
                                            .toFormatter();
                            lblDiff.setText(prefix + dateFormat.print(toPeriod));
                        }
                    }
    
                });
    
                markStart.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        Calendar cal = Calendar.getInstance();
                        startHour.setText(Integer.toString(cal.get(Calendar.HOUR_OF_DAY)));
                        startMin.setText(Integer.toString(cal.get(Calendar.MINUTE)));
                        diffButton.doClick();
                    }
    
                });
                markEnd.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        Calendar cal = Calendar.getInstance();
                        endHour.setText(Integer.toString(cal.get(Calendar.HOUR_OF_DAY)));
                        endMin.setText(Integer.toString(cal.get(Calendar.MINUTE)));
                        diffButton.doClick();
                    }
    
                });
    
                timer = new Timer(500, new ActionListener() {
                    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm.ss");
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        realTime.setText(sdf.format(new Date()));
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
    
    
            }
    
        }
    
    }
    

    Your questions a little vague, so I’ve done a wide example. Mark, basically auto fills the fields with the current time.

    There is little validation 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project requirement to render HTML and capture the rendered image as
I have a Camtasia project named aaa.camrec and want to capture an image in
I have a RFID project, and wants the system to detect the card on
What tool I can use for .Net/C# project to capture run-time dependencies between classes
I have an interesting project wherein I need to allow users to capture video
I have a PHP project (I'm using CodeIgniter) with data capture. The form is
I'm building a web page screen capture application for an internal R&D project. Environment:
I have a project to do which is packet monitoring. I want to capture
I am working on a project where I have to capture images from webcam.I
I am working on a project to capture images via webcam in a predefined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.