Java Month Day getFirstDayOfMonth(Date date)

Here you can find the source of getFirstDayOfMonth(Date date)

Description

get First Day Of Month

License

Open Source License

Parameter

Parameter Description
date a parameter

Return

Date

Declaration

public static Date getFirstDayOfMonth(Date date) 

Method Source Code


//package com.java2s;

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

public class Main {
    public static final String format0 = "yyyy-MM-dd HH:mm:ss";
    public static final String format2 = "yyyy-MM";

    public static Date getFirstDayOfMonth(Date date) {
        if (null == date)
            return null;
        return parserDate(getFirstDayStringOfMonth(date), format2);
    }/*from w  w w.j av  a 2  s .com*/

    public static Date parserDate(String str, String format) {
        if (null == str || "".equals(str) || "NULL".equalsIgnoreCase(str))
            return null;
        if (null == format)
            format = format0;

        java.util.Date d = Calendar.getInstance().getTime();
        if (str.length() == 10)
            str = str + " 00:00:00";
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(str, pos);
        return d;
    }

    public static Date parserDate(String str) {
        if (null == str)
            return null;
        return parserDate(str, format0);
    }

    public static String getFirstDayStringOfMonth(Date date) {
        if (null == date)
            return null;
        return getFormateDateString(date, format2);
    }

    public static String getFormateDateString(Date date, String format) {
        if (date == null)
            return null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }
}

Related

  1. getFirstDayOfMonth(Calendar c)
  2. getFirstDayOfMonth(Date date)
  3. getFirstDayOfMonth(Date date)
  4. getFirstDayOfMonth(Date date)
  5. getFirstDayOfMonth(Date date)
  6. getFirstDayOfMonth(Long date)
  7. getFirstDayOfNextMonth()
  8. getFirstDayStringOfMonth(Date date)
  9. getFirstDayStrOfMonth(Date date)