java date of acquisition, first of every month, every Monday
-
package info.lumanman.h5.util;
-
-
import java.text.SimpleDateFormat;
-
import java.util.Calendar;
-
-
public class DateUtil {
-
-
public static void main(String[] args) {
-
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
-
//addDay(sdf,10);
-
// getWeekDay (sdf);
-
getMonth(sdf);
-
-
}
-
/**
-
* Increase the number of days
-
* @param sdf
-
*/
-
public static void addDay(SimpleDateFormat sdf,int days){
-
-
// Get Calendar example, java package performance, private constructor Calendar class object is created by the static method
-
Calendar calendar=Calendar.getInstance();
-
// get the current time and format
-
. The System Out.println ( "Current Time:" + sdf.format (calendar.getTime () ));
-
-
// plus the number of days to the current time and outputs days, where the first parameter can be the following three
-
Calendar calendar1=Calendar.getInstance();
-
calendar1. add(Calendar.DAY_OF_YEAR, days);
-
. The System Out.println ( "plus" + Days + "time after day:" + sdf.format (calendar1.getTime () ));
-
-
Calendar calendar2=Calendar.getInstance();
-
calendar2. add(Calendar.DAY_OF_MONTH, days);
-
. The System Out.println ( "plus" + Days + "time after:" + sdf.format (calendar2.getTime () ));
-
-
Calendar calendar3=Calendar.getInstance();
-
calendar3. add(Calendar.DAY_OF_WEEK, days);
-
. The System Out.println ( "plus" + Days + "time after:" + sdf.format (calendar3.getTime () ));
-
}
-
/**
-
* Get the current week, a week ago, after the first day and the last day of the week
-
* @param sdf
-
*/
-
public static void getWeekDay(SimpleDateFormat sdf){
-
// Get Calendar example, java package performance, private constructor Calendar class object is created by the static method
-
Calendar calendar=Calendar.getInstance();
-
// get the current time and format
-
. The System Out.println ( "Current Time:" + sdf.format (calendar.getTime () ));
-
-
// Monday
-
Calendar calendar1=Calendar.getInstance();
-
calendar1. set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
-
System. Out.println ( "Monday:" + sdf.format (calendar1.getTime () ));
-
// Sunday
-
Calendar calendar2=Calendar.getInstance();
-
// foreign Sunday and we are not one week (Sunday through Saturday are foreign to one week)
-
calendar2. add(Calendar.DAY_OF_WEEK, 7);
-
calendar2. set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
-
. System out.println ( "Sunday:" + sdf.format (calendar2.getTime () ));
-
-
//Next Mon
-
Calendar calendar3=Calendar.getInstance();
-
calendar3. add(Calendar.DAY_OF_MONTH, 7);
-
calendar3. set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
-
. The System Out.println ( "Monday:" + sdf.format (calendar3.getTime () ));
-
// next Sunday
-
Calendar calendar4=Calendar.getInstance();
-
calendar4. add(Calendar.DAY_OF_MONTH, 14);
-
calendar4. set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
-
. System out.println ( "next Sunday:" + sdf.format (calendar4.getTime () ));
-
-
//last Monday
-
Calendar calendar5=Calendar.getInstance();
-
calendar5. add(Calendar.DAY_OF_YEAR, -7);
-
calendar5. set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
-
. System out.println ( "last Monday:" + sdf.format (calendar5.getTime () ));
-
// on Sunday
-
Calendar calendar6=Calendar.getInstance();
-
calendar6. set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
-
. System out.println ( "on Sunday:" + sdf.format (calendar6.getTime () ));
-
}
-
/**
-
* Get the current month, the previous month, the first day after the last day of the month
-
* @param sdf
-
*/
-
public static void getMonth(SimpleDateFormat sdf){
-
// define the total number of days of the current month, such as 30,31,28,29
-
int maxCurrentMonthDay=0;
-
Calendar calendar=Calendar.getInstance();
-
. The System Out.println ( "Current Time:" + sdf.format (calendar.getTime () ));
-
-
// One month
-
Calendar calendar1=Calendar.getInstance();
-
calendar1. set(Calendar.DAY_OF_MONTH, 1);
-
. The System Out.println ( "the first day of the month:" + sdf.format (calendar1.getTime () ));
-
// the last day of the month
-
Calendar calendar2=Calendar.getInstance();
-
maxCurrentMonthDay=calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar2. set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
-
. System out.println ( "the last day of this month:" + sdf.format (calendar2.getTime () ));
-
-
// One month
-
Calendar calendar3=Calendar.getInstance();
-
maxCurrentMonthDay=calendar3.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar3. add(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
-
calendar3. set(Calendar.DAY_OF_MONTH, 1);
-
. The System Out.println ( "the first day of the next month:" + sdf.format (calendar3.getTime () ));
-
// the last day of next month
-
Calendar calendar4=Calendar.getInstance();
-
maxCurrentMonthDay=calendar4.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar4. add(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
-
// The first maxCurrentMonthDay get the number of days of the month, the second maxCurrentMonthDay get the number of days of the next month
-
maxCurrentMonthDay=calendar4.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar4. set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
-
. The System Out.println ( "the first day of the next month:" + sdf.format (calendar4.getTime () ));
-
-
// One month
-
Calendar calendar5=Calendar.getInstance();
-
maxCurrentMonthDay=calendar5.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar5. add(Calendar.DAY_OF_MONTH, -maxCurrentMonthDay);
-
calendar5. set(Calendar.DAY_OF_MONTH, 1);
-
. The System Out.println ( "the first day of the month:" + sdf.format (calendar5.getTime () ));
-
// the last day of last month
-
Calendar calendar6=Calendar.getInstance();
-
maxCurrentMonthDay=calendar6.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar6. add(Calendar.DAY_OF_MONTH, -maxCurrentMonthDay);
-
// The first maxCurrentMonthDay get the number of days of the month, the second maxCurrentMonthDay get the number of days last month
-
maxCurrentMonthDay=calendar6.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar6. set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
-
. The System Out.println ( "the first day of the month:" + sdf.format (calendar6.getTime () ));
-
-
}
-
}