Java Date to Month getMonthDays(String date)

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

Description

get Month Days

License

Open Source License

Declaration

public static int[][] getMonthDays(String date) 

Method Source Code


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

public class Main {
    public static int[][] getMonthDays(String date) {
        int[][] months = new int[6][7];
        int days = 1;

        int year = getYear(date);
        int month = getMonth(date);
        int day = getDate(date);

        for (int i = getFirstDayOfWeek(year, month); i < 7; i++) {
            months[0][i] = days;//from ww  w  .j a va  2s .co  m
            days++;
        }

        for (int i = 1; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                if (days <= LastDateInMonth(year, month, day)) {
                    months[i][j] = days;
                    days++;
                } else
                    break;
            }
        }

        return months;
    }

    public static int getYear(String date) {
        return Integer.parseInt(date.substring(0, 4));
    }

    public static int getMonth(String date) {
        return Integer.parseInt(date.substring(4, 6));
    }

    public static int getDate(String date) {
        return Integer.parseInt(date.substring(6, 8));
    }

    public static int getFirstDayOfWeek(int _year, int _month) {
        Calendar tmpCal = Calendar.getInstance();
        tmpCal.set(_year, _month - 1, 1);

        return (tmpCal.get(Calendar.DAY_OF_WEEK) - 1);
    }

    public static int LastDateInMonth(int _year, int _month, int _day) {
        Calendar tmpCal = Calendar.getInstance();
        tmpCal.set(Calendar.YEAR, _year);
        tmpCal.set(Calendar.MONTH, _month);
        tmpCal.set(Calendar.DATE, 0);

        return (tmpCal.get(Calendar.DATE));
    }
}

Related

  1. getMonth(long date, int increment)
  2. getMonth(String strDate)
  3. getMonthBeginDate()
  4. getMonthBetween(Date data1, Date data2)
  5. getMonthByOffset(Date date, int offset)
  6. getMonthFirstDay(Date date)
  7. getMonthFromDate(Date date)
  8. getMonthInterval(Date _one, Date _two)
  9. getMonthLastDate(Date date)