Here you can find the source of dateDiffToSeconds(final String diff)
Parameter | Description |
---|---|
diff | format ddays.hhours.mminutes.sseconds e.g., "d2.h8.m30.s45" |
public static long dateDiffToSeconds(final String diff)
//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; } }