36

In my android application. I need to display tomorrow's date, for example today is 5th March so I need to display as 6 March. I know the code for getting today's date, month and year.

date calculating

    GregorianCalendar gc = new GregorianCalendar();
    yearat = gc.get(Calendar.YEAR);
    yearstr = Integer.toString(yearat);
    monthat = gc.get(Calendar.MONTH) + 1;
    monthstr = Integer.toString(monthat);
    dayat = gc.get(Calendar.DAY_OF_MONTH);
    daystr = Integer.toString(dayat);

If I have the code

dayat = gc.get(Calendar.DAY_OF_MONTH) + 1;

will it display tomorrow's date. or just add one to today's date? For example, if today is January 31. With the above code, will it display like 1 or 32? If it displays 32, what change I need to make?

jpp
  • 159,742
  • 34
  • 281
  • 339
Jocheved
  • 1,125
  • 3
  • 15
  • 32

16 Answers16

62
  1. Get today's date as a Calendar.

  2. Add 1 day to it.

  3. Format for display purposes.

For example,

GregorianCalendar gc = new GregorianCalendar();
gc.add(Calendar.DATE, 1);
// now do something with the calendar
laalto
  • 150,114
  • 66
  • 286
  • 303
57

Use the following code to display tomorrow date

Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();

calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();

Use SimpleDateFormat to format the Date as a String:

DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");

String todayAsString = dateFormat.format(today);
String tomorrowAsString = dateFormat.format(tomorrow);

System.out.println(todayAsString);
System.out.println(tomorrowAsString);

Prints:

05-Mar-2014
06-Mar-2014
8
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();

calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
Pararth
  • 8,114
  • 4
  • 34
  • 51
5

you have to add just 1 in your Calendar Day.

GregorianCalendar gc = new GregorianCalendar();
gc.add(Calendar.DATE, 1);
infused
  • 24,000
  • 13
  • 68
  • 78
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • 1
    Does it compile? (Thanks for copying my brainfart :) – laalto Mar 05 '14 at 10:47
  • 1
    @laalto Not Copying you. Just generally think that tomorrow is after one day. So try to give answer that's way. BTW your description is much better. So no worry for that. I know you have a good and best brainfart Thanks anywhy!! – Piyush Mar 05 '14 at 11:29
4

java.util.Date and java.util.Calendar are terrible to work with. I suggest you use JodaTime which has a much cleaner / nicer API. JodaTime is pretty standard these days.

http://www.joda.org/joda-time/#Why_Joda-Time

Note that JDK 8 will introduce a new date/time API heavily influenced by JodaTime.

lance-java
  • 25,497
  • 4
  • 59
  • 101
4

Other options:

Calendar tomorrow = Calendar.getInstance();
tomorrow.roll(Calendar.DATE, true);

or

tomorrow.roll(Calendar.DATE, 1);

roll can also be used to go back in time by passing a negative number, so for example:

Calendar yesterday = Calendar.getInstance();
yesterday.roll(Calendar.DATE, -1);
Alberto Malagoli
  • 1,173
  • 10
  • 14
3

the first answers pretty much covers the possibilities. but here one another solution which you can use from org.apache.commons.lang.time:

Date lTomorrow = DateUtils.addDays(new Date(), 1);
nano_nano
  • 12,351
  • 8
  • 55
  • 83
2

The java.util.Date and .Calendar classes are notoriously troublesome. Avoid them. Instead use either Joda-Time library or the new java.time package in bundled with Java 8.

Some example code using the Joda-Time 2.3 library.

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime now = new DateTime( timeZone );
DateTime tomorrow = now.plusDays( 1 );
String output = DateTimeFormat.forPattern( "FF" ).withLocale(Locale.FRANCE).print( tomorrow );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Get todays date by using calendar and then add 1 day to it.

roshanpeter
  • 1,334
  • 3
  • 13
  • 32
1

This is working to me well!!

    Date currentDate = new Date();// get the current date
    currentDate.setDate(currentDate.getDate() + 1);//add one day to the current date
    dateView.setText(currentDate.toString().substring(0, 10));// put the string in specific format in my textView

good luck!!

yoc
  • 214
  • 2
  • 13
1

much easier now

String today = LocalDateTime.now().format(DateTimeFormatter.ofPattern("MM-dd"));

String tomorrow = LocalDate.now().plusDays(1).format(DateTimeFormatter.ofPattern("MM-dd"));
NEDZAD
  • 33
  • 6
0

Try like this..

dayat = gc.add(Calendar.DATE, 1);
Naveen
  • 1,948
  • 4
  • 17
  • 21
0

tl;dr

java.time.LocalDate.now()
                   .plusDays( 1 ) 

java.time

All the other Answers are outmoded, using the troublesome old Date & Calendar classes or the Joda-Time project which is now in maintenance mode. The modern approach uses the java.time classes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;

From that LocalDate you can do math to get the following day.

LocalDate tomorrow = today.plusDays( 1 ) ;

Strings

To generate a String representing the LocalDate object’s value, call toString for text formatted per the ISO 8601 standard: YYYY-MM-DD.

To generate strings in other formats, search Stack Overflow for DateTimeFormatter.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Best way for setting next day is

 public void NextDate()
{

    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    // set current date into textview
    e_date.setText(new StringBuilder()
            // Month is 0 based, just add 1
            .append(mDay+1).append("-").append(mMonth + 1).append("-")
            .append(mYear).append(" "));



}
Kunal Nikam
  • 79
  • 1
  • 11
0
Just call this method and send date from which you want next date     

public String nextDate(Date dateClicked) {
           // 
String next_day;
calander_view.setCurrentDayTextColor(context.getResources().getColor(R.color.white_color));
            //calander_view.setCurrentDayBackgroundColor(context.getResources().getColor(R.color.gray_color));
            SimpleDateFormat dateFormatForDisplaying = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
            String date_format = dateFormatForDisplaying.format(dateClicked);
            SimpleDateFormat simpleDateformat = new SimpleDateFormat("E"); // the day of the week abbreviated
            final Calendar calendar = Calendar.getInstance();
            try {
                Date date = dateFormatForDisplaying.parse(date_format);
                calendar.setTime(date);
                calendar.add(Calendar.DATE, 1);
                String nex = dateFormatForDisplaying.format(calendar.getTime());
                Date d1 = dateFormatForDisplaying.parse(nex);
                String day_1 = simpleDateformat.format(d1);
                next_day = nex + ", " + day_1;
            } catch (ParseException e) {
                e.printStackTrace();
            }

            return next_day;

        }
Mohit Hooda
  • 273
  • 3
  • 9
0
 String  lastDate="5/28/2018";
        Calendar calendar = Calendar.getInstance();
        String[] sDate=lastDate.split("/");
        calendar.set(Integer.parseInt(sDate[2]),Integer.parseInt(sDate[0]),Integer.parseInt(sDate[1]));

        DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");

//        String todayAsString = dateFormat.format(today);

        for (int i=1;i<29;i++)
        {
            calendar.add(Calendar.DAY_OF_YEAR,1);
//            td[i].setText(dateFormat.format(calendar.getTime()));
          System.out.println(dateFormat.format(calendar.getTime()));  
        }
pruthwiraj.kadam
  • 1,033
  • 10
  • 11