Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to get tomorrow's date

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?

like image 960
Jocheved Avatar asked Mar 05 '14 10:03

Jocheved


People also ask

How can I get tomorrow date in Android?

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.

How can I get tomorrow date in Java?

Date today = new Date(); Date tomorrow = new Date(today. getTime() + (1000 * 60 * 60 * 24));

How can I get system date in Android?

Date c = Calendar. getInstance(). getTime(); System. out.


2 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 
like image 78
laalto Avatar answered Oct 05 '22 22:10

laalto


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 
like image 27
NaVaNeeTh PrOdHutuR Avatar answered Oct 05 '22 20:10

NaVaNeeTh PrOdHutuR