Convert time field H:M into integer field (minutes) in JAVA

后端 未结 5 900
名媛妹妹
名媛妹妹 2021-01-06 14:25

The JTable includes the time field, e.g. \"01:50\". I need to read this value into the integer variable. For this I´d like to convert time into minutes. For instance \"01:50

5条回答
  •  萌比男神i
    2021-01-06 15:01

    How about this:

    String time = "1:50";
    String[] split = time.split(":"); 
    if(split.length == 2) { 
            long minutes = TimeUnit.HOURS.toMinutes(Integer.parseInt(split[0])) + 
                             Integer.parseInt(split[1]);
            System.out.println(minutes);
        }
    
    /* Output: 110 */
    

提交回复
热议问题