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

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

Description

getmonthfl

License

Apache License

Declaration

public static Vector getmonthfl(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 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;/*  w  ww . ja  v  a  2  s. c  om*/
            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. getMonth(final java.sql.Date date)
  2. getMonthAmount(java.sql.Date _startDate, java.sql.Date _endDate)
  3. getMonthBefroe(Date date)
  4. getMonthDate(Date myDate, int month)
  5. getMonthDays(java.sql.Date date)
  6. getMonthFromYMD(Date ymd)
  7. getMonthStart(Date stamp)
  8. getMulmonthday(String startdate, String enddate)
  9. getNextMonthFirstDate(java.sql.Date date)