Changing Java Date one hour back

how to subtract hours from timestamp in java
add hours to date = java
java localdatetime to date
how to add 1 hour to current time in java
java date add minutes
java util date
java instant to date
java 8 date minus days

I have a Java date object:

Date currentDate = new Date();

This will give the current date and time. Example:

Thu Jan 12 10:17:47 GMT 2012

Instead, I want to get the date, changing it to one hour back so it should give me:

Thu Jan 12 09:17:47 GMT 2012

What would be the best way to do it?


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


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


new LocalDateTime().minusHours(1)

Java 8: java.time.LocalDateTime

Java 8 java.time.Instant

// always in UTC if not timezone set, ChronoUnit.HOURS));
// with timezone, Europe/Berlin for example

Add Hours To a Date In Java, In this article, you'll find several ways of adding or subtracting years, Add/​Subtract years, months, days, hours, minutes, seconds to a Date SECOND, 50); // Convert calendar back to Date Date currentDatePlusOne = c. Use java.util.Date toInstant() method to convert a Date object to a java.time.Instant instance; Add a specific Duration to the java.time.Instant object using the plus() method; Recover our java.util.Date instance by passing in the java.time.Instant object to the java.util.Date.from() method; Let's have a quick look at this approach:

Similar to @Sumit Jain's solution

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


Date currentDate = new Date(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1));

Java Add/subtract years, months, days, hours, minutes, or seconds , YEAR, 1); //Add one hour to current date time cal.add(Calendar.HOUR Here is the full source code to show how to modify date time in Java. Changing Java Date one hour back. Related. 1513. Calculate relative time in C#. 3594. Create ArrayList from array. 2247. How do I call one constructor from another in

How to modify date time (Date Manipulation) - Java, Opposite of adding hours to a java date, this example shows how to of hours from a date using java's Calendar.add, java 8 date time api, hours, in this case a negative number of hours, to the date returning a new object. j'ai un objet Date Java: Date currentDate = new Date(); indique la date et l'heure actuelles. Exemple: Thu Jan 12 10:17:47 GMT 2012 au lieu de cela, je veux obtenir la date, en la changeant à une heure en arrière de sorte qu'il devrait me donner:

Use Calendar.

Calendar cal = Calendar.getInstance();

cal.setTime(new Date());
cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) - 1);

Subtract hours from date, Furthermore, as well as different countries being in one or more different timezones, In the fall, clocks are set back an hour as sunrise happens later each day. Updating the Java Runtime Environment With the Latest Timezone Data to ship each JRE update release with the most up-to-date timezone data possible. Formatting Date and Time. The "T" in the example above is used to separate the date from the time. You can use the DateTimeFormatter class with the ofPattern() method in the same package to format or parse date-time objects. The following example will remove both the "T" and nanoseconds from the date-time:

Or using the famous Joda Time library:

DateTime dateTime = new DateTime();
dateTime = dateTime.minusHours(1);
Date modifiedDate = dateTime.toDate();

Timezones, Daylight Savings Time, and the Sun TZupdater for the , A common programming challenge is to perform simple date "math" on a value. with their own implementation), but one simple way is just combine a get and set call. So for example, adding three hours to a JavaScript date can be done by using setHours after running getHours. getHours() + 2); //Go back 3 days date. The java.util.Date has no concept of time zone, and only represents the number of seconds passed since the Unix epoch time – 1970-01-01T00:00:00Z. But, if you print

Perform date manipulations based on adding or subtracting time, Daylight saving time (DST), also daylight savings time or daylight time (United States and The typical implementation of DST is to set clocks forward by one hour in the set clocks back by one hour in autumn ("fall back") to return to standard time. Many systems in use today base their date/time calculations from data  The Date/Time API. Java 8 introduced us to a whole new API, which was included in the build to replace java.util.Date and java.util.Calendar. It's still useful to know how to get the current date and time using the previous two classes since not all applications have yet migrated to Java 8.

Daylight saving time, Now with the new Date/Time API, there'are new and better options. The second type also states the offset from UTC/GMT time, but with one of the following prefixes: UTC, GMT When the daylight time ends, clocks are set back by an hour. If you are prompted for an administrator password or confirmation, type the password or provide confirmation. 3. In the Date and Time Settings dialog box, do one or more of the following: •. To change the hour, double-click the hour, and then click the arrows to increase or decrease the value. •.

22. Time Zones and Daylight Savings, There are three ways to get time in milliseconds in java. By Chaitanya Singh | Filed Under: Java Date 1) Using public long getTime() method of Date class. getInstance(); //Setting the Calendar date and time to the given date and time Java Date – Convert time in 12 hour format to 24 hour format · Java – Display time  The plus and minus methods let you add various date and time units to a Date-Time object. There are four variants of each for the LocalDate class, allowing you to add or subtract years, months, weeks, and days to a LocalDate object. The following code prints the current date, tomorrow’s date, and the date one week, one month, and one year

  • is there any mktime equivalent in java? subtract 60 minutes from it?
  • Just be vary of the fact that there is Calendar.HOUR which works with 12 hour clock and Calendar.HOUR_OF_DAY for 24 hour clock:…
  • Would the first solution with Calendar work if the time is midnight of one day and you want an hour early which is 11pm the previous day?
  • @GeorgeArokiam yes it would
  • Way better solution. Calendar instance creation looks like overkill in this particular application.
  • @cosmolev Calendar is overkill in most examples I find.
  • @Stoan I have added a more readable example. You should note that Calendar is incredibly inefficient.
  • @PeterLawrey Date currentDate = new Date(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(10)); This should be 10 minutes behind?
  • Hello sir, I still cannot find the proper way to make "time ago" for the Instant.
  • FYI: This Answer uses troublesome old Date-time classes that are now legacy, supplanted by the modern java.time classes.
  • @Makky: I know things have changed. But its not a wrong answer!!! Why do you tell this a wrong answer? Did you see the accepted answer fully?
  • Why would you even answer to a question which is 5 years old and has accepted answer.
  • This Answer provides no value beyond the existing years-old Answers. And this Answer unwisely uses terrible old date-time classes that have been supplanted for years by the java.time classes. Another problem: this Answer ignores the issue of time zone, and shows code that will use the JVM’s current default time zone while the Question asked for GMT/UTC. See the later parts of the correct Answer by Kimi or see my Answer for more discussion.
  • This Answer does not address the Question. The Question is about more than a time-of-day alone.