How to get all week dates for given date java

后端 未结 3 1375
不知归路
不知归路 2020-12-03 18:28

i have a date and how to get all the dates fall on the week that the given date belongs in java?

example:

if i give today\'s date then i should get all date         


        
3条回答
  •  执念已碎
    2020-12-03 19:26

    You can try following way,

    Calendar cal = Calendar.getInstance();
    //cal.setTime(new Date());//Set specific Date if you want to
    
    for(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
        cal.set(Calendar.DAY_OF_WEEK, i);
        System.out.println(cal.getTime());//Returns Date
    }
    

    OUTPUT

    Sun Jul 12 08:12:38 IST 2015
    Mon Jul 13 08:12:38 IST 2015
    Tue Jul 14 08:12:38 IST 2015
    Wed Jul 15 08:12:38 IST 2015
    Thu Jul 16 08:12:38 IST 2015
    Fri Jul 17 08:12:38 IST 2015
    Sat Jul 18 08:12:38 IST 2015
    

提交回复
热议问题