Java Minute Calculate shiftTime(Object o, int minutes)

Here you can find the source of shiftTime(Object o, int minutes)

Description

shift Time

License

Open Source License

Parameter

Parameter Description
o A timestamp string as 'HH:mm' or '-'.
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.

Declaration

public static String shiftTime(Object o, int minutes) 

Method Source Code

//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));
    }
}

Related

  1. minutesBetween(String from, String to, String format)
  2. minutesToHylaTime(int mins)
  3. plusMinute(Date date, int minutes)
  4. plusMinute(int minute)
  5. setMinutes(Date date, int amount)
  6. toDegreesMinutes(double degrees, int nDecimals, boolean longitude)
  7. toMinutes(Date fecha)
  8. toMinuteString(long time)