Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding days to a date

I have a program that needs to start on 1/1/09 and when I start a new day, my program will show the next day. This is what I have so far:

GregorianCalendar startDate = new GregorianCalendar(2009, Calendar.JANUARY, 1);
SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy"); 
public void setStart()
{
    startDate.setLenient(false);
    System.out.println(sdf.format(startDate.getTime()));
}

public void today()
{
    newDay = startDate.add(5, 1);
    System.out.println(newDay);
//I want to add a day to the start day and when I start another new day, I want to add another day to that.
}

I am getting the error found void but expected int, in 'newDay = startDate.add(5, 1);' What should I do?

like image 634
Karen Avatar asked Sep 13 '09 05:09

Karen


People also ask

How do you calculate number of days to date?

To calculate the number of days between two dates, you need to subtract the start date from the end date. If this crosses several years, you should calculate the number of full years. For the period left over, work out the number of months. For the leftover period, work out the number of days.

Can you add 30 days to a date in Excel?

In cell C1, type =A1+30, and then press RETURN . This formula adds 30 days to the date in cell A1.

How do you add days to a date in Excel without formula?

Adding days to a date in Excel. The date can be entered in several ways: As a cell reference, e.g. =A2 + 10. Using the DATE(year, month, day) function, e.g. =DATE(2015, 5, 6) + 10.


2 Answers

If you can swing it requirement wise, move all your date/time needs to JODA, which is a much better library, with the added bonus that almost everything is immutable, meaning multithreading comes in for free.

like image 20
user44242 Avatar answered Oct 20 '22 10:10

user44242


The Calendar object has an add method which allows one to add or subtract values of a specified field.

For example,

Calendar c = new GregorianCalendar(2009, Calendar.JANUARY, 1);
c.add(Calendar.DAY_OF_MONTH, 1);

The constants for specifying the field can be found in the "Field Summary" of the Calendar class.

Just for future reference, The Java API Specification contains a lot of helpful information about how to use the classes which are part of the Java API.


Update:

I am getting the error found void but expected int, in 'newDay = startDate.add(5, 1);' What should I do?

The add method does not return anything, therefore, trying to assign the result of calling Calendar.add is not valid.

The compiler error indicates that one is trying to assign a void to a variable with the type of int. This is not valid, as one cannot assign "nothing" to an int variable.

Just a guess, but perhaps this may be what is trying to be achieved:

// Get a calendar which is set to a specified date.
Calendar calendar = new GregorianCalendar(2009, Calendar.JANUARY, 1);

// Get the current date representation of the calendar.
Date startDate = calendar.getTime();

// Increment the calendar's date by 1 day.
calendar.add(Calendar.DAY_OF_MONTH, 1);

// Get the current date representation of the calendar.
Date endDate = calendar.getTime();

System.out.println(startDate);
System.out.println(endDate);

Output:

Thu Jan 01 00:00:00 PST 2009
Fri Jan 02 00:00:00 PST 2009

What needs to be considered is what Calendar actually is.

A Calendar is not a representation of a date. It is a representation of a calendar, and where it is currently pointing at. In order to get a representation of where the calendar is pointing at at the moment, one should obtain a Date from the Calendar using the getTime method.

like image 187
coobird Avatar answered Oct 20 '22 08:10

coobird