Java Hour Calculate plusOrMinusHours(Date dateTime, long count)

Here you can find the source of plusOrMinusHours(Date dateTime, long count)

Description

plus Or Minus Hours

License

Open Source License

Declaration

public static String plusOrMinusHours(Date dateTime, long count) 

Method Source Code

//package com.java2s;
/*//from ww w .j  a  v a 2  s.  c o  m
 * @(#)CommonUtils.java Jun 30, 2009
 * 
 * Copyright 2008 by ChinanetCenter Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ChinanetCenter Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ChinanetCenter.
 * 
 */

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static String plusOrMinusHours(Date dateTime, long count) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateTime.setTime(dateTime.getTime() + count * 3600000);
            return sdf.format(dateTime);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String plusOrMinusHours(String dateTime, long count) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(dateTime);
            date.setTime(date.getTime() + count * 3600000);
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}

Related

  1. isValidTime(int hour, int minute, boolean ampm)
  2. parseDate(String date, String hours)
  3. plusHour(int hour)
  4. plusHour(String dateStr, int hour)
  5. plusHours(Date date, int hours)
  6. setHours(Date date, int amount)
  7. toStringHour(int hours, int minutes, int seconds, int millis)
  8. toTime(int hours, int minutes, int seconds)
  9. trimToHour(Date date)