Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.Date - Deleting three months from a date?

Tags:

java

People also ask

How do you subtract date in Java?

The minusDays() method of LocalDate class in Java is used to subtract the number of specified day from this LocalDate and return a copy of LocalDate. For example, 2019-01-01 minus one day would result in 2018-12-31. This instance is immutable and unaffected by this method call.

Why you should not use Java Util date?

Some other problems are: It rates years as two digits since 1900. There are many workarounds in the Java world around this banal design decision, like handling years before 1900. Months are zero indexed (0 – January, 11 – December).

How do I remove the time from a date object?

Use the toDateString() method to remove the time from a date, e.g. new Date(date. toDateString()) . The method returns only the date portion of a Date object, so passing the result to the Date() constructor would remove the time from the date. Copied!


Here's the plain JDK version, it needs the Calendar class as a helper:

Date referenceDate = new Date();
Calendar c = Calendar.getInstance(); 
c.setTime(referenceDate); 
c.add(Calendar.MONTH, -3);
return c.getTime();

But you should seriously consider using the Joda library, because of various shortcomings of the Date and Calendar classes. With Joda you can do the following:

new DateTime().minusMonths(3).toDate();

Or if you want to subtract from a given date instead of the current:

new DateTime(referenceDate).minusMonths(3).toDate();

Update for Java 8: With Java 8 you can also use the new JSR 310 API (which is inspired by Joda):

LocalDateTime.from(referenceDate.toInstant()).minusMonths(3);

Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MONTH, -3);

Set your date using setTime method.


Using Java 8 you can do it like this,

Date d = Date.from(LocalDate.now().minusMonths(3).atStartOfDay(ZoneId.systemDefault()).toInstant());

The LocalDate class has a lot of methods to help you make easy computations about dates like the above,

// Add 2 months
Date d = Date.from(LocalDate.now().plusMonths(2).atStartOfDay(ZoneId.systemDefault()).toInstant());
// Add 5 days
Date d = Date.from(LocalDate.now().plusDays(5).atStartOfDay(ZoneId.systemDefault()).toInstant());
// Minus 1 day and 1 year
Date d = Date.from(LocalDate.now().minusYears(1).minusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());

In order to compute time you can use the LocalDateTime class,

// Minus 1 year, minus 1 days, plus 1 hour
Date d = Date.from(LocalDateTime.now().minusYears(1).minusDays(1).plusHours(1).toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant());

I always recommend Joda for this sort of stuff. It has a much nicer API, and doesn't suffer from threading issues that the standard Java date/time has (e.g. issues with SimpleDateFormat, or general mutability).

e.g.

DateTime result = dt.minusMonths(3);

Ok with java.sql.Date (subclass of java.util.Date) and JDK's 8 LocalDate help you can do it in one line ;)

Date date = java.sql.Date.valueOf(LocalDate.now().minus(3, ChronoUnit.MONTHS));

The Date class itself isn't enough (+: You've got to use the Calendar class here

Something along these lines

GregorianCalendar lCalendar = new GregorianCalendar();
lCalendar.setTime( aDate );
lCalendar.add(Calendar.MONTH, -3);

p.s. the snippet above is not tested to be compilable.


You want today - 3 Month formatted as dd MMMM yyyy

     SimpleDateFormat format = new SimpleDateFormat("dd MMMM yyyy");

     Calendar c = Calendar.getInstance(); 
     c.setTime(new Date()); 
     c.add(Calendar.MONTH, -3);

     Date d = c.getTime();
     String res = format.format(d);

     System.out.println(res);

So this code can do the job ;)