Reputation: 265
public String getCurrentWeekDate() {
Calendar c = GregorianCalendar.getInstance();
System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String startDate = "", endDate = "";
startDate = df.format(c.getTime());
c.add(Calendar.DATE, 6);
endDate = df.format(c.getTime());
System.out.println("Start Date = " + startDate);
System.out.println("End Date = " + endDate);
return "";
}
this is my code from which I am getting current week date in given time format like 2017-07-17 to 2017-07-23 .
if (v.getId() == R.id.previousButtonView) {
getCurrentWeekDate();
} else if (v.getId() == R.id.nextButtonView) {
getCurrentWeekDate();
}
this is my code from which I have to display next week and previous week date please suggest me how to increase and decrease week date i am new in android.
Upvotes: 0
Views: 4958
Reputation: 11
private val calendar = Calendar.getInstance()
fun getLastWeekRange(): String {
val calendarFrom = Calendar.getInstance()
val calendarTo = Calendar.getInstance()
val dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)
val fotmat = SimpleDateFormat("dd.MM.yyyy")
if (dayOfWeek == 1) {
calendarFrom.add(Calendar.DATE, -(dayOfWeek + 12))
calendarTo.add(Calendar.DATE, -(dayOfWeek + 6))
} else {
calendarFrom.add(Calendar.DATE, -(dayOfWeek + 5))
calendarTo.add(Calendar.DATE, -(dayOfWeek - 1))
}
return "[${fotmat.format(calendarFrom.time)} - ${fotmat.format(calendarTo.time)}]"
}
Upvotes: 0
Reputation: 467
If you want the previous week start and end date, decrement currentWeekNumber by 1
else if you want next week start and end date increment currentWeekNumber by 1
//initialize Calendar
Calendar c = Calendar.getInstance();
//get current week number
int currentWeekNumber = c.get(Calendar.WEEK_OF_YEAR);
//decrement week number by 1 for the previous week
c.set(Calendar.WEEK_OF_YEAR, currentWeekNumber - 1);
//increment week number by 1 for the next week
//c.set(Calendar.WEEK_OF_YEAR, currentWeekNumber + 1);
//set first day of week
c.set(Calendar.DAY_OF_WEEK, 1);
//get start date of previous week
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String startDate = df.format(c.getTime());
//set last day of week
c.set(Calendar.DAY_OF_WEEK, 7);
//get end date of previous week
String endDate = df.format(c.getTime());
Log.d("LOG_TAG", "Start Date = " + startDate);
Log.d("LOG_TAG", "End Date = " + endDate);
Upvotes: 2
Reputation: 469
So many tries I got this result:
fun getWeekDates(currentWeek: Int): Array<String?> {
val daysOfWeek = arrayOfNulls<String>(7)
val calendar = Calendar.getInstance()
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
calendar.set(Calendar.WEEK_OF_MONTH, currentWeek)
for (i in 0..6) {
daysOfWeek[i] = changeDateFormat(LocalDate(calendar.time))
calendar.add(Calendar.DAY_OF_MONTH, 1)
}
return daysOfWeek
}
Upvotes: 0
Reputation: 4141
Since you have already found current week's date, all you have to do is subtract 7 days for previous week dates and add 7 days for next week date.
You can do that by following code
int x = -7;
Calendar cal = GregorianCalendar.getInstance();
cal.add( Calendar.DAY_OF_YEAR, x);
Date sevenDaysAgo = cal.getTime();
So your code will look like below -
public void getCurrentWeekDate(int week) {
Calendar c = GregorianCalendar.getInstance();
System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String startDate;
String endDate;
startDate = df.format(c.getTime());
// previous week calculation
Calendar previousWeekCalendar = c;
previousWeekCalendar.add( Calendar.DAY_OF_YEAR, -7);
startPreviousWeekDate = df.format(previousWeekCalendar.getTime());
previousWeekCalendar.add(Calendar.DATE, 6);
endPreviousWeekDate = df.format(previousWeekCalendar.getTime());
c.add(Calendar.DATE, 6);
//for previous week
//c.add(Calendar.DAY_OF_WEEK, -1);
//for next week
c.add(Calendar.DAY_OF_WEEK, week);
endDate = df.format(c.getTime());
// Do next week calculation same as previous week. Just check what is the value of c before starting the calculation
System.out.println("Start Date = " + startDate);
System.out.println("End Date = " + endDate);
System.out.println("End Date = " + endDate);
}
Upvotes: 0
Reputation: 4701
For next week calendar.add(Calendar.DAY_OF_WEEK, 1);
For previous week calendar.add(Calendar.DAY_OF_WEEK, -1);
public void getCurrentWeekDate(int week) {
Calendar c = GregorianCalendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
c.add(Calendar.DAY_OF_WEEK, week);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String startDate;
String endDate;
startDate = df.format(c.getTime());
c.add(Calendar.DAY_OF_MONTH, 6);
endDate = df.format(c.getTime());
System.out.println("Start Date = " + startDate);
System.out.println("End Date = " + endDate);
}
Upvotes: 1