gpt4 book ai didi

android - 获取当月过去几周的日期

转载 作者:行者123 更新时间:2023-11-29 16:47:11 24 4
gpt4 key购买 nike

我试过了,但没有得到我需要的格式。

 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+"<<<" );
}

关于android - 获取当月过去几周的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47550187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com