/** * 得到当前月的上一个月,返回2016-06这样的字符串 */
public static String getLastMonth() {
Calendarcal=Calendar.getInstance();
Calendar cal = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
String date = format.format(cal.getTime());
return date;
}
/** * 得到当前月的第一天,返回2016-06-01这样的字符串 * *
@return */
public static String getCurrentMonthFirstDay() {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String first = format.format(c.getTime());
return first;
}
public static String getCurrentMonthTheLastDay() {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
return dateToStr(c.getTime(), "yyyy-MM-dd");
}
/** * 获取当前年的第一个月,返回2016-01这样的数据 * * @return */
public static String getCurrentYearFirstMonth() {
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, 0);
c.set(Calendar.DAY_OF_YEAR, 1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
String first = format.format(c.getTime());
return first; }
/** * 获取当前年已过的所有季度的集合(季度包含的月份未完则也算入)*/
public static ListgetAllQuarterOfThisYear() {
Listquarters = new ArrayList();
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
if (month > 9) {
quarters.add(year + "-" + 1);
quarters.add(year + "-" + 2);
quarters.add(year + "-" + 3);
quarters.add(year + "-" + 4);
return quarters;
} if (month > 6 && month <= 9) {
quarters.add(year + "-" + 1);
quarters.add(year + "-" + 2);
quarters.add(year + "-" + 3);
return quarters;
} if (month > 3 && month <= 6) {
quarters.add(year + "-" + 1);
quarters.add(year + "-" + 2);
return quarters;
} if (month >= 1 && month <= 3) {
quarters.add(year + "-" + 1);
return quarters;
}
return null;
}
/** * 获取昨天 * * @return */
public static String getYesterDay() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
return yesterday;
}
/** * 获取指定时间范围内的所有日期 * * @param startDate * (format="2016-06-01") * @param endDate * (format="2016-07-12") * @return * @throws ParseException */
public static ListgetDaysBetween(String startDate, String endDate) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Listresult = new ArrayList();
result.add(startDate);
try {
Calendar calBegin = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 Date begin = format.parse(startDate);
calBegin.setTime(begin);
Calendar calEnd = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 Date end = format.parse(endDate); calEnd.setTime(end); // 测试此日期是否在指定日期之后 while (end.after(calBegin.getTime())) { // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 calBegin.add(Calendar.DAY_OF_MONTH, 1);
result.add(format.format(calBegin.getTime()));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/* * 获取指定时间范围内的所有月份 * * @param startDate(format="2016-06") * @param endDate(format="2016-08") * @return * @throws ParseException */
public static ListgetMonthsBetween(String startDate, String endDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Listresult = new ArrayList();
try { Calendar calBegin = Calendar.getInstance();
Calendar calEnd = Calendar.getInstance();
calBegin.setTime(sdf.parse(startDate));
calBegin.set(calBegin.get(Calendar.YEAR), calBegin.get(Calendar.MONTH), 1); calEnd.setTime(sdf.parse(endDate));
calEnd.set(calEnd.get(Calendar.YEAR), calEnd.get(Calendar.MONTH), 2);
Calendar curr = calBegin;
while (curr.before(calEnd)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
} catch (Exception e) {
e.printStackTrace();
}
return result; }
/** * 获取指定日期范围的所有周的周一~周日,格式为周一~周日,如2016-09-19为周一,2016-09-25为周天,返回结果为[2016-09-19 * ~2016-09-25] * * @param startDate * 起始时间(2016-09-19) * @param endDate * 结束时间(2016-09-25) * @return * @throws ParseException */
public static ListgetWeeksBetween(String startDate, String endDate) {
// startDate不能大于endDate
Listresult = new ArrayList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
try {
Calendar calBegin = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 Date begin = sdf.parse(startDate); calBegin.setTime(begin);
int dayWeek = calBegin.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
if (1 == dayWeek) { calBegin.add(Calendar.DAY_OF_MONTH, -1); } calBegin.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
Calendar calEnd = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 Date end = sdf.parse(endDate);
calEnd.setTime(end); // 测试此日期是否在指定日期之后
while (end.after(calBegin.getTime())) { // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 calBegin.add(Calendar.DAY_OF_MONTH, 1);
int day = calBegin.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天 calBegin.add(Calendar.DATE, calBegin.getFirstDayOfWeek() - day);// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
// 获取周一
String monday = sdf.format(calBegin.getTime());
calBegin.add(Calendar.DATE, 6);
// 获取周日
String sunday = sdf.format(calBegin.getTime()); result.add(monday + "~" + sunday); }
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/** * 获取当前日期的前index天(以当前日期开始计算) index = -6表示当天日期的前7天 index = -29表示当前日期的前30天 * * @return */
public static String getLastIndexDayStr(int index) {
Date now = new Date();
Calendar c = Calendar.getInstance();
c.setTime(now);
c.add(Calendar.DAY_OF_MONTH, index);
return dateToStr(c.getTime(), "yyyy-MM-dd");
}
/** * 获取当天 */
public static String getTodayStr() {
Date now = new Date();
return dateToStr(now, "yyyy-MM-dd"); }
/** * 获取最近3个月开始日期(包含当前月所有月) * * @return */
public static String getLast3MonthsStartDateStr() {
Date now = new Date(); Calendar c = Calendar.getInstance(); c.setTime(now); c.add(Calendar.MONTH, -2);
return dateToStr(c.getTime(), "yyyy-MM") + "-01";
}
/** * 获取星期几 * @param dateStr * @return */
public static String getWeekNameOfDate(String dateStr) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(format.parse(dateStr));
} catch (ParseException e) {
e.printStackTrace(); }
int dayForWeek = 0;
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
dayForWeek = 7;
} else {
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
String result = "";
switch (dayForWeek) {
case 1: result = "星期一"; break;
case 2: result = "星期二"; break;
case 3: result = "星期三"; break;
case 4: result = "星期四"; break;
case 5: result = "星期五"; break;
case 6: result = "星期六"; break;
case 7: result = "星期天"; break; }
return result; }
/** * 获取指定日期所在月的第一天 * * @param date * @return yyyy-MM-dd */
public static String getMonthStartDate(String date) {
try {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); calendar.setTime(format.parse(date));
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return format.format(calendar.getTime()); }
catch (ParseException e) {
e.printStackTrace(); }
return null; }
/** * 获取指定日期所在月的最后一天 * * @param date * @return yyyy--MM-dd */
public static String getMonthEndDate(String date) {
try {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); calendar.setTime(format.parse(date));
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return format.format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace(); }
return null; }
/** * 获取指定日期所在周的周一 * @param date * @return yyyy-MM-dd */
public static String getMondayOfDate(String date) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 设置时间格式
Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(date));
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1); }
cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
return sdf.format(cal.getTime());
} catch (ParseException e) {
e.printStackTrace(); }
return null; }
/** * 获取指定日期所在周的周日 * @param date * @return yyyy-MM-dd */
public static String getSundayOfDate(String date) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式 Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(date));
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, 6);
return sdf.format(cal.getTime());
} catch (ParseException e) {
e.printStackTrace(); }
return null; }
[java] view plain copy** * 获取月末到当前时间的秒数 */
public static long getSecondsBetweenEndMothToNow() {
Calendar c = Calendar.getInstance();
c = Calendar.getInstance();
c.add(Calendar.MONTH, 1);
c.set(Calendar.DAY_OF_MONTH, 0);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String lastday = format.format(c.getTime());
lastday = lastday + " 23:59:59";
Date last = null; long sec = 0;
try { last = format.parse(lastday);
long diff = last.getTime() - System.currentTimeMillis();
sec = diff / 1000;
} catch (ParseException e) {
e.printStackTrace();
} return sec; }