Home » Android » java – Android how to get tomorrow's date

java – Android how to get tomorrow's date

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:
  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

Answer:

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

Answer:

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

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

Answer:

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.

Answer:

you have to add just 1 in your Calendar Day.

GregorianCalendar gc = new GregorianCalendar();
gc.add(Calendar.DATE, 1);

Answer:

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);

Answer:

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);

Answer:

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 );

Answer:

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

Answer:

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!!

Answer:

Try like this..

dayat = gc.add(Calendar.DATE, 1);

Answer:

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?

Answer:

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(" "));



}

Answer:

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;

        }

Answer:

 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()));  
        }