This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
How to subtract X day from a Date object in Java?
(10 answers)
Closed 8 years ago.
Can any one help me with the code for adding some number of days to any date..?
For example today is 11-04-2014. I want 15-04-2014 + 3 days output:18-04-2014.
My question is not adding dates to current date..
With Java 8, you can write:
import java.time.LocalDate;
LocalDate date = LocalDate.of(2014, 4, 11);
LocalDate newDate = date.plusDays(3);
System.out.println(newDate); // Prints 2014-04-14
Its that simple.
String dateString = "11-04-2014" // Say you have a date in String format
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy"); // Create an instance of SimpleDateFormat with the right format.
Date date = format.parse(dateString); // Then parse the string, this will need a try catch statement.
Calendar calendar = Calendar.getInstance(); // Get an instance of the calendar.
calendar.setTime(date); // Set the time of the calendar to the parsed date
calendar.add(Calendar.DATE, 3); // Add the days to the calendar
String outputFormat = format.format(calendar.getTime());
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class A {
public static void main(String[] args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, 1);
calendar.set(Calendar.YEAR, 2012);
calendar.add(Calendar.DAY_OF_MONTH, 3);
System.out.println(simpleDateFormat.format(calendar.getTime()));
}
}
You can use the calendar function:
Calendar cal = Calendar.getInstance();
cal.setTime(dateInstance);
cal.add(Calendar.DATE, NO_OF_DAYS_TO_ADD);
Date addedDays = cal.getTime();
DateInstance is the date you are using. addedDays can be formatted using SimpleDateFormat to display in any date format that you would like to use.
Related
I am using SimpleDateFormat to get the current month but i want to show a recyclerview table with information of this month and past three months.
My php json loads this but i want to put automatically it in android.
periodo1 = findViewById(R.id.tittle_periodo1);
periodo2 = findViewById(R.id.tittle_periodo2);
periodo3 = findViewById(R.id.tittle_periodo3);
periodo4 = findViewById(R.id.tittle_periodo4);
SimpleDateFormat month_date = new SimpleDateFormat("yyyyMM");
String month1 = month_date.format(c.getTime());
periodo1.setText(month1);
try this :
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, -3); // -3 is Number of months past (july)
Date newDate = calendar.getTime();
and you can format it if you want :
String date = DateFormat.format("MM/dd/yyyy", newDate).toString();
With Java8 syntax you can use time library to achieve this
import java.time.LocalDate;
LocalDate now = LocalDate.now(); // 2019-11-01 (Nov)
LocalDate minusOneMonth = now.minusMonths(1); // 2019-10-01
minusOneMonth.getMonth().getValue(); // Gives -1 month (10)
LocalDate minusTwoMonth = now.minusMonths(2); // 2019-09-01
minusTwoMonth.getMonth().getValue(); // Gives -2 month (09)
Hope that's what you are looking for.
If you can't use Java8 syntax, use following
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MONTH, -1);
Date newDate = cal.getTime();
.... = new SimpleDateFormat("M")
This question already has answers here:
SimpleDateFormat ignoring month when parsing
(4 answers)
How to add one day to a date? [duplicate]
(18 answers)
When Using Date Picker In Android It is Picking the Wrong Date [duplicate]
(1 answer)
Closed 3 years ago.
I'm trying to add days that exceed the month days. example July 1,2019 and I add 32 days so the result would be August 2,2019.
SimpleDateFormat format = new SimpleDateFormat("mm/dd/yyy");
SimpleDateFormat Dateformat = new SimpleDateFormat("mm/dd/yyy");
String getDate = date_pick.getText().toString();
Date mDate;
Date result_desu;
try {
mDate = format.parse(getDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(mDate);
calendar.add(Calendar.DATE, 32);
String formattedDate = Dateformat.format(calendar.getTime());
date_result.setText(formattedDate); // format output
} catch (ParseException e) {
e.printStackTrace();
}
I've been using this code but it turns out the days only reset with the same month example: July 1,2019 ; result: July 2,2019.
Try this:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class Main{
public static void main(String args[]){
String oldDate = "2019-07-1";
System.out.println("Date before Addition: "+oldDate);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try{
c.setTime(sdf.parse(oldDate));
}catch(ParseException e){
e.printStackTrace();
}
c.add(Calendar.DAY_OF_MONTH, 32);
String newDate = sdf.format(c.getTime());
System.out.println("Date after Addition: "+newDate);
}
}
Output:
Date before Addition: 2019-07-1
Date after Addition: 2019-08-02
This question already has answers here:
Adding days to a date in Java [duplicate]
(6 answers)
Closed 8 years ago.
Below is the code which generates the output as "9/2/2014"
public static void main (String[]args) throws ParseException
{
java.util.Date d = new Date();
SimpleDateFormat sd = new SimpleDateFormat("M/d/yyyy");
System.out.println(sd.format(d));
}
Now i need to add some n no of days and i wanted to get the output as 9/12/2014
please help me ...
If you want add month, or days to your date, use something like that:
public static Date addDays(Date date, int days)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, days); //minus number would decrement the days
return cal.getTime();
}
to add month use Calendar.Month
Calendar has methods for date manipulations. First create Calendar instance and set date to it
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
Then you can use calendar instance to add days like
calendar.add(Calendar.DATE,10);
to get date from calendar, use
System.out.println(calendar.getTime());
This question already has answers here:
Modify the week in a Calendar
(4 answers)
Closed 5 years ago.
I am getting a Date from the object at the point of instantiation, and for the sake of outputting I need to add 2 weeks to that date. I am wondering how I would go about adding to it and also whether or not my syntax is correct currently.
Current Java:
private final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
private Date dateOfOrder;
private void setDateOfOrder()
{
//Get current date time with Date()
dateOfOrder = new Date();
}
public Date getDateOfOrder()
{
return dateOfOrder;
}
Is this syntax correct? Also, I want to make a getter that returns an estimated shipping date, which is 14 days after the date of order, I'm not sure how to add and subtract from the current date.
Use Calendar and set the current time then user the add method of the calendar
try this:
int noOfDays = 14; //i.e two weeks
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateOfOrder);
calendar.add(Calendar.DAY_OF_YEAR, noOfDays);
Date date = calendar.getTime();
I will show you how we can do it in Java 8. Here you go:
public class DemoDate {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println("Current date: " + today);
//add 2 week to the current date
LocalDate next2Week = today.plus(2, ChronoUnit.WEEKS);
System.out.println("Next week: " + next2Week);
}
}
The output:
Current date: 2016-08-15
Next week: 2016-08-29
Java 8 rocks !!
Use Calendar
Date date = ...
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.WEEK_OF_MONTH, 2);
date = c.getTime();
Try this to add two weeks.
long date = System.currentTimeMillis() + 14 * 24 * 3600 * 1000;
Date newDate = new Date(date);
if pass 14 to this addDate method it will add 14 to the current date and return
public String addDate(int days) throws Exception {
final DateFormat dateFormat1 = new SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, addDays); // Adding 5 days
return dateFormat1.format(c.getTime());
}
Using the Joda-Time library will be easier and will handle Daylight Saving Time, other anomalies, and time zones.
java.util.Date date = new DateTime( DateTimeZone.forID( "America/Denver" ) ).plusWeeks( 2 ).withTimeAtStartOfDay().toDate();
If you are on java 8 you can use new date time api http://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#plusWeeks-long-
if you are on java 7 or more old version of java you should use old api http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#add-int-int-
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get yesterday's date using Date
What is an elegant way set to a Java Date object's value to yesterday?
With JodaTime
LocalDate today = LocalDate.now();
LocalDate yesterday = today.minus(Period.days(1));
System.out.printf("Today is : %s, Yesterday : %s", today.toString("yyyy-MM-dd"), yesterday.toString("yyyy-MM-dd"));
Do you mean to go back 24 hours in time.
Date date = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
or to go back one day at the time same time (this can be 23 or 25 hours depending on daylight savings)
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
These are not exactly the same due to daylight saving.
Convert the Date to a Calendar object and "roll" it back a single day. Something like this helper method take from here:
public static void addDays(Date d, int days)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.DATE, days);
d.setTime(c.getTime().getTime());
}
For your specific case, just pass in days as -1 and you should be done. Just make sure you take into consideration the timezone/locale if doing extensive date specific manipulations.
you can try the follwing code:
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Today's date is "+dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));
As many people have already said use Calendar rather than date.
If you find you really want to use dates:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -24);
cal.getTime();//returns a Date object
Calendar cal1 = Calendar.getInstance();
cal1.add(Calendar.DAY_OF_MONTH, -1);
cal1.getTime();//returns a Date object
I hope this helps.
tomred
You can try the following example to set it to previous date.
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("Today's date is " +dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));