Java Date Now getDayToMonthEnd(boolean iscurrentdate)

Here you can find the source of getDayToMonthEnd(boolean iscurrentdate)

Description

get Day To Month End

License

Apache License

Declaration

public static List<String> getDayToMonthEnd(boolean iscurrentdate) 

Method Source Code

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;

import java.util.List;

public class Main {

    public static List<String> getDayToMonthEnd(boolean iscurrentdate) {
        List<String> list = new ArrayList<String>();
        Calendar calendar = new GregorianCalendar();
        int cday = calendar.get(Calendar.DAY_OF_MONTH);
        int currentyear = calendar.get(Calendar.YEAR);
        int currentmonth = calendar.get(Calendar.MONTH) + 1;
        if (iscurrentdate) {
            list.add(getYearAndMonthAndDay(currentyear, currentmonth, cday));
        }//from  ww  w.j  a v a2  s .  c  om
        int maxDate = getMaxDayByYearMonth(getCustomerDay("yyyy-MM"));
        for (int i = cday + 1; i <= maxDate; i++) {
            list.add(getYearAndMonthAndDay(currentyear, currentmonth, i));
        }
        Collections.sort(list);
        return list;
    }

    private static String getYearAndMonthAndDay(int year, int month, int day) {
        String m = String.valueOf(month < 10 ? "0" + month : month);
        String d = String.valueOf(day < 10 ? "0" + day : day);
        String tmp = year + "-" + m + "-" + d;
        return tmp;
    }

    public static int getMaxDayByYearMonth(String ym) {
        int day = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar calendar = new GregorianCalendar();
            Date date = sdf.parse(ym);
            calendar.setTime(date);
            day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        } catch (ParseException e) {
            day = 0;
        }
        return day;
    }

    public static String getCustomerDay(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }
}

Related

  1. getCurrentWeekFirst()
  2. getCurrentYear()
  3. getCurrentYearBegDate(Date date)
  4. getCurrentYMDDate()
  5. getCurrentZeroTime()
  6. getNowDate()
  7. getNowDate()
  8. getNowDate()
  9. getNowDate()