find Nearest Previous Monday - Java java.util

Java examples for java.util:Day

Description

find Nearest Previous Monday

Demo Code


//package com.java2s;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static void main(String[] argv) throws Exception {
        Date actual = new Date();
        System.out.println(findNearestPrevMonday(actual));
    }//w  w w.  ja  v a2s  .  c  o m

    private static Calendar calendar;

    public static Date findNearestPrevMonday(Date actual) {
        calendar.setTime(actual);

        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DATE, -1);
        }

        return calendar.getTime();
    }
}

Related Tutorials