Java Second Get getSecondsFromHMS(String hmsvalue)

Here you can find the source of getSecondsFromHMS(String hmsvalue)

Description

Convertie quelque chose comme 123:45:12 donne 445512

License

Open Source License

Parameter

Parameter Description
hmsvalue La valeur temporelle telle que H:MM:SS avec autant de H que l'on veut. On peut utiliser ce que l'on veut comme separateur.

Return

la valeur convertie en secondes

Declaration

public static int getSecondsFromHMS(String hmsvalue) 

Method Source Code

//package com.java2s;
/*/*from  ww w.  j  av  a  2s  . c o m*/
 * This file is part of Java Tools for hdsdi3g'.
 * 
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * Copyright (C) hdsdi3g for hd3g.tv 2009-2012
 * 
*/

public class Main {
    /**
     * Convertie quelque chose comme 123:45:12 donne 445512
     * @param hmsvalue La valeur temporelle telle que H:MM:SS avec autant de H que l'on veut.
     *        On peut utiliser ce que l'on veut comme separateur.
     * @return la valeur convertie en secondes
     */
    public static int getSecondsFromHMS(String hmsvalue) {
        // 00000:00:00
        if (hmsvalue.length() < 7) {
            return -1;
        }

        try {
            int secs = Integer.valueOf(hmsvalue.substring(hmsvalue.length() - 2, hmsvalue.length()));
            int mins = Integer.valueOf(hmsvalue.substring(hmsvalue.length() - 5, hmsvalue.length() - 3));
            int hours = Integer.valueOf(hmsvalue.substring(0, hmsvalue.length() - 6));

            return (hours * 3600) + (mins * 60) + secs;
        } catch (Exception e) {
        }
        return -1;
    }
}

Related

  1. getSeconds(String time, int defaultValue)
  2. getSecondsAsClock(int time)
  3. getSecondsByDays(int days)
  4. getSecondsForShortString(String string)
  5. getSecondsFromEpoch()
  6. getSecondsFromString(String s)
  7. getSecondsFromString(String text)
  8. getSecondsFromStringDuration(String duration)
  9. getSecondSimpleType(String completeType)