Show 12 days before date as current date

I created a custom Calendar.It shows the device date as the current date. But my requirement is to show 12 days before the date as the current date. for example, today is 21, but I want my calendar to show 9. I want to do the same with the year. I know how to get the current date. I am using this code.

_calendar = Calendar.getInstance(Locale.getDefault());
month = _calendar.get(Calendar.MONTH) + 1;
year = _calendar.get(Calendar.YEAR);
Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: " + year);

My new code

_calendar = Calendar.getInstance(Locale.getDefault());
month = _calendar.get(Calendar.MONTH) + 1;
year = _calendar.get(Calendar.YEAR);
Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: " + year);
/*_calendar.add(Calendar.DAY_OF_YEAR, -12);*/
_calendar.roll(Calendar.DAY_OF_MONTH, -12);
_calendar.roll(Calendar.MONTH, -12);
Calendar _calendar=Calendar.getInstance();
+5
source share
4 answers

Try:

_calendar.roll(Calendar.DAY_OF_YEAR, -12);
_calendar.roll(Calendar.YEAR, -12);

This will roll back 12 days, and then a year 12.

+1
source

NOW - , DateTime , . add (-) , , . , , , .;-)

, .

0

Give it a try.

_calendar = Calendar.getInstance(Locale.getDefault());
_calendar.setTime(new Date());
_calendar.add(Calendar.DATE, -12);
Date twelveDaysAgo = _calendar.getTime();
0
source

This will return the date by 12 days, and also change the month / year if necessary.

_calendar.add(Calendar.DAY_OF_YEAR, -12);
0
source

All Articles