-
package info.lumanman.h5.util;
-
-
import java.text.SimpleDateFormat;
-
import java.util.Calendar;
-
-
-
-
public static void main(String[] args) {
-
SimpleDateFormat sdf=
new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
-
-
-
-
-
-
-
-
-
-
public static void addDay(SimpleDateFormat sdf,int days){
-
-
-
Calendar calendar=Calendar.getInstance();
-
-
System.
out.println("当前时间: "+sdf.format(calendar.getTime()));
-
-
-
Calendar calendar1=Calendar.getInstance();
-
calendar1.
add(Calendar.DAY_OF_YEAR, days);
-
System.
out.println("加了"+days+"天之后的时间: "+sdf.format(calendar1.getTime()));
-
-
Calendar calendar2=Calendar.getInstance();
-
calendar2.
add(Calendar.DAY_OF_MONTH, days);
-
System.
out.println("加了"+days+"之后的时间: "+sdf.format(calendar2.getTime()));
-
-
Calendar calendar3=Calendar.getInstance();
-
calendar3.
add(Calendar.DAY_OF_WEEK, days);
-
System.
out.println("加了"+days+"之后的时间: "+sdf.format(calendar3.getTime()));
-
-
-
-
-
-
public static void getWeekDay(SimpleDateFormat sdf){
-
-
Calendar calendar=Calendar.getInstance();
-
-
System.
out.println("当前时间: "+sdf.format(calendar.getTime()));
-
-
-
Calendar calendar1=Calendar.getInstance();
-
calendar1.
set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
-
System.
out.println("本周一: "+sdf.format(calendar1.getTime()));
-
-
Calendar calendar2=Calendar.getInstance();
-
-
calendar2.
add(Calendar.DAY_OF_WEEK, 7);
-
calendar2.
set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
-
System.
out.println("本周日: "+sdf.format(calendar2.getTime()));
-
-
-
Calendar calendar3=Calendar.getInstance();
-
calendar3.
add(Calendar.DAY_OF_MONTH, 7);
-
calendar3.
set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
-
System.
out.println("下周一: "+sdf.format(calendar3.getTime()));
-
-
Calendar calendar4=Calendar.getInstance();
-
calendar4.
add(Calendar.DAY_OF_MONTH, 14);
-
calendar4.
set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
-
System.
out.println("下周日: "+sdf.format(calendar4.getTime()));
-
-
-
Calendar calendar5=Calendar.getInstance();
-
calendar5.
add(Calendar.DAY_OF_YEAR, -7);
-
calendar5.
set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
-
System.
out.println("上周一: "+sdf.format(calendar5.getTime()));
-
-
Calendar calendar6=Calendar.getInstance();
-
calendar6.
set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
-
System.
out.println("上周日: "+sdf.format(calendar6.getTime()));
-
-
-
-
-
-
public static void getMonth(SimpleDateFormat sdf){
-
-
int maxCurrentMonthDay=0;
-
Calendar calendar=Calendar.getInstance();
-
System.
out.println("当前时间: "+sdf.format(calendar.getTime()));
-
-
-
Calendar calendar1=Calendar.getInstance();
-
calendar1.
set(Calendar.DAY_OF_MONTH, 1);
-
System.
out.println("本月第一天: "+sdf.format(calendar1.getTime()));
-
-
Calendar calendar2=Calendar.getInstance();
-
maxCurrentMonthDay=calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar2.
set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
-
System.
out.println("本月最后一天: "+sdf.format(calendar2.getTime()));
-
-
-
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);
-
System.
out.println("下月第一天: "+sdf.format(calendar3.getTime()));
-
-
Calendar calendar4=Calendar.getInstance();
-
maxCurrentMonthDay=calendar4.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar4.
add(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
-
-
maxCurrentMonthDay=calendar4.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar4.
set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
-
System.
out.println("下月第一天: "+sdf.format(calendar4.getTime()));
-
-
-
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);
-
System.
out.println("上月第一天: "+sdf.format(calendar5.getTime()));
-
-
Calendar calendar6=Calendar.getInstance();
-
maxCurrentMonthDay=calendar6.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar6.
add(Calendar.DAY_OF_MONTH, -maxCurrentMonthDay);
-
-
maxCurrentMonthDay=calendar6.getActualMaximum(Calendar.DAY_OF_MONTH);
-
calendar6.
set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
-
System.
out.println("上月第一天: "+sdf.format(calendar6.getTime()));
-
-
-
}
转载于:https://www.cnblogs.com/mayhh/p/11354818.html