3

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.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
user2911924
  • 331
  • 3
  • 5
  • 15
  • I use Calendar for this. Other people use the JodaTime library which I am not familiar with. – BevynQ May 09 '14 at 00:23
  • Me neither, rather not include new things to the project. – user2911924 May 09 '14 at 00:24
  • If you don't mind using Calendar then this will help you. http://stackoverflow.com/questions/3581258/adding-n-hours-to-a-date-in-java – binary_assemble May 09 '14 at 00:27
  • Or this one: http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java – Paul Hicks May 09 '14 at 01:24
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 25 '18 at 18:53

4 Answers4

7

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()));
Robert Durgin
  • 1,810
  • 19
  • 23
  • 2
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 25 '18 at 18:53
3

As others have mentioned, the Calendar class is designed for this.

As of Java 8, you can also do this:

DateTimeFormatter dateFormat =
    DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");

LocalDateTime date = LocalDateTime.now();
System.out.println(dateFormat.format(date));
System.out.println(dateFormat.format(date.plusHours(10)));

java.time.format.DateTimeFormatter uses a lot of the same pattern letters as java.text.SimpleDateFormat, but they are not all the same. See the DateTimeFormatter javadoc for the details.

VGR
  • 40,506
  • 4
  • 48
  • 63
  • To be clear, the `Calendar` class is now supplanted by the *java.time* classes, specifically `DateTimeFormatter`. There is no longer any reason to ever use `Date`, `Calendar`, `SimpleDateFormat`, or any other of the troublesome legacy date-time classes bundled with the earliest versions of Java. Use only classes from the `java.time.*` package. – Basil Bourque Mar 25 '18 at 18:55
1
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date currentDate = new Date();
final long reqHoursInMillis = 1 * 60 * 60 * 1000;  // change 1 with required hour
Date newDate = new Date(currentDate.getTime() + reqHoursInMillis);
System.out.println(dateFormat.format(newDate));

This will add 1 hour in current time in the given date format. Hope it helps.

Ashish John
  • 1,867
  • 2
  • 23
  • 38
0
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));

/*
 * Add x hours to the time
 */

int x = 10;

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

calendar.add(Calendar.HOUR, x);

System.out.println(dateFormat.format(calendar.getTime()));

Console output:

05/08/2014 20:34:18
05/09/2014 06:34:18
mrres1
  • 1,147
  • 6
  • 10