Java Month Day getMonthFirstDay(T date)

Here you can find the source of getMonthFirstDay(T date)

Description

get Month First Day

License

Apache License

Declaration

public static <T extends Date> T getMonthFirstDay(T date) 

Method Source Code

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

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {

    public static <T extends Date> T getMonthFirstDay(T date) {
        if (date == null)
            return null;
        String dateStr = format(date, "yyyy-MM") + "-01";
        Long mill = parseDate(dateStr).getTime();
        T another = (T) date.clone();//w  ww  . jav  a 2 s  .c  o m
        another.setTime(mill);
        return another;
    }

    /**
      * @param date
      * @param pattern: Date format pattern
      * @return
      */
    public static final <T extends Date> String format(T date, String pattern) {
        if (date == null)
            return null;
        try {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            String result = df.format(date);
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    public static final Date parseDate(String strDate) {
        Date date = null;
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            date = dateFormat.parse(strDate);
            return date;
        } catch (Exception pe) {
            return null;
        }
    }

    /**
      * @param strDate
      * @param pattern
      * @return
      */
    public static final Date parseDate(String strDate, String pattern) {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(pattern);
        try {
            date = df.parse(strDate);
            return date;
        } catch (Exception pe) {
            return null;
        }
    }
}

Related

  1. getMonthEndDay(String yyyy, String mm)
  2. getMonthFirstDay()
  3. getMonthFirstDay()
  4. getMonthFirstDay(Date date)
  5. getMonthFirstDay(String date)
  6. getMonthFristWeekSunday(String month)
  7. getMonthLastDay()
  8. getMonthLastDay()
  9. getMonthLastDay()