Hi I am working on a project where I need to get the date of the first day of the week in format of yyyy-MM-dd
and show it in a TextView, I tried this solution but I got confused with using Date
and Calendar
classes, I tried the below code but always I get the complete date string or wrong date my overall purpose is to get date of the first day of the week any one who write the code to do this or explain the difference between Date
and Calendar
classes I would appreciate.
Thanks
here is the code
int year, month, day;
tvTest = (TextView) findViewById(R.id.tvTheoryStudies);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);
String todayDate = new StringBuilder().append(year).append("-").append(month).append("-").append(day).toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cal.set(Calendar.DAY_OF_WEEK, 1);
Date date =new Date();
try {
date = sdf.parse(todayDate);
}
catch (Exception e){
e.printStackTrace();
}
cal.setTime(date);
date = cal.getTime();
tvTest.setText(date.toString());