I want to do something like:
Date date = new Date(); // current date
date = date - 300; // substract 300 days from current date and I want to use this "date"
How to do it?
With Java 8's date time API change, Use LocalDate
LocalDate date = LocalDate.now().minusDays(300);
Similarly you can have
LocalDate date = someLocalDateInstance.minusDays(300);
Refer to https://stackoverflow.com/a/23885950/260990 for translation between java.util.Date <--> java.time.LocalDateTime
Date in = new Date();
LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault());
Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
Calendar cal = Calendar.getInstance();
cal.setTime(dateInstance);
cal.add(Calendar.DATE, -30);
Date dateBefore30Days = cal.getTime();
java.util.Date
, java.util.Calendar
, and java.text.SimpleTextFormat
are now legacy, supplanted by the java.time classes. See Tutorial by Oracle.
Commented
Aug 21, 2017 at 0:50
@JigarJoshi it's the good answer, and of course also @Tim recommendation to use .joda-time.
I only want to add more possibilities to subtract days from a java.util.Date
.
One possibility is to use apache-commons-lang. You can do it using DateUtils
as follows:
Date dateBefore30Days = DateUtils.addDays(new Date(),-30);
Of course add the commons-lang
dependency to do only date subtract it's probably not a good options, however if you're already using commons-lang
it's a good choice. There is also convenient methods to addYears
,addMonths
,addWeeks
and so on, take a look at the api here.
Another possibility is to take advantage of new LocalDate
from Java 8 using minusDays(long days)
method:
LocalDate dateBefore30Days = LocalDate.now(ZoneId.of("Europe/Paris")).minusDays(30);
LocalDateTime
as that class purposely has no concept of time zone nor offset-from-UTC. Use that only when zone/offset is unknown or not relevant. Instead use LocalDate
and pass a ZoneId
when calling its now
method. See the correct Answer by Jacob van Lingen.
Commented
Oct 2, 2017 at 5:28
s
in DateUtils
, it’s just DateUtil
.
Commented
Dec 3, 2018 at 20:24
Simply use this to get date before 300 days, replace 300 with your days:
Date date = new Date(); // Or where ever you get it from
Date daysAgo = new DateTime(date).minusDays(300).toDate();
Here,
DateTime
is org.joda.time.DateTime;
Date
is java.util.Date
Java 8 Time API:
Instant now = Instant.now(); //current date
Instant before = now.minus(Duration.ofDays(300));
Date dateBefore = Date.from(before);
java.time
might help. Also, some discussion/explanation is generally expected on Stack Overflow rather than just a code snippet. You should definitely explain Duration
as that is the added-value of your Answer over the existing one by van Lingen.
Commented
Nov 26, 2016 at 20:58
As you can see HERE there is a lot of manipulation you can do. Here an example showing what you could do!
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
//Add one day to current date.
cal.add(Calendar.DATE, 1);
System.out.println(dateFormat.format(cal.getTime()));
//Substract one day to current date.
cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println(dateFormat.format(cal.getTime()));
/* Can be Calendar.DATE or
* Calendar.MONTH, Calendar.YEAR, Calendar.HOUR, Calendar.SECOND
*/
java.util.Date
, java.util.Calendar
, and java.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
Commented
Dec 4, 2018 at 5:25
With Java 8 it's really simple now:
LocalDate date = LocalDate.now().minusDays(300);
A great guide to the new api can be found here.
now
method. If omitted, the JVM‘s current default time zone is implicitly and silently applied. That default lies outside your control and can change at any moment, even during runtime. Example: LocalDate.now( ZoneId.of( "America/Montreal" )
.
Commented
Jun 6, 2016 at 19:56
In Java 8 you can do this:
Instant inst = Instant.parse("2018-12-30T19:34:50.63Z");
// subtract 10 Days to Instant
Instant value = inst.minus(Period.ofDays(10));
// print result
System.out.println("Instant after subtracting Days: " + value);
I have created a function to make the task easier.
For 7 days after dateString: dateCalculate(dateString,"yyyy-MM-dd",7);
To get 7 days upto dateString: dateCalculate(dateString,"yyyy-MM-dd",-7);
public static String dateCalculate(String dateString, String dateFormat, int days) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat s = new SimpleDateFormat(dateFormat);
try {
cal.setTime(s.parse(dateString));
} catch (ParseException e) {
e.printStackTrace();
}
cal.add(Calendar.DATE, days);
return s.format(cal.getTime());
}
java.util.Date
, java.util.Calendar
, and java.text.SimpleDateFormat
that are now legacy, supplanted by the java.time classes. See Tutorial by Oracle.
Commented
Oct 1, 2017 at 22:12
LocalDate.parse( "2017-01-23").plusWeeks( 1 )
is simpler than writing your own method and is more self-documenting.
Commented
Oct 1, 2017 at 22:14
You may also be able to use the Duration
class. E.g.
Date currentDate = new Date();
Date oneDayFromCurrentDate = new Date(currentDate.getTime() - Duration.ofDays(1).toMillis());
You can easily subtract with calendar with SimpleDateFormat
public static String subtractDate(String time,int subtractDay) throws ParseException {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ENGLISH);
cal.setTime(sdf.parse(time));
cal.add(Calendar.DATE,-subtractDay);
String wantedDate = sdf.format(cal.getTime());
Log.d("tag",wantedDate);
return wantedDate;
}
java.util.Date
are now legacy, supplanted by the java.time classes.