1.获得当前日期
public static String getTodayDate() {
Calendar today = Calendar.getInstance();
String format= "yyyy-MM-dd";
SimpleDateFormat sdf= new SimpleDateFormat(format);
return sdf.format(today.getTime());
}
2.获得当前时间
public static String getTodayTime() {
Calendar today = Calendar.getInstance();
String format= "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf= new SimpleDateFormat(format);
return sdf.format(today.getTime());
}
3.获得上周是*年的第?周
public static int getPreWeek() {
Calendar thisWeek = Calendar.getInstance();
thisWeek.add(Calendar.WEEK_OF_YEAR, -1);
int lastWeek = thisWeek.get(Calendar.WEEK_OF_YEAR);
return lastWeek;
}
4.给时间增加秒数
public static Date addSecond(Date date, int second) {
if (date != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setTimeZone(TimeZone.getDefault());
long Time = (cal.getTimeInMillis() / 1000) + second;
cal.setTimeInMillis(Time * 1000);
return cal.getTime();
} else {
return null;
}
}
5.给时间减去秒数
public static Date subtractSecond(Date date, int second) {
if (date != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setTimeZone(TimeZone.getDefault());
long Time = (cal.getTimeInMillis() / 1000) - second;
cal.setTimeInMillis(Time * 1000);
return cal.getTime();
} else {
return null;
}
}
6. 根据时间获取(utcMs)时间毫秒##
public static Long getUtcMsTime(Date date) {
if (date != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.setTimeZone(TimeZone.getDefault());
return cal.getTimeInMillis();
} else {
return null;
}
}
7.获得上周开始时间(周一)
public static String getPreWeekStartDay() {
Calendar cal = Calendar.getInstance();
String format = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.add(Calendar.WEEK_OF_MONTH, -1);
return sdf.format(cal.getTime());
}
8.获得上周结束时间(周日)
public static String getPreWeekEndDay() {
Calendar cal = Calendar.getInstance();
String format = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.add(Calendar.WEEK_OF_MONTH, 0);
return sdf.format(cal.getTime());
}
9.获得上月开始时间
public static String getPreMonthStartDay() {
GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
String format = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, -1);
Date theDate = calendar.getTime();
gcLast.setTime(theDate);
gcLast.set(Calendar.DAY_OF_MONTH, 1);
return sdf.format(gcLast.getTime());
}
10. 获得上月结束时间##
public static String getPreMonthEndDay() {
GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
String format = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
Date theDate = calendar.getTime();
gcLast.setTime(theDate);
gcLast.set(Calendar.DAY_OF_MONTH, 0);
return sdf.format(gcLast.getTime());
}
```
11.获取某年某月第一日
------------
public static String getFirstDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
return new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
}
12.获取某年某月最后一日
-------------
public static String getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
return new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
}