Calculate the minutes difference between two hours

0

I have the following problem:

I have two hours each in their respective jTextField , what I need to know is what to do to subtract those two hours, and calculate only the difference minutes at the moment of pressing the respective button.

Start time I bring SQL with this format: *2018-08-14 9:31*
The departure time is taken from the system in this format: 9:50

I would like to know what has to be done to calculate in the Jtetxfield for example:

  

Time: 19 (minutes).

I'll leave you the code that I've been carrying so far:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String in = txtHoraInicio.getText();
    String fin = txtHoraSalida.getText();
    int inicio = Integer.parseInt(in);
    int salida = Integer.parseInt(fin);
    int diferencia = (salida - inicio);
    String dif = Integer.toString(diferencia);
    txtTiempo.setText(dif);
    
asked by Eddy Trejo 14.08.2018 в 16:36
source

3 answers

0

The first thing is to extract the hours that you have in the form of String and pass them to a Date type object, if the format you occupy is 2018-08-14 9:31, you do it in the following way:

public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd h:mm", Locale.US);
public static SimpleDateFormat sdfResult = new SimpleDateFormat("HH:mm:ss", Locale.US);
public static SimpleDateFormat sdfResultMinutos = new SimpleDateFormat("m", Locale.US);

public static void main(String[] args) throws ParseException {
    Date difference = getDifferenceBetwenDates(sdf.parse("2018-11-01 9:31"), sdf.parse("2018-11-01 9:33"));
    System.out.println(sdfResult.format(difference)); //00:02:00
    System.out.println(sdfResultMinutos.format(difference) + " Minutos"); //2 Minutos

    Date difference2 = getDifferenceBetwenDates(sdf.parse("2018-11-01 9:31"), sdf.parse("2018-11-01 11:28"));

    System.out.println(sdfResult.format(difference2)); //01:57:00
    System.out.println(sdfResultMinutos.format(difference2) + " Minutos"); //57 Minutos
}

public static Date getDifferenceBetwenDates(Date dateInicio, Date dateFinal) {
    long milliseconds = dateFinal.getTime() - dateInicio.getTime();
    int seconds = (int) (milliseconds / 1000) % 60;
    int minutes = (int) ((milliseconds / (1000 * 60)) % 60);
    int hours = (int) ((milliseconds / (1000 * 60 * 60)) % 24);
    Calendar c = Calendar.getInstance();
    c.set(Calendar.SECOND, seconds);
    c.set(Calendar.MINUTE, minutes);
    c.set(Calendar.HOUR_OF_DAY, hours);
    return c.getTime();
}

What you are doing first is to convert the current value of your date to Date, this is done with the function parse of SimpleDateFormat remember that this function requires you to capture a possible exception, once in Date, you can move to milliseconds by means of the function getTime () this will give you a type long , you would get the difference between these two values and divide them to get seconds, minutes and hours, then create a Calendar object and assign the second minutes and hours calculated previously, when calling the function getTime() of Calendar you get an object type Date , which later you can format your taste by SimpleDateFormat .

Greetings!

    
answered by 01.11.2018 в 15:48
0

I do not really understand how you do it, but here is an example that will help you:

Public void calcularTiempo(){ //Método para saber la hora
    //Llamamos al obbjeto
    Date myDate = new Date();
    inicio = System.currentTimeMillis(); //Cogemos el tiempo en que has iniciado el programa
    long tempsFinal = System.currentTimeMillis(); //Calculamos el tiempo de ejecucion del programa
    long diferenciaTemps = tempsFinal - inicio; //Restamos el tiempo de ejecución por el decuando lo iniciaste
    //Conversion de horas,minutos,segundos respecto a milisegundos 
    SimpleDateFormat formatTemps = new SimpleDateFormat("HH:mm:ss");
    //Canviamos formato de franja horaria CET a UTC
    formatTemps.setTimeZone(TimeZone.getTimeZone("UTC"));
    //Imprimimos el tiempo en la consola
    System.out.println("Tiempo: "+formatTemps.format(diferenciaTemps));

His thing is to do it with a method apart to not fill much the main code.

PS: It's the first time I help, if you do not understand something I'll be happy to repeat / modify it

    
answered by 22.01.2019 в 10:24
-1

If you throw a Date () then use java.util.concurrent.TimeUnit :

long diferencia=fecha1.getTime()-fecha2.getTime();
minutos = TimeUnit.MILLISECONDS.toMinutes(diferencia); 

And the value thrown will be in minutes.

    
answered by 16.08.2018 в 01:25