[Q#12783102][A#12783806] How to get the last Sunday before current date?
https://stackoverflow.com/q/12783102
I have the following code for getting the last Sunday before the current date: But this code doesn't work. Please, tell me, how can I fix it?
Answer
https://stackoverflow.com/a/12783806
This will work. We first get the day count, and then subtract that with the current day and add 1 ( for sunday)
APIzation
Calendar cal=Calendar.getInstance();
cal.add( Calendar.DAY_OF_WEEK, -(cal.get(Calendar.DAY_OF_WEEK)-1));
System.out.println(cal.get(Calendar.DATE));
package com.stackoverflow.api;
import java.util.Calendar;
public class Human12783806 {
public static int getLastSunday() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_WEEK, -(cal.get(Calendar.DAY_OF_WEEK) - 1));
return cal.get(Calendar.DATE);
}
}
package com.stackoverflow.api;
import java.util.Calendar;
/**
* How to get the last Sunday before current date?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/12783806">https://stackoverflow.com/a/12783806</a>
*/
public class APIzator12783806 {
public static int get() throws Exception {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_WEEK, -(cal.get(Calendar.DAY_OF_WEEK) - 1));
return cal.get(Calendar.DATE);
}
}