Java Day getLastDay(String nowdate, String inFormat, String outFormat)

Here you can find the source of getLastDay(String nowdate, String inFormat, String outFormat)

Description

get Last Day

License

Apache License

Declaration

public static String getLastDay(String nowdate, String inFormat, String outFormat) throws ParseException 

Method Source Code

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

import java.text.ParseException;
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 getLastDay(String nowdate, String inFormat, String outFormat) throws ParseException {
        String returndate = "";

        Date date = getDateFromString(nowdate, inFormat);

        Calendar cl = Calendar.getInstance();
        cl.setTime(date);//w w  w. j  a v a  2 s  .com
        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, outFormat);

        return returndate;
    }

    public static Date getDateFromString(String date, String pattern) throws ParseException {
        SimpleDateFormat sDateFormat = getDateFormat(pattern);

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

    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. getDayTime_End()
  2. getLastDay()
  3. getLastDay(Date date)
  4. getLastDay(Date date)
  5. getLastDay(Date date)
  6. getLastDayOfQuarter(Date date)
  7. getLastNDays(int aInN)
  8. getLastTimeInDay(Date day)