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?
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.
Date today = new Date(); Date tomorrow = new Date(today. getTime() + (1000 * 60 * 60 * 24));
Date c = Calendar. getInstance(). getTime(); System. out.
Get today's date as a Calendar
.
Add 1 day to it.
Format for display purposes.
For example,
GregorianCalendar gc = new GregorianCalendar(); gc.add(Calendar.DATE, 1); // now do something with the calendar
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With