First, let me walk you through the process.
I want to custom week calendar which i have made. It is work fine but problem is when i run same code in Samsung SM-T531 and Moto G it will return me previous Week.
Let me explain with code.
public WeekCalendar(Context context) {
this.mContext = context;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.setFirstDayOfWeek(Calendar.MONDAY);
Log.e(TAG, "Formatted Current Date Before - " + format.format(calendar.getTime()));
Log.e(TAG, "Current Date Before - " + calendar.getTime());
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Log.e(TAG, "Formatted Current Date After - " + format.format(calendar.getTime()));
Log.e(TAG, "Current Date After - " + calendar.getTime());
String[] days = new String[7];
for (int i = 0; i < 7; i++) {
days[i] = format.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
Log.e(TAG, "List days " + "" + days[i]);
}
weekDataModel.setDays(days);
weekDataModel.setCurruntdate(Utility.getCurrentDate());
}
Output of This code
Formatted Current Date Before - 2018-04-20
Current Date Before - Fri Apr 20 15:25:58 GMT+05:30 2018
Formatted Current Date After - 2018-04-09
Current Date After - Mon Apr 09 15:25:58 GMT+05:30 2018
List days 2018-04-09
List days 2018-04-10
List days 2018-04-11
List days 2018-04-12
List days 2018-04-13
List days 2018-04-14
List days 2018-04-15
Current date is 20-4-2018 from calendar.getTime() it is correct but after adding below line current date changed, i am not understand why it is happen.
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
You can see my output before and after date.