Share on Google+Share on Google+

Java add hours to Date

In this tutorial, you will learn how to add hours to date.

Java add hours to Date

In this tutorial, you will learn how to add hours to date.

Java Calendar class is a very useful and handy class. It is basically used in date time manipulation. Here, we are going to add few hours to current date and return the resultant time. For this, we have created a calendar instance and get a date to represent the current date. Then using the method add() of Calendar class, we have added 4 hours to the calendar which in result display the resulted time.


import java.util.*;
import java.text.*;

public class AddHoursToDate{

  public static void main(String[] args){
    Calendar calendar = Calendar.getInstance();
    Date today = calendar.getTime();
	SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
    System.out.println("Current Time: " + sdf.format(today));
    calendar.add(Calendar.HOUR_OF_DAY, 4);
    Date addHours = calendar.getTime();
    System.out.println("Time after 4 hours: " + sdf.format(addHours));


Current Time: 12:27:16
Time after 4 hours: 04:27:16


Posted on: October 11, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.