Java Month Day getfirstDayOfMonth()

Here you can find the source of getfirstDayOfMonth()

Description

getfirst Day Of Month

License

Open Source License

Declaration

public static Date getfirstDayOfMonth() 

Method Source Code

//package com.java2s;

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

public class Main {
    public static final int MINUTE = 2;
    public static final String YYYYMMDD = "yyyy-MM-dd";

    public static Date getfirstDayOfMonth() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat(YYYYMMDD);
        //      String sdate = format.format(date);
        Calendar sCal = Calendar.getInstance();
        sCal.setTime(date);//from  w  w w.  jav  a2 s  . c om

        sCal.add(Calendar.DAY_OF_MONTH, -(sCal.get(Calendar.DAY_OF_MONTH) - 1));

        try {
            date = format.parse(format.format(sCal.getTime()));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

    public static Date add(Date date, long minutes) {
        if (date == null) {
            throw new IllegalArgumentException();
        }

        return addMinutes(date, minutes);
    }

    public static java.util.Date addMinutes(java.util.Date date, long... minutes) {
        if (minutes == null) {
            return date;
        }
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        for (long minute : minutes) {
            c.add(Calendar.MINUTE, (int) minute);
        }

        return c.getTime();
    }

    public static Date addMinutes(Date date, long minutes) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + ((long) minutes) * 60 * 1000);
        return c.getTime();
    }

    public static long getMillis(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }
}

Related

  1. getFirstDateOfMonth(Date theDate)
  2. getFirstday_Month(Date date, int months)
  3. getFirstDayOfCurMonth()
  4. getFirstDayOfCurrentMonth()
  5. getFirstDayOfLastMonth()
  6. getFirstDayOfMonth(Calendar c)
  7. getFirstDayOfMonth(Date date)
  8. getFirstDayOfMonth(Date date)
  9. getFirstDayOfMonth(Date date)