public static String toString(Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
return simpleDateFormat.format(date);
}
public static Date toDate(String date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return simpleDateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String sdf(Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(date);
}
public static String getMonthBeginTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);
return sdf.format(c.getTime());
}
public static String getMonthEndTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
return sdf.format(c.getTime());
}
public static String getMonthBeginTimeOfLastYear() {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
c.add(Calendar.MONTH, 0);
c.add(Calendar.YEAR, -1);
c.set(Calendar.DAY_OF_MONTH, 1);
return sdf.format(c.getTime());
}
public static String getMonthEndTimeOfLastYear() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.add(Calendar.YEAR, -1);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
return sdf.format(c.getTime());
}
public static String getToday() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}
public static String getYesterday() {
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -10);
SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd");
return startSdf.format(c.getTime());
}
public static String getYesterdayBeginTime() {
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
return startSdf.format(c.getTime());
}
public static String getYesterdayEndTime() {
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
return startSdf.format(c.getTime());
}
public static String getTodayBeginTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
return sdf.format(new Date());
}
public static String getTodayEndTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
return sdf.format(new Date());
}
public static String getNowDateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
public static String getTomorrowBeginTime() {
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
return startSdf.format(c.getTime());
}
public static String getTomorrowEndTime() {
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
return startSdf.format(c.getTime());
}
public static String getTomorrowTime(String time) {
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
SimpleDateFormat startSdf = new SimpleDateFormat(String.format("yyyy-MM-dd %s", time));
return startSdf.format(c.getTime());
}
public static String getYearBeginTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-01-01 00:00:00");
return sdf.format(c.getTime());
}
public static String getYearEndTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-12-31 23:59:59");
Calendar c = Calendar.getInstance();
return sdf.format(c.getTime());
}
public static String getYearBeginTimeOfLastYear() {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-01-01 00:00:00");
c.add(Calendar.YEAR, -1);
return sdf.format(c.getTime());
}
public static String getYearEndTimeOfLastYear() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-12-31 23:59:59");
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -1);
return sdf.format(c.getTime());
}
public static Map<String, Integer> getWeekOfMonth(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
int resultYear;
int resultMonth;
int resultWeekOfMonth;
Map<String, Integer> resultMap = new HashMap<>();
try {
Date parse = sdf.parse(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(parse);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
resultYear = year;
resultMonth = month + 1;
Calendar monthCalendar = Calendar.getInstance();
monthCalendar.set(Calendar.YEAR, year);
monthCalendar.set(Calendar.MONTH, month);
monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
int monthDayOfWeek = monthCalendar.get(Calendar.DAY_OF_WEEK);
if (monthDayOfWeek == Calendar.SUNDAY) {
if (dayOfWeek == Calendar.SUNDAY) {
if (dayOfMonth == 1) {
if (month == 0) {
resultWeekOfMonth = getLastWeekOfMonth(year - 1, 11);
resultYear = year - 1;
resultMonth = 12;
} else {
resultWeekOfMonth = getLastWeekOfMonth(year, month - 1);
resultMonth = month;
}
} else {
resultWeekOfMonth = weekOfMonth - 1;
}
} else {
resultWeekOfMonth = weekOfMonth;
}
} else if (monthDayOfWeek == Calendar.MONDAY) {
if (dayOfWeek == Calendar.SUNDAY) {
resultWeekOfMonth = weekOfMonth - 1;
} else {
resultWeekOfMonth = weekOfMonth;
}
} else {
if (dayOfWeek == Calendar.SUNDAY) {
resultWeekOfMonth = weekOfMonth - 2;
} else {
resultWeekOfMonth = weekOfMonth - 1;
}
if (resultWeekOfMonth == 0) {
if (month == 0) {
resultWeekOfMonth = getLastWeekOfMonth(year - 1, 11);
resultYear = year - 1;
resultMonth = 12;
} else {
resultWeekOfMonth = getLastWeekOfMonth(year, month - 1);
resultMonth = month;
}
}
}
resultMap.put("year", resultYear);
resultMap.put("month", resultMonth);
resultMap.put("weekOfMonth", resultWeekOfMonth);
} catch (ParseException e) {
e.printStackTrace();
}
return resultMap;
}
private static int getLastWeekOfMonth(int year, int month) {
int result;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, 1);
Calendar lastDayOfMonth = Calendar.getInstance();
lastDayOfMonth.set(Calendar.YEAR, year);
lastDayOfMonth.set(Calendar.MONTH, month);
lastDayOfMonth.set(Calendar.DAY_OF_MONTH, 1);
lastDayOfMonth.roll(Calendar.DAY_OF_MONTH, -1);
int lastWeekOfMonth = lastDayOfMonth.get(Calendar.WEEK_OF_MONTH);
int lastDayOfWeek = lastDayOfMonth.get(Calendar.DAY_OF_WEEK);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.MONDAY) {
if (lastDayOfWeek == Calendar.SUNDAY) {
result = lastWeekOfMonth - 1;
} else {
result = lastWeekOfMonth;
}
} else {
if (lastDayOfWeek == Calendar.SUNDAY) {
result = lastWeekOfMonth - 2;
} else {
result = lastWeekOfMonth - 1;
}
}
return result;
}
public static String getWeekBeginTime() {
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
return sdf.format(cal.getTime());
}
public static String getWeekEndTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
cal.setTime(cal.getTime());
cal.add(Calendar.DAY_OF_WEEK, 6);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
return sdf.format(cal.getTime());
}