Java Month of Year getNextMonthFirstDay(T day)

Here you can find the source of getNextMonthFirstDay(T day)

Description

get Next Month First Day

License

Apache License

Declaration

public static <T extends Date> T getNextMonthFirstDay(T day) 

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 {
    public static <T extends Date> T getNextMonthFirstDay(T day) {
        if (day == null)
            return null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);//from  ww  w. j  av  a  2  s.co m
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month + 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String datefor = format(calendar.getTime(), "yyyy-MM-dd");
        Long mill = parseDate(datefor).getTime();
        T another = (T) day.clone();
        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. getNextiMonth(Date date, int month)
  2. getNextMonth()
  3. getNextMonth(Calendar aCal)
  4. getNextMonth(Date nowdate, int delay)
  5. getNextMonthFirstDay(String date)
  6. getNextMonthFistDay(String nowdate, String inFormat, String outFormat)
  7. getNextYearMonth(String yearMonth)
  8. getNrDaysOfMonth(int month, int year)
  9. getNumberOfDays(int year, int month)