一、获取当前的年月日时分秒可以使用Calendar日历类
public static void main(String
[] args
) {
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal
= Calendar
.getInstance();
Date date
= cal
.getTime();
String nowTime
= sdf
.format(cal
.getTime());
System
.out
.println(nowTime
);
Calendar calendar
= Calendar
.getInstance();
int year
= calendar
.get(Calendar
.YEAR
);
int month
= calendar
.get(Calendar
.MONTH
);
int hour2
= calendar
.get(Calendar
.HOUR_OF_DAY
);
int week
= calendar
.get(Calendar
.DAY_OF_WEEK
);
System
.out
.println(month
+" "+ hour2
+" " + week
);
int day
= calendar
.get(Calendar
.DAY_OF_MONTH
);
int hour
= calendar
.get(Calendar
.DAY_OF_YEAR
);
int minute
= calendar
.get(Calendar
.MINUTE
);
int second
= calendar
.get(Calendar
.SECOND
);
int millisecond
= calendar
.get(Calendar
.MILLISECOND
);
System
.out
.println(year
+ " " + month
+ " " + day
+ " "+ hour
+" "+minute
+" "+ second
+ " "+millisecond
);
}
二、常用的日期操作
public static Date
getNextDate(int next
){
Calendar calendar
= Calendar
.getInstance();
calendar
.add(Calendar
.DAY_OF_YEAR
, next
);
return calendar
.getTime();
}
public static int days(Date beginTime
, Date endTime
){
SimpleDateFormat simpleDateFormat
= new SimpleDateFormat("DDD");
Integer beginDays
= Integer
.parseInt(simpleDateFormat
.format(beginTime
));
Integer endDays
= Integer
.parseInt(simpleDateFormat
.format(endTime
));
return endDays
- beginDays
;
}
public static String
date2Str(Date date
, String pattern
){
SimpleDateFormat simpleDateFormat
= new SimpleDateFormat(pattern
);
return simpleDateFormat
.format(date
);
}
转载请注明原文地址:https://tech.qufami.com/read-20110.html