0

I am writing a Java program where I need to increase the hour and minute by 1 and output the current time in HH:MM format. If the hour is 23 it should go back to 0 or the minute is 59 the minutes should go to 0 and the hours should go to the next hour. This is what i have so far, however I am not entirely sure it is right. I am also unsure if that is the proper way to make the time appear with a : between hours and minutes. Here is my code:

//TimeOfDay program

public class TimeOfDay
{

    private int hour;
    private int minute;


    /**
     * Constructor for objects of class TimeOfDay
     */
    public TimeOfDay( int h, int m )
    {
        setHr( h );
        setMin( m );
    String time = "17:46";
    String [] values = time.split(":");

    }

    public TimeOfDay()
    {

    }




    /**
    - set the hour using an input parameter h
    - set the minute using an input parameter m
    - add one hour (increase hour by 1)
    - add one minute (increase minute by 1)
    - output the current time in HH:MM format
         (single digit hour or minute must have a leading 0, e.g. 04:09) 
     */


   public void setHr (int h )
   {
       if (0 <= h && h < 23 )
        hour = h;
       else 
        hour = 0;
    }

    public void setMin (int m)
    {
       if (0 <= m && m < 59)
        minute = m;
       else
        minute = 0;
    }

    public void addHour()
    {
        hour++;

        if ( hour > 23 )
        hour = 0;
    }

    public void addMinute()
    {
        minute++;

        if ( minute > 59 )
        {
            minute = 0;
            addHour(); //increment hour
        }
    }

    public int  printCurrentTime()
    {
        if (hour < 10)
        System.out.print( "0" + hour + ":");

        if (minute < 10)
        System.out.print( "0" + minute);

        return time.split;


    }
}
Sophia Ali
  • 301
  • 2
  • 6
  • 14
  • If this is **NOT** homework, you can simply use [Java's Calendar API](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) to do all this for you. – jahroy May 30 '13 at 03:55

4 Answers4

3

There are already libraries to do type of work for for you such as Calender and JodaTime. The latter makes this type of work especially easy. A single class LocalTime can manage the addition and formatting:

LocalTime localTime = new LocalTime("17:46");
LocalTime newTime = localTime.plusHours(1).plusMinutes(1); 
System.out.println(newTime.toString("HH:mm"));
Reimeus
  • 158,255
  • 15
  • 216
  • 276
3

This is obviously homework, so I won't bother recommending a library that will do this for you.

Your code looks good with the exception of your printCurrentTime() method, which doesn't make sense.

All this method should do is build a string from the hours and minutes.

It should be this simple:

public void printCurrentTime() {
    System.out.println(hour + ":" + minute);
}

Or if you want it to return a String (that can be printed elsewhere) it would look like this:

public String toString() {
    return hour + ":" + minute;
}

It makes no sense to have that method return an int.

It should return void if you want it to print.

It should return String if you want it to return.... a String.


I just read through the comments in your code... which mention requirements not included in your question.

Because you need to pad hours and minutes (when they're less than 10), you need to add some more logic.

I would personally use String.format() to pad your numbers...

But.... Since this is homework, you're probably not supposed to.

I would create two simple methods:

  1. getHourString()
  2. getMinuteString()

Both should return Strings.

The logic inside each method would be very simple. Someething like this:

private String getHourString() {
    String h = "";
    if (hour < 10) {
        h = "0";
    }
    return h + hour;
}

You could do the same thing for getMinuteString().

Your printCurrentTime() method could just combine the output of these methods (with a : in between).

In other words:

public void printCurrentTime() {
    String s = getHourString() + ":" + getMinuteString();
    System.out.println(s);
}
jahroy
  • 22,322
  • 9
  • 59
  • 108
  • Yes it is homework. that was the main part I was having trouble with. Is it just ok to use the public string tostring code? for instance if i create a object called mytime and i set mytime to 11:30 and i want it to display mytime would i use public String toString(11, 30) or public String toString(mytime)? – Sophia Ali May 30 '13 at 03:34
  • In my opinion that would be perfectly acceptable. That's exactly what I would do: I would implement _toString()_ so it returns the formatted time. I've seen people on this site complain that overriding `toString()` like that is bad practice... But I haven't seen any convincing reason to agree with them. – jahroy May 30 '13 at 03:37
  • Here's [an article](http://www.javapractices.com/topic/TopicAction.do?Id=55) that addresses some theories on best practices when overriding `toString()` in your own classes. Here's [an answer](http://stackoverflow.com/a/11659528/778118) where a knowledgeable user suggests that the `toString()` method should only be used for debugging. Either way... What you want to do is certainly fine for a homework assignment! – jahroy May 30 '13 at 03:42
2

You can use the built-in Calendar class in java.util to achieve that. With add(field, value) you can increase the hour and the Calendar takes care for the date-break.

To get the HH:MM format you can use SimpleDateFormat with the pattern "HH:mm".

Calendar c = Calendar.getInstance(); //automatically set to current time
c.add(Calendar.HOUR, 1);

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
System.out.println(dateFormat.format(c.getTime()));
Daniel Lerps
  • 5,256
  • 3
  • 23
  • 33
1

I would change printCurrentTime()

public int  printCurrentTime() {
     System.out.printf("%02d:%02d%n", hour, minute);
     return hour * 60 + minute;
}

the rest of the code seems OK

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Thank you! This really helped. Just two questions: 1. Could you explain what this part means: "%02d:%02d%n"? 2. When I run the program to make mytime(which i set to 11:30) appear this comes up "11:30 TimeOfDay@b6e0054" what is the second line and how can i get it to disappear? or is this part of the program BlueJ ? Thank you again for your help! – Sophia Ali May 30 '13 at 04:31
  • 1) "%02d:%02d%n" is format string for printf func. %02d means: print first arg as decimal with width = 2 padding with 0s if neccesary... %b means new line, see java.util.Formatter API for details. 2) The extra line comes from somewhere else in your program. – Evgeniy Dorofeev May 30 '13 at 04:48
  • The extra line looks like you're calling `System.out.println()` and passing it an instance of a `TimeOfDay` object. It's printing the value returned by `TimeOfDay.toString()`. – jahroy May 30 '13 at 04:49
  • Thank you! I have one more quick question. I am writing the test file, and I am now trying to add 30 minutes to mytime. This is my code: TimeOfDay mytime = new TimeOfDay(11, 30); int d = mytime.printCurrentTime(); System.out.print(mytime); //increase mytime by 30 minutes(add 1 minute 30 times), display mytime(12:00) int i=0; while (i < 30) { i++; d = mytime.addMinute(); } System.out.println(d); But it states d = mytime.addMinute(); is an incompatible type. I understand it needs to only include minutes, but how? – Sophia Ali May 30 '13 at 04:55
  • for d = mytime.addMinute(); to work addMinute() must return minutes: public int addMinute() { .... return minute;} – Evgeniy Dorofeev May 30 '13 at 05:11
  • Thank you! i still get the extra line though, not sure how to make it go away. – Sophia Ali May 30 '13 at 05:21
  • I got it to go away, thank you so much for all your help!! :) – Sophia Ali May 30 '13 at 05:39