How to take time from one field and add up to 3 minutes in another?

Asked

Viewed 552 times

-1

I’d like to know how to get to the time of the field txt_hora and move on to the txt_hora2 adding up +3 minutes. In case on the field one would be 09:50 and move on to the other 9:53.

Here I enter with the Hour

inserir a descrição da imagem aqui

And after clicking "IR" falls on this screen

inserir a descrição da imagem aqui

According to the image, I need to transfer the first hour to the other fields, but counting +3 minutes. I tried to do it this way, but I could not:

txt_hora2.setText("" + (Integer.parseInt(txt_hora.getText()) + 3));

and this too:

txt_hora2.setText("" + (Integer.parseInt(txt_hora.getText()) + (1000 * 60 * 3)));
  • 1

    What field are you talking about? There’s no code for them in the question.

  • I guess I didn’t explain it right. The field I say, is a jtextfield where I take an hour of the site in an hour and minutes. but I would like to pass this time to another jtextfield, but adding +3 minutes

  • 1

    See this: https://stackoverflow.com/a/40952687/5524514

  • This link has several methods, just choose one.

  • I don’t quite understand. What I have difficulty understanding is that in one of the fields I have to bring the correct time, but in the second field I already have to bring it counting +3 minutes. pq in my case are tests that are carried out every 3 minutes, and as I am bringing 3 test at once, I need to add +3 in their header.

  • Rafael, add a [mcve] of your code, not everyone can see images and they, depending on the problem, do not help much.

  • in this case I will not know how to create it, but thank you for the attention.

  • It gets complicated to reopen your question so with a [mcve] makes it easy to test the problem.

  • I understand, but I appreciate your help anyway.

  • You don’t want your question to be reopened?

  • I would like to, because I am standing still because of this doubt, but I will not be able to give an example in this condition.

  • Think about who wants to help you, with the closed question, you won’t get help, and without a [mcve], it’s even harder to suggest something that can solve your problem. I’d like to help you myself, but if you don’t show an effort to facilitate my collaboration in understanding your problem, how do you expect to get help? That’s why we ask for a simple testable example, in the link explains how to make a simplicate version of your code.

  • the problem is that I don’t know how to develop an example that is executable, without the database, because this information is from there, and the example that I could do would only run in netbeans, which would not be applicable by you.

  • first you have convert to a date and after that use the Calendar and give an add() of 3 minutes, the question would ta open to post the solution. Take a look at this https://stackoverflow.com/questions/9015536/java-how-to-add-10-mins-in-my-time

  • I suggest you also search for Jspinner instead of converting string to date and time. Java has a component that already enables you to recover Timestamp values directly without conversions or parses, opting for text fields is to go around in something that the language simplifies for you.

Show 10 more comments

1 answer

1

How did you not introduce a Minimum, Complete and Verifiable Example, I will assume that it is always guaranteed that this field receives formatted hours and within the valid range of each unit of time to be considered valid(Ex.: hours < 24, minutes <60...).

First you need to convert the text from the field String for a temporal type, such as util.Date and then perform this operation. Once you have converted the time to the right type, you perform the calculation, recovering the time in milliseconds and adding the 3 minutes also in milliseconds:

SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date time = format.parse(txt_hora.getText());
txt_hora2.setText(sdf.format(new Date(time.getTime() + (3*60*1000))));

I would like to recommend reading on new java date API, because with it operations with time become simpler.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.