获取当月过去几周的日期
Get past weeks days date of current month
我试过了,但没有得到我需要的格式。
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
for (int i = 7; i >=1; i--) {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DATE, i);
String day = sdf.format(calendar.getTime());
Log.i("days", day);
Wednesday 06-Dec-2017
Tuesday 05-Dec-2017
Monday 04-Dec-2017
Sunday 03-Dec-2017
Saturday 02-Dec-2017
Friday 01-Dec-2017
Thursday 30-Nov-2017
我想要这样的格式,一周的开始日期和一周的结束日期..
Oct 1-7 Oct 8-14 Oct 15-21
试试这个代码,
public static void getWeeksOfMonth(int month, int year)
{
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 1);
int ndays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(ndays+"<<<ff" );
for (int i = 1; i <= ndays; i++)
{
String day = sdf.format(cal.getTime());
System.out.println(day+"<<<" );
Log.e("", day+"<<<");
if(i % 7 == 0){
Log.e("", "=======week days===========");
}
cal.add(Calendar.DATE, 1);
}
}
// only previous week days get
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
for (int i = 7; i >=1; i--) {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DATE, -i);
String day = sdf.format(calendar.getTime());
System.out.println(day+"<<<" );
}
我试过了,但没有得到我需要的格式。
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
for (int i = 7; i >=1; i--) {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DATE, i);
String day = sdf.format(calendar.getTime());
Log.i("days", day);
Wednesday 06-Dec-2017 Tuesday 05-Dec-2017 Monday 04-Dec-2017 Sunday 03-Dec-2017 Saturday 02-Dec-2017 Friday 01-Dec-2017 Thursday 30-Nov-2017
我想要这样的格式,一周的开始日期和一周的结束日期..
Oct 1-7 Oct 8-14 Oct 15-21
试试这个代码,
public static void getWeeksOfMonth(int month, int year)
{
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 1);
int ndays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(ndays+"<<<ff" );
for (int i = 1; i <= ndays; i++)
{
String day = sdf.format(cal.getTime());
System.out.println(day+"<<<" );
Log.e("", day+"<<<");
if(i % 7 == 0){
Log.e("", "=======week days===========");
}
cal.add(Calendar.DATE, 1);
}
}
// only previous week days get
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
for (int i = 7; i >=1; i--) {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DATE, -i);
String day = sdf.format(calendar.getTime());
System.out.println(day+"<<<" );
}