Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add X hours to a date & time

I am currently fetching the time and date trough:

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));

This returns the example '05/14/2014 01:10:00'

Now I am trying to make it so I can add a hour to this time without having to worry about a new day or month etc.

How would I go on getting '05/14/2014 01:10:00' but then for 10 hours later in the same format?

Thanks in advance.

like image 276
user2911924 Avatar asked Oct 23 '25 14:10

user2911924


1 Answers

Look at the Calendar object: Calendar

Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, 10);
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
System.out.println(dateFormat.format(cal.getTime()));
like image 91
Robert Durgin Avatar answered Oct 26 '25 03:10

Robert Durgin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!