2

I have some problems. I am a beginner programmer. I need to create an application like a clock with Android Studio and need to add and subtract hours and minutes. When I press the +1H will need to add 1H to take the same with +1M and -1M and I can't coordinate how can I do that. I am doing so that when the clock reached 23:59 again, it was 00:00 and so cyclically

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Button button,button2,button3,button4;
    private TextView textView;
    int hour = 0;
    int minute = 0;
    int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        textView = findViewById(R.id.res);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    if (hour >= 9) {
                        textView.setText((++hour + "") + ":" + "00");
                    } else {
                        textView.setText("0" + (++hour + "") + ":" + "00");
                    }

            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(hour>=9) {
                    textView.setText((--hour + "") + ":" + "00");
                }else{
                    textView.setText("0"+(--hour + "") + ":" + "00");
                }
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(minute>=9) {
                    textView.setText("00" + ":" + (++minute + ""));
                }else{
                    textView.setText("00" + ":" + "0"+(++minute + ""));
                }
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(minute>=9) {
                    textView.setText("00" + ":" + (--minute + ""));
                }else{
                    textView.setText("00" + ":" + "0"+(--minute + ""));
                }
            }
        });
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Riarm
  • 21
  • 1
  • 3
  • 2
    Have you had a look at the Calendar class? It takes care of most of these things for you. https://developer.android.com/reference/java/util/Calendar.html#add(int,%20int) – TofferJ May 21 '19 at 17:42

4 Answers4

3

java.time and ThreeTenABP

All of this functionality already exists. Use LocalTime from java.time, the modern Java date and time API. Here’s a short demonstration with output given in comments:

    LocalTime time = LocalTime.of(23, 59);
    System.out.println(time); // 23:59
    time = time.minusHours(1);
    System.out.println(time); // 22:59
    time = time.minusMinutes(1);
    System.out.println(time); // 22:58
    time = time.plusHours(1);
    System.out.println(time); // 23:58
    time = time.plusMinutes(1);
    System.out.println(time); // 23:59
    time = time.plusHours(1);
    System.out.println(time); // 00:59
    time = time.plusMinutes(1);
    System.out.println(time); // 01:00

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

Make use of Calendar#add for this. Confusingly, you can use it to both add and subtract.

final Calendar calendar = Calendar.getInstance(); //A calendar set to the current time
calendar.add(Calendar.HOUR_OF_DAY, 1); //Add one hour
calendar.add(Calendar.HOUR_OF_DAY, -1); //Subtract one hour

When it comes to printing times, the default option on API versions < 24 is to uuse a DateFormat. So, instead of trying to determine it yourself:

final SimpleDateFormat format = new SimpleDateFormat("HH:mm"); //hours and minutes, 24hr clock
textView.setText(format.format(calendar.getTime());

However, do note there are issues with SimpleDateFormat, particularly when it comes to multithreading. As Ole V.V. has suggested in the comments, you should consider using the ThreeTenABP library instead.

If you're targeting API 24+, you can make use of the Java 8 additions around time in the java.time package, like Instant and DateTimeFormatter

PPartisan
  • 8,173
  • 4
  • 29
  • 48
  • I do recommend java.time, added in Java 8. You may also use it at lower API levels since it has been backported in [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Ole V.V. May 22 '19 at 13:21
  • @OleV.V. Yes, this is good advice, thank you - I've updated my answer – PPartisan May 22 '19 at 13:24
0

May be this code will help you to get the time and get it incremented or decremented properly.

Calendar calendar = Calendar.getInstance();
calendar.setTime(yourdate);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);
b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
        if(hours>=9)
        {
            hours=hours+1;
            textView.setText(hours+":"+minutes+":"+seconds);
        }
        else
        {
            textView.setText(hours+":"+minutes+":"+seconds);
        }
    }
});
b1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(hours>=9)
        {
            hours=hours-1;
            textView.setText(hours+":"+minutes+":"+seconds);
        }
        else
        {
            textView.setText(hours+":"+minutes+":"+seconds);
        }
    }
});
b2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(minutes>=9)
        {
            minutes=minutes+1;
            textView.setText(hours+":"+minutes+":"+seconds);
        }
        else
        {
            textView.setText(hours+":"+minutes+":"+seconds);
        }
    }
});
b3.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(minutes>=9)
        {
            minutes=mminutes-1;
            textView.setText(hours+":"+minutes+":"+seconds);
        }
        else
        {
            textView.setText(hours+":"+minutes+":"+seconds);
        }
    }
});
nLee
  • 1,320
  • 2
  • 10
  • 21
  • how do I do so when the clock reaches 23:59 and when I press +1H or +1M it was 00:00 and how to do that the Clock does not exceed the mark 23 and the minute 59 on Android studio – Riarm May 21 '19 at 20:01
0

To add or minus 30 minutes in current time in Kotlin language:

    val timeFormat = SimpleDateFormat("hh:mm:ss", Locale.US)
    val date = Date()
    val c = Calendar.getInstance()
    c.time = date
    //30 for add 30 minutes or -30 to minus 30 minutes
    c.add(Calendar.MINUTE, -30)
    val timeMinus30 = c.time
    val timeInString = timeFormat.format(timeMinus30)
Abdul Waheed
  • 562
  • 6
  • 14