Java Month Get getMonthEnd(String strdate)

Here you can find the source of getMonthEnd(String strdate)

Description

get Month End

License

Apache License

Declaration

public static String getMonthEnd(String strdate) 

Method Source Code

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

import java.text.DateFormat;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static String getMonthEnd(String strdate) {
        Date date = parseDate(getMonthBegin(strdate));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);/*from   w  ww  .j  a  v a2s.c  o m*/
        calendar.add(2, 1);
        calendar.add(6, -1);
        return formatDate(calendar.getTime());
    }

    public static Date parseDate(String dateStr, String format) {
        Date date = null;
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);

            date = dateFormat.parse(dateStr);
        } catch (Exception localException) {
        }
        return date;
    }

    public static Date parseDate(String dateStr) {
        return parseDate(dateStr, "yyyy-MM-dd");
    }

    public static String getMonthBegin(String strdate) {
        Date date = parseDate(strdate);
        return format(addDate(null, 0), "yyyy-MM") + "-01";
    }

    public static String formatDate(String dateStr, String format,
            String toFormat) {
        return format(parseDate(dateStr, format), toFormat);
    }

    public static String formatDate(Date date) {
        return formatDateByFormat(date, "yyyy-MM-dd");
    }

    public static String getTime(Date date) {
        return format(date, "HH:mm:ss");
    }

    public static String format(Date date, String format) {
        String result = "";
        try {
            if (date != null) {
                DateFormat dateFormat = new SimpleDateFormat(format);
                result = dateFormat.format(date);
            }
        } catch (Exception localException) {
        }
        return result;
    }

    public static String format(Date date) {
        return format(date, "yyyy-MM-dd");
    }

    public static Date addDate(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        long millis = getMillis(date) + day * 24L * 3600L * 1000L;
        calendar.setTimeInMillis(millis);
        return calendar.getTime();
    }

    public static String formatDateByFormat(Date date, String format) {
        String result = "";
        if (date != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                result = sdf.format(date);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static long getMillis(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }
}

Related

  1. getMonthByNow(int month)
  2. getMonthCount(String from, String to)
  3. getMonthDesc(Date month)
  4. getMonthEnd(Date date)
  5. getMonthEnd(String strdate)
  6. getMonthEndDate(Date date)
  7. getMonthFirstDate(final Date date, final String format)
  8. getMonthForDate(String dateToCheck, String pattern)
  9. getMonthForInt(int num)