Java Month Day getMonthLastDay(String fmt)

Here you can find the source of getMonthLastDay(String fmt)

Description

get Month Last Day

License

Apache License

Declaration

public static String getMonthLastDay(String fmt) 

Method Source Code

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

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

public class Main {
    private static Map<String, SimpleDateFormat> formats = new HashMap();

    public static String getMonthLastDay(String fmt) {
        String returndate = "";
        Date date = null;//from  w w  w .  jav  a 2 s.c  o m
        Calendar cl = Calendar.getInstance();
        switch (cl.get(2)) {
        case 0:
            cl.set(5, 31);
            break;
        case 1:
            int year = cl.get(1);

            if (isLeapYear(year))
                cl.set(5, 29);
            else {
                cl.set(5, 28);
            }
            break;
        case 2:
            cl.set(5, 31);
            break;
        case 3:
            cl.set(5, 30);
            break;
        case 4:
            cl.set(5, 31);
            break;
        case 5:
            cl.set(5, 30);
            break;
        case 6:
            cl.set(5, 31);
            break;
        case 7:
            cl.set(5, 31);
            break;
        case 8:
            cl.set(5, 30);
            break;
        case 9:
            cl.set(5, 31);
            break;
        case 10:
            cl.set(5, 30);
            break;
        case 11:
            cl.set(5, 31);
        }

        date = cl.getTime();

        returndate = getFormatTimeString(date, fmt);

        return returndate;
    }

    public static boolean isLeapYear(int year) {
        if (year % 400 == 0)
            return true;
        if (year % 4 == 0) {
            return (year % 100 != 0);
        }

        return false;
    }

    public static String getFormatTimeString(Date date, String pattern) {
        SimpleDateFormat sDateFormat = getDateFormat(pattern);

        synchronized (sDateFormat) {
            return sDateFormat.format(date);
        }
    }

    public static SimpleDateFormat getDateFormat(String pattern) {
        SimpleDateFormat sDateFormat = (SimpleDateFormat) formats.get(pattern);
        if (sDateFormat == null) {
            sDateFormat = new SimpleDateFormat(pattern);
            formats.put(pattern, sDateFormat);
        }
        return sDateFormat;
    }
}

Related

  1. getMonthLastDay()
  2. getMonthLastDay()
  3. getMonthLastDay()
  4. getMonthLastDay(String curmonth, int month)
  5. getMonthLastDay(String dateString)
  6. getMonthMaxDay(String sDate)
  7. getMonthWeek(String curday)
  8. getMonthWeek(String curday)
  9. getPreviosMonthFirstDay()