Java Hour Format hoursFrom(Date date, int amount, String format)

Here you can find the source of hoursFrom(Date date, int amount, String format)

Description

hours From

License

Open Source License

Declaration

public static String hoursFrom(Date date, int amount, String format) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Oobium, Inc./*from w w  w .java2 s . c om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Jeremy Dowdall <jeremy@oobium.com> - initial API and implementation
 ******************************************************************************/

import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static Date hoursFrom(Date date, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, amount);
        return cal.getTime();
    }

    public static String hoursFrom(Date date, int amount, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(hoursFrom(date, amount));
    }

    private static Date add(Date date, int field, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(field, amount);
        return cal.getTime();
    }
}

Related

  1. getHourFormat(String string)
  2. getHourMinuteFormatter()
  3. getHoursNumberFormat()
  4. getOnehouragoFormatString(String format)
  5. hoursBetween(String from, String to, String format)