Java Date Now getCurrentWeekDay(Date date, int day)

Here you can find the source of getCurrentWeekDay(Date date, int day)

Description

get Current Week Day

License

Open Source License

Declaration

public static String getCurrentWeekDay(Date date, int day) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;

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

public class Main {
    /**/*from ww w. j a v  a2  s .  com*/
     * default date format pattern
     */
    public final static String DATE_FORMAT = "yyyy-MM-dd";
    private static final int DAYS_OF_A_WEEK = 7;

    public static String getCurrentWeekDay(Date date, int day) {
        Calendar calendar = getCalendarFromDate(date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        int year = calendar.get(Calendar.YEAR);

        return formatDate(getDateOfYearWeek(year, week, day));
    }

    /**
     * get calendar from date
     *
     * @param date the passing date
     * @return the calendar instance
     */
    public static Calendar getCalendarFromDate(Date date) {
        Calendar calendar = getDefaultCalendar();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * format date with the default pattern
     *
     * @param date the date that want to format to string
     * @return the formated date
     */
    public static String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
        return format.format(date);
    }

    /**
     * format date with the given pattern
     *
     * @param date the date that want to format to string
     * @param pattern the formated pattern
     * @return the formated date
     */
    public static String formatDate(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    private static Date getDateOfYearWeek(int yearNum, int weekNum, int dayOfWeek) {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        cal.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
        cal.set(Calendar.YEAR, yearNum);
        cal.set(Calendar.WEEK_OF_YEAR, weekNum);
        /*cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);*/
        return cal.getTime();
    }

    /**
     * get the default calendar
     *
     * @return the calendar instance
     */
    public static Calendar getDefaultCalendar() {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        return calendar;
    }
}

Related

  1. getCurrentUtcDate()
  2. getCurrentUTCSeconds(Date current)
  3. getCurrentUTCString()
  4. getCurrentUTCTime()
  5. getCurrentUTCTimeAsString()
  6. getCurrentWeekDays()
  7. getCurrentWeekFirst()
  8. getCurrentYear()
  9. getCurrentYearBegDate(Date date)