Java Day of Week getDateWeek(String date)

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

Description

get Date Week

License

Open Source License

Declaration

public static int getDateWeek(String date) 

Method Source Code


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

public class Main {

    public static int getDateWeek(String date) {
        Calendar calendar = toCalendar(date + " 00:00:01");
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        week = week - 1;/*from w ww . j  a v  a 2 s.c  o m*/
        if (week == 0)
            week = 7;

        return week;
    }

    public static Calendar toCalendar(String yearStr, String monthStr, String dayStr, String hourStr,
            String minuteStr, String secondStr) {
        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(yearStr);
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
        } catch (Exception e) {
            return null;
        }

        return toCalendar(year, month, day, hour, minute, second);
    }

    public static Calendar toCalendar(String datetime) {
        int index = datetime.indexOf(" ");
        String date = datetime.substring(0, index);
        String time = datetime.substring(index + 1);

        int dateSlash1 = date.indexOf("-");
        int dateSlash2 = date.lastIndexOf("-");

        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2)
            return null;

        int timeColon1 = time.indexOf(":");
        int timeColon2 = time.lastIndexOf(":");

        if (timeColon1 <= 0 || timeColon1 == timeColon2)
            return null;

        String yearStr = date.substring(0, dateSlash1);
        String monthStr = date.substring(dateSlash1 + 1, dateSlash2);
        String dayStr = date.substring(dateSlash2 + 1);

        String hourStr = time.substring(0, timeColon1);
        String minuteStr = time.substring(timeColon1 + 1, timeColon2);
        String secondStr = time.substring(timeColon2 + 1);
        ;

        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(yearStr);
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
            Calendar calendar = Calendar.getInstance();

            calendar.set(year, month - 1, day, hour, minute, second);
            return calendar;
        } catch (Exception e) {
            return null;
        }
    }

    public static Calendar toCalendar(int[] dates) {
        if (dates == null || dates.length < 6)
            return null;

        return toCalendar(dates[0], dates[1], dates[2], dates[3], dates[4], dates[5]);
    }

    public static Calendar toCalendar(int year, int month, int day, int hour, int minute, int second) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DATE, day);
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, second);

        return c;
    }
}

Related

  1. getDateByYearAndWeekNum(int year, int weekNum)
  2. getDateForCurrentWeekDay(int day)
  3. getDateOfWeek(int week)
  4. getDateOfWeek(int year, int weekOfYear, int i)
  5. getDateOfYearWeek(int yearNum, int weekNum, int dayOfWeek)
  6. getDay(long date, int startOfWeek, int increment)
  7. getDayAfterGivenDate(Date date, int dayOfWeek)
  8. getDayOfWeek()
  9. getDayOfWeek(Date date)