Java Day in Month firstDayOfMonth(int year, int month, String dateFormat)

Here you can find the source of firstDayOfMonth(int year, int month, String dateFormat)

Description

get first day of specified month and specified year in specified date format.

License

Apache License

Parameter

Parameter Description
year the year of the date.
month the month of the date.
dateFormat the formatter of date, such as:yyyy-MM-dd HH:mm:ss:SSS.

Declaration

public static String firstDayOfMonth(int year, int month, String dateFormat) 

Method Source Code


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

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

public class Main {
    /**//from  w w  w .jav a 2 s.  c  o  m
     * get first day of specified month and specified year in specified date
     * format.
     * 
     * @param year
     *            the year of the date.
     * @param month
     *            the month of the date.
     * @param dateFormat
     *            the formatter of date, such as:yyyy-MM-dd HH:mm:ss:SSS.
     */
    public static String firstDayOfMonth(int year, int month, String dateFormat) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.YEAR, year);
        cal.add(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(cal.getTime());
    }

    /**
     * get first day of specified month of current year in specified date
     * format.
     * 
     * @param month
     *            the month of the date.
     * @param dateFormat
     *            the formatter of date, such as:yyyy-MM-dd HH:mm:ss:SSS.
     */
    public static String firstDayOfMonth(int month, String dateFormat) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(cal.getTime());
    }

    public static String getTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("HHmmssSSS");
        return sdf.format(new Date());
    }
}

Related

  1. daysInMonth(int year, int month)
  2. daysOfMonth(int year)
  3. daysOfMonth(int year, int month)
  4. firstDayOfLastMonth()
  5. firstDayOfMonth(int commonMonthIndx, int iyear)
  6. firstDayOfNextMonth(String dateFormat)
  7. getCountDayForMonth(int year, int month)
  8. GetDay(int month)
  9. getDayByOfSomeMonth(String curday, int month)