Java Week Day getMonday(String date)

Here you can find the source of getMonday(String date)

Description

get Monday

License

Apache License

Declaration

public static String getMonday(String date) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    public static String getMonday(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");
        Date d = null;//from w w w  . ja  v  a 2s.com
        try {
            d = format.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        int days = cal.get(Calendar.DAY_OF_WEEK);
        if (days == 1) {
            days = 1;
            String preMonday = getPreviousMonday(d, days);
            System.out.println("preMonday" + preMonday);
            return preMonday;
        } else {
            cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            return format2.format(cal.getTime());
        }
    }

    public static String getPreviousMonday(Date date, int days) {
        int weeks = -1;
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.setTime(date);
        currentDate.add(GregorianCalendar.DATE, days + 7 * weeks);
        Date monday = currentDate.getTime();
        DateFormat df = DateFormat.getDateInstance();
        String preMonday = df.format(monday);
        return preMonday;
    }
}

Related

  1. getLastWeekday()
  2. getLastWeekDay(int weekDay)
  3. getLastWeekDay(int weekDay)
  4. getLastWeekMs()
  5. getMon()
  6. getMonday(String date, int weekDay)
  7. getMondayAfter(Date date)
  8. getMondayOfThisWeek()
  9. getNextDayOfweek()