Here you can find the source of shiftTime(Object o, int minutes)
Parameter | Description |
---|---|
o | A timestamp string as 'HH:mm' or '-'. |
minutes | The minutes to increment/decrement from timestamp. |
public static String shiftTime(Object o, int minutes)
//package com.java2s; //License from project: Open Source License import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { /**//w w w. j ava 2 s.com * @param o A timestamp string as 'HH:mm' or '-'. * @param minutes The minutes to increment/decrement from timestamp. * @return Timestamp string as 'HH:mm', modified with the given minutes or the current time + 60 minutes if * the given timestamp was '-' or the given timestamp couldn't be parsed. */ public static String shiftTime(Object o, int minutes) { String timestamp = o.toString(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); Date date = null; if (timestamp != null && timestamp.length() >= 1 && timestamp.substring(0, 1).equals("-")) { date = new Date(); date.setTime(date.getTime() + 60 * 60000); } else { try { date = sdf.parse(timestamp); date.setTime(date.getTime() + minutes * 60000); } catch (ParseException ex) { date = new Date(); date.setTime(date.getTime() + 60 * 60000); } } return (sdf.format(date)); } }