Adding N Hours to a Date in Java

Adding time in hours to a date object in java?

If you are using java.time it can be more helpful :

LocalDateTime dateStart = LocalDateTime.now();
LocalDateTime dateStop = dateStart.plusHours(4);

To format the date you can use :

String d1 = dateStart.format(DateTimeFormatter.ofPattern("yy/MM/dd HH:mm:ss"));
String d2 = dateStop.format(DateTimeFormatter.ofPattern("yy/MM/dd HH:mm:ss"));

Add X hours to a date & time

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()));

Time not adding

The first thing to note is h in SimpleDateFormat represents the "Hour in am/pm (1-12)"

So, 10am + 12 is 10pm, but since you've not supplied a formatter for the am/pm marker, it's basically just 10...

Now days, the preferred method would be to use Java 8's Time API or Joda-Time, for example...

LocalDateTime dateTime = LocalDateTime.now();
System.out.println(DateTimeFormatter.ofPattern("MM-dd-yy HH:mm:ss").format(dateTime));
LocalDateTime later = dateTime.plusHours(12);
System.out.println(DateTimeFormatter.ofPattern("MM-dd-yy HH:mm:ss").format(later));

Which outputs...

02-23-16 10:27:27
02-23-16 22:27:27

I've deliberately used HH to show that it works

how to add 10 hour in current date and time in java

Your code is almost working, but you have made a typo. You try to invoke parse() on formatter which has not been declared. Instead you have to call parse() on sdf:

public static void main(final String[] args) throws Exception {
final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
final String currentDateandTime = " 9/4/2014 3:55:10 AM ";
final Date date = sdf.parse(currentDateandTime);
final Calendar calendar = Calendar.getInstance();

calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);

System.out.println("Time here " + calendar.getTime());
}

Since you are using 12-hour system you can modify it like this:

public static void main(final String[] args) throws Exception {
final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
final String currentDateandTime = "9/4/2014 3:55:10 AM";
final Date date = sdf.parse(currentDateandTime);
final Calendar calendar = Calendar.getInstance();

calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);

System.out.println("Time here " + sdf.format(calendar.getTime()));
}

Changing Java Date one hour back

java.util.Calendar

Calendar cal = Calendar.getInstance();
// remove next line if you're always using the current time.
cal.setTime(currentDate);
cal.add(Calendar.HOUR, -1);
Date oneHourBack = cal.getTime();

java.util.Date

new Date(System.currentTimeMillis() - 3600 * 1000);

org.joda.time.LocalDateTime

new LocalDateTime().minusHours(1)

Java 8: java.time.LocalDateTime

LocalDateTime.now().minusHours(1)

Java 8 java.time.Instant

// always in UTC if not timezone set
Instant.now().minus(1, ChronoUnit.HOURS));
// with timezone, Europe/Berlin for example
Instant.now()
.atZone(ZoneId.of("Europe/Berlin"))
.minusHours(1));

How to add one day to a date?

Given a Date dt you have several possibilities:

Solution 1: You can use the Calendar class for that:

Date dt = new Date();
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.DATE, 1);
dt = c.getTime();

Solution 2: You should seriously consider using the Joda-Time library, because of the various shortcomings of the Date class. With Joda-Time you can do the following:

Date dt = new Date();
DateTime dtOrg = new DateTime(dt);
DateTime dtPlusOne = dtOrg.plusDays(1);

Solution 3: With Java 8 you can also use the new JSR 310 API (which is inspired by Joda-Time):

Date dt = new Date();
LocalDateTime.from(dt.toInstant()).plusDays(1);

Android Add 2 hours to date String

final String dateString = "16-08-2015 16:15:16";
final long millisToAdd = 7_200_000; //two hours

DateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

Date d = format.parse(dateString);
d.setTime(d.getTime() + millisToAdd);

System.out.println("New value: " + d); //New value: Sun Aug 16 18:15:16 CEST 2015


Related Topics



Leave a reply



Submit