每个人心里都有一段伤痕,时间才是最好的疗剂。
1.常用工具类import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
public class DateUnits {
public static String getThisYears() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
Date date = new Date();
return sdf.format(date);
}
public static String getNextYears() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.YEAR, 1);
return sdf.format(c.getTime());
}
public static Date strToDate(String strDate, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
date = new Date();
}
return date;
}
public static String getNextMonth(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, 1);
return sdf.format(c.getTime());
}
public static String lastHours(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
Date d = strToDate(date, "yyyy-MM-dd HH");
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.HOUR, -1);
return sdf.format(c.getTime());
}
public static String lastDays(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = strToDate(date, "yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.DAY_OF_MONTH, -1);
return sdf.format(c.getTime());
}
public static String lastMonth(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date d = strToDate(date, "yyyy-MM");
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.MONTH, -1);
return sdf.format(c.getTime());
}
public static String[] dateSplit(String startDate, String endDate) {
String[] ar = {};
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date newstartDate;
try {
newstartDate = simpleDateFormat.parse(startDate);
Date newendDate = simpleDateFormat.parse(endDate);
List dateList = new ArrayList();
Long spi = newendDate.getTime() - newstartDate.getTime();
Long step = spi / (24 * 60 * 60 * 1000);
List newdateList = new ArrayList();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
newdateList.add(newstartDate);
dateList.add(startDate);
for (int i = 1; i <= step; i++) {
dateList.add(formatter.format(new Date(newdateList.get(i - 1).getTime() + (24 * 60 * 60 * 1000))));
newdateList.add(new Date(newdateList.get(i - 1).getTime() + (24 * 60 * 60 * 1000)));
}
String[] strings = new String[dateList.size()];
String[] array = dateList.toArray(strings);
return array;
} catch (ParseException e) {
e.printStackTrace();
return ar;
}
}
public static String getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String year = calendar.get(Calendar.YEAR) + "";
return "".equals(year) ? "" : year;
}
public static int getDaysOfMonth(String month) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(sdf.parse(month));
} catch (ParseException e) {
e.printStackTrace();
}
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
public static int getDaysOfMonth(String month) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(sdf.parse(month));
} catch (ParseException e) {
e.printStackTrace();
}
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}



