Java Month Day lastdayofmonth()

Here you can find the source of lastdayofmonth()

Description

lastdayofmonth

License

Open Source License

Declaration

public static String lastdayofmonth() 

Method Source Code


//package com.java2s;
/*/*  w  ww  .  j  a v  a  2s. c  o m*/
 * Copyright 2010 Mttang.com All right reserved. This software is the
 * confidential and proprietary information of Mttang.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Mttang.com.
 */

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

public class Main {
    static final SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyy-MM");
    static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");

    public static String lastdayofmonth() {
        Calendar calendar = Calendar.getInstance();
        Calendar cpcalendar = (Calendar) calendar.clone();
        cpcalendar.set(Calendar.DAY_OF_MONTH, 1);
        cpcalendar.add(Calendar.MONTH, 1);
        cpcalendar.add(Calendar.DATE, -1);
        String date = yyyyMMdd.format(new Date(cpcalendar.getTimeInMillis()));
        return date;
    }

    public static String lastdayofmonth(String date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(formatYearMonth(date));
        Calendar cpcalendar = (Calendar) calendar.clone();
        cpcalendar.set(Calendar.DAY_OF_MONTH, 1);
        cpcalendar.add(Calendar.MONTH, 1);
        cpcalendar.add(Calendar.DATE, -1);
        return yyyyMMdd.format(new Date(cpcalendar.getTimeInMillis()));
    }

    public static Date formatYearMonth(String day) {
        try {
            return yyyyMM.parse(day);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return new java.util.Date();
        }
    }

    public static String formatYearMonth(Date date) {
        try {
            return yyyyMM.format(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

Related

  1. getStringOfFirstDayInMonth()
  2. isFirstDayOfMonth()
  3. isLastDayOfMonth()
  4. isLastDayOfMonth(Calendar cal)
  5. lastDayOfLastMonth()
  6. lastDayOfMonth(String batdate)
  7. lastDayOfMonth(String src)
  8. previousMonthFirstDay()