Java日期工具方法
public static String getFirstDayOfMonth1(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
int firstDay = cal.getMinimum(Calendar.DATE);
cal.set(Calendar.DAY_OF_MONTH,firstDay);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(cal.getTime());
}
public static String getLastDayOfMonth1(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
int lastDay = cal.getActualMaximum(Calendar.DATE);
cal.set(Calendar.DAY_OF_MONTH, lastDay);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(cal.getTime());
}
public static String getCurrentDayTime()
{
Calendar calendar = Calendar.getInstance();
Date currentTime = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
return formatter.format(currentTime);
}
public static String getBeforeTime(int min)
{
Calendar beforeTime = Calendar.getInstance();
beforeTime.add(Calendar.MINUTE, -min);
Date beforeD = beforeTime.getTime();
String before = new SimpleDateFormat("yyyyMMddHHmmss").format(beforeD);
return before;
}
public static String getPastDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String result = format.format(today);
return result;
}
public static String getFutureDate(int dayNum) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + dayNum);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String result = format.format(today);
return result;
}
public static List<String> findDates(String dBegin, String dEnd) {
DateFormat format = new SimpleDateFormat("yyyyMMdd");
try {
Calendar calBegin = Calendar.getInstance();
calBegin.setTime(format.parse(dBegin));
calBegin.set(Calendar.DAY_OF_YEAR, calBegin.get(Calendar.DAY_OF_YEAR) - 1);
Calendar calEnd = Calendar.getInstance();
calEnd.setTime(format.parse(dEnd));
calEnd.set(Calendar.DAY_OF_YEAR, calEnd.get(Calendar.DAY_OF_YEAR) - 1);
List<String> DateList = new ArrayList<String>();
while (calEnd.getTime().after(calBegin.getTime())) {
calBegin.add(Calendar.DAY_OF_MONTH, 1);
DateList.add(format.format(calBegin.getTime()));
}
return DateList;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}