Get start of a month
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] argv) {
System.out.println(getStartOfMonth(new Date()));
}
public static Date getStartOfMonth(Date day) {
return getStartOfMonth(day, Calendar.getInstance());
}
public static Date getStartOfMonth(Date day, Calendar cal) {
if (day == null) day = new Date();
cal.setTime(day);
// set time to start of day
cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
// set time to first day of month
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Date Get:
- Day of Week
- Number of days in a month
- Month of year
- Current month name
- Full date time
- Get all attributes from a Calendar
- Get year, month, day, minute, hour, second, milli second from java.util.Date through Calendar
- Get age from a birthday date
- Get Monday
- Get next Sunday
- Get today's date
- Get the last day of a month
- Get date of yesterday
- Get Day-of-Week for a Particular Date
- Get date of last week
- Get date of last month
- Get last Date of This Month
- Get Month in a leap year
- Get start of a month
- Get the end of a month
- Get noon of a day
- Get days since a date
- Get TimeZone
- Get Last day fo previous Month
- Is an hour between an interval
- Is a Year a Leap Year