Deimos
Deimos

Reputation: 1

Java class - add n hours, minutes, and seconds to a time

I am fairly new to Java would like to know if this logic looks sound. The purpose of this class is to receive input from the user for a time in 12-hour format. Then the user is prompted to input a period of time. Finally, it outputs the final time (with the time added), in 12-hour format. I've run several tests scenarios through this and everything seems to be working fine. I'd just like some additional sets of trained eyes to look at it before I call it good. Thanks for your help!

import javax.swing.JOptionPane;

public class M3E7 {
public static void main(String args[]) {

    String start_hr = null;
    String start_min = null;
    String start_sec = null;
    String abbr = null;
    String hr = null;
    String min = null;
    String sec = null;
    int start_hr_num = 0;
    int start_min_num = 0;
    int start_sec_num = 0;
    int hr_num = 0;
    int min_num = 0;
    int sec_num = 0;
    int final_hr = 0;
    int final_min = 0;
    int final_sec = 0;

    start_hr = JOptionPane.showInputDialog("Start time - Enter the hours.");
    start_min = JOptionPane.showInputDialog("Start time - Enter the minutes.");
    start_sec = JOptionPane.showInputDialog("Start time - Enter the seconds.");
    abbr = JOptionPane.showInputDialog("Start time - Enter either am or pm.");
    hr = JOptionPane.showInputDialog("Enter the number of hours to add (less than 24).");
    min = JOptionPane.showInputDialog("Enter the number of minutes to add (less than 60).");
    sec = JOptionPane.showInputDialog("Enter the number of seconds to add (less than 60).");

    start_hr_num = Integer.parseInt(start_hr);
    start_min_num = Integer.parseInt(start_min);
    start_sec_num = Integer.parseInt(start_sec);
    hr_num = Integer.parseInt(hr);
    min_num = Integer.parseInt(min);
    sec_num = Integer.parseInt(sec);

    if (abbr.equals("pm")); {
        start_hr_num += 12;
    }

    final_hr = (start_hr_num + hr_num);
    final_min = (start_min_num + min_num);
    final_sec = (start_sec_num + sec_num);

    if (final_sec >= 60) {
        final_min++;
        final_sec -= 60;
    }

    if (final_min >= 60) {
        final_hr++;
        final_min -= 60;
    }

    if (final_hr >= 24) {
        final_hr -= 24;
    }

    if (final_hr > 12) {
        final_hr -= 12;
        abbr.equals("pm");
    }
    else if (final_hr == 12) {
        final_hr -= 12;
        abbr.equals("am");
    }
    else {
        abbr.equals("am");
    }

    JOptionPane.showMessageDialog(null, "The new time of day is " + final_hr + ":" + final_min + ":" + final_sec + " " + abbr);

    System.exit(0);
   }
}

Upvotes: 0

Views: 2693

Answers (3)

Salil
Salil

Reputation: 536

As @Jon Skeet mentioned, use Joda Time. It will help you do the complex date calculations easily.

Upvotes: 0

Eric B.
Eric B.

Reputation: 24441

Since it is homework, I don't want to give you the entire answer written up, but at least give you something to think about.

All times and dates are stored as milliseconds from epoch (ie: jan 1, 1970). The way I would approach the problem is take the date that the user entered, and create a java.util.Date() object from it. From that Date object, you can simply add/subtract the number of milliseconds from the user's "period".

Then, it simply becomes an exercise to print out the new date object.

Of course, I don't know if this is already too complicated for your class or not, but it is a fairly simple approach (assuming that you have already used Date objects) without using any 3rd party libs (like Joda Time).

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1501966

Do yourself a favour and don't perform date/time arithmetic yourself.

Instead, use Joda Time to handle it for you:

  • Parse the first input as a LocalTime (via DateTimeFormatter.parseLocalTime)
  • Parse the next three inputs as Period values using Period.hours(), Period.minutes() and Period.seconds()
  • Use LocalTime.plus(ReadablePeriod) to add the period to the time
  • Format and output as you wish

Upvotes: 4

Related Questions