Trying to get date of Monday in current week

前端 未结 4 1539
感动是毒
感动是毒 2021-01-19 05:55

I have implemented a method to get the date of the Monday in the current week, and I have assigned Monday to be the first day of the week.

But, no matter what I do,

相关标签:
4条回答
  • 2021-01-19 06:35

    Or with JodaTime

    LocalDate.now().withDayOfWeek(DateTimeConstants.MONDAY);
    

    From Joda Time: First day of week?

    0 讨论(0)
  • 2021-01-19 06:39

    Just add one to the day of the week:

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_WEEK, cal.getActualMinimum(Calendar.DAY_OF_WEEK) + 1);
    return cal.getTime();
    
    0 讨论(0)
  • 2021-01-19 06:40

    try this :

    public static Date getFirstDayOfWeekDate() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK,
                cal.getActualMinimum(Calendar.DAY_OF_WEEK));
        Date now = new Date();
        cal.setTime(now);
        int week = cal.get(Calendar.DAY_OF_WEEK);
        return new Date(now.getTime() - 24 * 60 * 60 * 1000 * (week - 1));
    }
    
    0 讨论(0)
  • 2021-01-19 06:48

    It works for me:

        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(new Date());
        int today = c.get(Calendar.DAY_OF_WEEK);
        c.add(Calendar.DAY_OF_WEEK, -today+Calendar.MONDAY);
        System.out.println("Date "+c.getTime());
    
    0 讨论(0)
提交回复
热议问题