Java Month Day getFirstDayOfMonth(Date date)

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

Description

get First Day Of Month

License

Apache License

Declaration

public static String getFirstDayOfMonth(Date date) 

Method Source Code


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

import java.text.DateFormat;

import java.text.SimpleDateFormat;

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

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static final String dateFormat3 = "yyyy-MM-dd";
    private static final Map<String, DateFormat> DFS = new HashMap<String, DateFormat>();

    public static String getFirstDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);/*from  w ww . j av a 2s  .  co  m*/
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
        return date2String(cal.getTime());
    }

    public static String date2String(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat3);
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String format(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        return getFormat(pattern).format(date);
    }

    public static DateFormat getFormat(String pattern) {
        DateFormat format = DFS.get(pattern);
        if (format == null) {
            format = new SimpleDateFormat(pattern);
            DFS.put(pattern, format);
        }
        return format;
    }
}

Related

  1. getFirstDayOfCurrentMonth()
  2. getFirstDayOfLastMonth()
  3. getfirstDayOfMonth()
  4. getFirstDayOfMonth(Calendar c)
  5. getFirstDayOfMonth(Date date)
  6. getFirstDayOfMonth(Date date)
  7. getFirstDayOfMonth(Date date)
  8. getFirstDayOfMonth(Date date)
  9. getFirstDayOfMonth(Long date)