Here you can find the source of firstDayOfMonth(int year, int month, String dateFormat)
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. |
public static String firstDayOfMonth(int year, int month, String dateFormat)
//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()); } }