Java Second dateDiffToSeconds(final String diff)

Here you can find the source of dateDiffToSeconds(final String diff)

Description

parse a string to get time span in seconds

License

Open Source License

Parameter

Parameter Description
diff format ddays.hhours.mminutes.sseconds e.g., "d2.h8.m30.s45"

Return

span in seconds

Declaration

public static long dateDiffToSeconds(final String diff) 

Method Source Code

//package com.java2s;
/*//w  w w.j a v a 2s  .  c om
 * *****************************************************************************
 *   Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
 *   Licensed under the Apache License, Version 2.0 (the "License"). You may not use
 *   this file except in compliance with the License. A copy of the License is located at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   or in the "license" file accompanying this file.
 *   This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *   CONDITIONS OF ANY KIND, either express or implied. See the License for the
 *   specific language governing permissions and limitations under the License.
 * ***************************************************************************
 */

public class Main {
    /**
     * parse a string to get time span in seconds
     * @param diff format ddays.hhours.mminutes.sseconds e.g., "d2.h8.m30.s45"
     * @return span in seconds
     */
    public static long dateDiffToSeconds(final String diff) {
        long secs = 0;
        final String[] units = diff.split("[.]");
        for (final String unit : units) {
            if (unit.matches("[dhms][0-9]+")) {
                final char unitdesc = unit.charAt(0);
                switch (unitdesc) {
                case 's':
                    secs += Integer.parseInt(unit.replace("s", ""));
                    break;
                case 'm':
                    secs += Integer.parseInt(unit.replace("m", "")) * 60;
                    break;
                case 'h':
                    secs += Integer.parseInt(unit.replace("h", "")) * 60 * 60;
                    break;
                case 'd':
                    secs += Integer.parseInt(unit.replace("d", "")) * 60 * 60 * 24;
                    break;
                }
            }
        }
        return secs;
    }
}

Related

  1. bytesPerSecond(long totalByteCount, long elapsed)
  2. calculateSeconds(long duration)
  3. countSecond(Long time)
  4. currentSecondsPlus(final long seconds)
  5. dateArrayFromSeconds(int inSeconds)
  6. HoursToSeconds(final float hours)
  7. isSecondsWholeHour(long secs)
  8. second2time(int seconds)
  9. seconds(int num)