get Last Date Of Month by different format - Android java.util

Android examples for java.util:Month

Description

get Last Date Of Month by different format

Demo Code


//package com.java2s;

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

public class Main {
    public static String fm = "yyyy/MM/dd/";
    public static String fm2 = "MM/dd/";
    public static String fm3 = "yyyy/MM/";
    public static String fm4 = "dd";

    public static String getLastDateOfMonth(int i) {
        SimpleDateFormat sdf = null;
        if (i == 1) {
            sdf = new SimpleDateFormat(fm);
        } else if (i == 2) {
            sdf = new SimpleDateFormat(fm2);
        } else if (i == 3) {
            sdf = new SimpleDateFormat(fm3);
        } else if (i == 4) {
            sdf = new SimpleDateFormat(fm4);
        }//w ww.  ja  v  a  2 s  . co m
        Date dt = new Date();

        if (dt == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, days);
        String result = sdf.format(cal.getTime());
        return result;
    }

    public static String getLastDateOfMonth(int year, int month, int i) {
        SimpleDateFormat sdf = null;
        if (i == 1) {
            sdf = new SimpleDateFormat(fm);
        } else if (i == 2) {
            sdf = new SimpleDateFormat(fm2);
        } else if (i == 3) {
            sdf = new SimpleDateFormat(fm3);
        } else if (i == 4) {
            sdf = new SimpleDateFormat(fm4);
        }
        Date dt = new Date();

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.setTime(dt);
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, days);
        String result = sdf.format(cal.getTime());
        return result;
    }
}

Related Tutorials