Java Month Get getMonthEndDate(Date date)

Here you can find the source of getMonthEndDate(Date date)

Description

get Month End Date

License

Apache License

Declaration


public static String getMonthEndDate(Date date) 

Method Source Code


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

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;

import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

public class Main {
    private static final String dateFormat3 = "yyyy-MM-dd";
    private static final Map<String, DateFormat> DFS = new HashMap<String, DateFormat>();

    public static String getMonthEndDate(Date date) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.add(GregorianCalendar.MONTH, 1);
        gc.add(GregorianCalendar.DATE, -date.getDate());
        Date dateTemp = gc.getTime();
        return date2String(dateTemp);
    }/*from  w w w.  ja v a  2s.c  o  m*/

    public static Date getDate(String pagetime, String dateformat) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(dateformat);
        Date date = dateFormat.parse(pagetime);
        return date;
    }

    public static Date getDate(String pagetime, String dateformat, String timezone) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(dateformat);
        dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
        Date date = dateFormat.parse(pagetime);
        return date;
    }

    public static Date getDate(String pagetime, String dateformat, Locale locale) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(dateformat, locale);
        Date date = dateFormat.parse(pagetime);
        return date;
    }

    public static Date getDate(String pagetime, String dateformat, Locale locale, String timezone)
            throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(dateformat, locale);
        if (timezone != null) {
            dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
        }
        Date date = dateFormat.parse(pagetime);
        return date;
    }

    public static String date2String(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat3);
        String dateString = formatter.format(date);
        return dateString;
    }

    public static Date parse(String source, String pattern) {
        if (source == null) {
            return null;
        }
        Date date;
        try {
            date = getFormat(pattern).parse(source);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }

    public static String format(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        return getFormat(pattern).format(date);
    }

    public static DateFormat getFormat(String pattern) {
        DateFormat format = DFS.get(pattern);
        if (format == null) {
            format = new SimpleDateFormat(pattern);
            DFS.put(pattern, format);
        }
        return format;
    }
}

Related

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