Java Month Day firstMonthDay(String dateTime, int num)

Here you can find the source of firstMonthDay(String dateTime, int num)

Description

first Month Day

License

Open Source License

Declaration

public static String firstMonthDay(String dateTime, int num) 

Method Source Code

//package com.java2s;
/*//  w  w w  . j  a va2 s . c o  m
 *  DateUtils.java
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 *  Author: Winter Lau (javayou@gmail.com)
 *  http://dlog4j.sourceforge.net
 */

import java.text.ParseException;

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static java.text.SimpleDateFormat dd = new java.text.SimpleDateFormat("yyyy-MM-dd");

    public static String firstMonthDay(String dateTime, int num) {
        try {
            Date date = dd.parse(dateTime);
            Calendar now = Calendar.getInstance();
            now.setTime(date);
            int year = now.get(Calendar.YEAR);
            int month = now.get(Calendar.MONTH) - num;
            int day = now.get(Calendar.DAY_OF_MONTH);
            now.set(year, month, day);
            now.set(now.DATE, 1);
            return dd.format(now.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dd.format(new Date());
    }

    public static Date firstMonthDay(Date dateTime, int num) {
        Calendar now = Calendar.getInstance();
        now.setTime(dateTime);
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) - num;
        int day = now.get(Calendar.DAY_OF_MONTH);
        now.set(year, month, day);
        now.set(now.DATE, 1);
        return now.getTime();
    }

    public static Date parse(String format, String d) {
        try {
            return new java.text.SimpleDateFormat(format).parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String format(String format, Date d) {
        if (d == null) {
            return "";
        }
        return new java.text.SimpleDateFormat(format).format(d);
    }
}

Related

  1. addMonthsAndDays(Date date, int months, int days)
  2. addMonthToDayOfMonth(Calendar c, int dayOfMonth, int monthInterval)
  3. daysInMonth(GregorianCalendar c)
  4. firstDayOfMonth(Date date)
  5. firstDayOfMonth(Date date)
  6. getActualMaximumDayOfMonth(final Date date)
  7. getCurMonthFirstDay()
  8. getCurrentDayOfMonth()
  9. getCurretnMonthFirstDay()