获得系统当前时间
public static String getSysTime(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
获得当前系统日期
public static String getSysDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}
传入当天日期(yyyy-MM-dd),获得当天上一周周一的时间(yyyy-MM-dd 00:00:00)
public static String getLastWeekSunday(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
if (date != null) {
cal.setTime(date);
}
//判断当前日期是否为周末,因为周末是本周第一天,如果不向后推迟一天的到的将是下周一的零点,而不是本周周一零点
if (Calendar.SUNDAY == cal.get(Calendar.DAY_OF_WEEK)) {
cal.add(Calendar.DATE, -1);
}
//Calendar.SUNDAY 周天想获取周几就更换这个
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
//return sdf.format(cal.getTime()) + " 23:59:59";
return sdf.format(cal.getTime());
}
传入当天日期(yyyy-MM-dd),获得当天上一周周天的时间(yyyy-MM-dd 23:59:59)
public static String getLastWeekMonday(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
//判断当前日期是否为周末,因为周末是本周第一天,如果不向后推迟一天的到的将是下周一的零点,而不是本周周一零点
if (1 == cal.get(Calendar.DAY_OF_WEEK)) {
cal.add(Calendar.DATE, -1);
}
//时间减去7天
cal.add(Calendar.DAY_OF_MONTH, -7);
//Calendar.MONDAY 这个是周一的意思 想获取周几就更换这个
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
//return sdf.format(cal.getTime()) + " 00:00:00";
return sdf.format(cal.getTime());
}
传入当天日期(yyyy-MM-dd),获得当天上一个月月初的时间(yyyy-MM-dd 23:59:59)
public static String getLastMonthStartDate(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
String monthFirstDate = "";
try {
date = sdf.parse(time);
Calendar c = Calendar.getInstance();
//设置为指定日期
c.setTime(date);
//指定日期月份减去一
c.add(Calendar.MONTH, -1);
//指定日期月份减去一后的 获取第一天
c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
//获取最终的时间
Date lastDateOfPrevMonth = c.getTime();
//monthFirstDate = sdf.format(lastDateOfPrevMonth)+" 00:00:00";
monthFirstDate = sdf.format(lastDateOfPrevMonth);
} catch (ParseException e) {
e.printStackTrace();
}
return monthFirstDate;
}
传入当天日期(yyyy-MM-dd),获得当天上一个月月底的时间(yyyy-MM-dd 23:59:59)
public static String getLastMonthEndDate(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
String monthFirstDate = "";
try {
date = sdf.parse(time);
Calendar c = Calendar.getInstance();
//设置为指定日期
c.setTime(date);
//指定日期月份减去一
c.add(Calendar.MONTH, -1);
//指定日期月份减去一后的 最大天数
c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
//获取最终的时间
Date lastDateOfPrevMonth = c.getTime();
//monthFirstDate = sdf.format(lastDateOfPrevMonth)+" 23:59:59";
monthFirstDate = sdf.format(lastDateOfPrevMonth);
} catch (ParseException e) {
e.printStackTrace();
}
return monthFirstDate;
}
获得当天是星期几
public static String getWeek() {
Date date=new Date();
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE", Locale.CHINESE);
return dateFm.format(date);
}