Java Day of Week getLastDayWeek(String s)

Here you can find the source of getLastDayWeek(String s)

Description

get Last Day Week

License

Open Source License

Parameter

Parameter Description
s a parameter

Exception

Parameter Description
ParseException an exception

Declaration

public static String getLastDayWeek(String s) throws ParseException 

Method Source Code


//package com.java2s;

import java.text.ParseException;
import java.text.SimpleDateFormat;

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

import java.util.Locale;

public class Main {
    /**/*w  w  w .ja  v  a 2 s. c  o m*/
     *
     * @param s
     * @return
     * @throws ParseException
     */
    public static String getLastDayWeek(String s) throws ParseException {
        int minuscnt = whichDay(s);

        minuscnt = minuscnt == 1 ? 0 : 7 - minuscnt + 1;

        return addDays(s, minuscnt);
    }

    public static int whichDay(String s) throws ParseException {
        return whichDay(s, "yyyyMMdd");
    }

    public static int whichDay(String s, String format) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.KOREA);
        Date date = check(s, format);

        Calendar calendar = formatter.getCalendar();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * return add day to date strings with user defined format.
     * @param s  input date
     * @param day  date value
     * @return addDays result
     * @throws ParseException  error info
     */
    public static String addDays(String s, int day) throws ParseException {
        return addDays(s, day, "yyyyMMdd");
    }

    /**
     * return add day to date strings with user defined format.
     * @param s  input date
     * @param day  date value
     * @param format date format
     * @return  addDays result
     * @throws ParseException   error info
     */
    public static String addDays(String s, int day, String format) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.KOREA);
        Date date = check(s, format);

        date.setTime(date.getTime() + ((long) day * 1000 * 60 * 60 * 24));
        return formatter.format(date);
    }

    /**
     *
     * @param s   date string you want to check.
     * @param format string representation of the date format. For example, "yyyy-MM-dd".
     * @return  date Date
     * @throws ParseException error info
     */
    public static Date check(String s, String format) throws ParseException {
        if (s == null)
            throw new ParseException("date string to check is null", 0);
        if (format == null)
            throw new ParseException("format string to check date is null", 0);

        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.KOREA);
        Date date = null;
        try {
            date = formatter.parse(s);
        } catch (ParseException e) {
            throw new ParseException(" wrong date:\"" + s + "\" with format \"" + format + "\"", 0);
        }

        if (!formatter.format(date).equals(s))
            throw new ParseException("Out of bound date:\"" + s + "\" with format \"" + format + "\"", 0);
        return date;
    }
}

Related

  1. getLastDayOfWeek(Date date)
  2. getLastDayOfWeek(Date date)
  3. getLastDayOfWeek(Date date)
  4. getLastDayOfWeek(Date date)
  5. getLastDayOfWeek(String str, int week)
  6. getLastMondayOfWeek(Date date)
  7. getLastWeekDate(Date date)
  8. getLastWeekDay(int weekDay, Date end)
  9. getMondayFirstOfWeek(Date baseDate)