Java SQL Date Month getMulmonthday(String startdate, String enddate)

Here you can find the source of getMulmonthday(String startdate, String enddate)

Description

get Mulmonthday

License

Apache License

Declaration

public static String getMulmonthday(String startdate, String enddate) 

Method Source Code


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

import java.text.SimpleDateFormat;
import java.sql.Date;

import java.util.Vector;

public class Main {
    static public SimpleDateFormat MMddYYYY_HHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static public SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");

    public static String getMulmonthday(String startdate, String enddate) {
        String pandy = "";
        Vector tempdate = getmonthfl(startdate, enddate);
        String temp[] = new String[2];
        for (int i = 0; i < tempdate.size(); i++) {
            temp = (String[]) tempdate.get(i);
            pandy += temp[0] + "--" + temp[1] + ",";

        }//  w  ww  .ja v  a 2  s  .  c  o m
        if (pandy.length() > 0)
            pandy = pandy.substring(0, pandy.length() - 1);
        return pandy;
    }

    public static Vector getmonthfl(String startdate, String enddate) {
        Vector pandy = new Vector();
        String tempdate = getCurMonthDayStr(startdate);
        while (tempdate.compareTo(enddate) <= 0) {
            String pan[] = new String[2];
            pan[0] = tempdate;
            pan[1] = getPreDayStr(getNextMonthDayStr(tempdate));
            pandy.add(pan);
            tempdate = getNextMonthDayStr(tempdate);
        }
        return pandy;
    }

    public static String getCurMonthDayStr(String curday) {
        int year = getYear(curday);
        String monthStr = getMonth(curday);
        int month = Integer.parseInt(monthStr);
        monthStr = String.valueOf(month);
        if (monthStr.length() == 1)
            monthStr = "0" + monthStr;
        return year + "-" + monthStr + "-01";
    }

    public static String getPreDayStr(String curday) {
        java.util.Date date = str2utilDate(curday);
        return date2str(new Date(date.getTime() - 24 * 3600 * 1000));

    }

    public static String getNextMonthDayStr(String curday) {
        int year = getYear(curday);
        String monthStr = getMonth(curday);
        int month = Integer.parseInt(monthStr);
        if (month >= 12) {
            month = 1;
            year = year + 1;
        } else
            month = month + 1;
        monthStr = String.valueOf(month);
        if (monthStr.length() == 1)
            monthStr = "0" + monthStr;
        return year + "-" + monthStr + "-01";
    }

    public static int getYear(String day) {
        if (day == null)
            return 0;
        if (day.length() < 8)
            return 0;
        return Integer.parseInt(day.substring(0, 4));
    }

    public static String getMonth(String day) {
        if (day == null)
            return "0";
        if (day.length() < 8)
            return "0";
        int m = day.indexOf("-", 0);
        int n = day.lastIndexOf("-");
        String temp = day.substring(m + 1, n);
        if (temp.length() == 1)
            temp = "0" + temp;
        return temp;
    }

    public static java.util.Date str2utilDate(String str) {
        try {
            java.util.Date udate = yyyyMMdd.parse(str);
            return udate;
        } catch (Exception e) {
            System.out.println("DateUtil.str2utilDate(str) Error:e = " + e);
            return null;
        }
    }

    public static String date2str(java.util.Date date) {
        if (date == null)
            return "";
        try {
            return MMddYYYY_HHmmss.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    static public String date2str(java.sql.Date date) {
        if (date == null)
            return "";
        try {
            return yyyyMMdd.format(date);
        } catch (Exception e) {
            return "";
        }

    }
}

Related

  1. getMonthBefroe(Date date)
  2. getMonthDate(Date myDate, int month)
  3. getMonthDays(java.sql.Date date)
  4. getMonthFromYMD(Date ymd)
  5. getMonthStart(Date stamp)
  6. getNextMonthFirstDate(java.sql.Date date)
  7. getNextMonthStartDate(Date date)
  8. getSqlDate(final int iDay, final int iMonth, final int iYear)
  9. getSqlDate(int year, int month, int day)