import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author lishuaishuai
*
*/
public class date_count {
/**
* 取得每月的第一天
*
* @return
*/
public String getFirstDayOfMonth(String s) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date DateEndTime = null;
try {
DateEndTime = sd.parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cal.setTime(DateEndTime);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_MONTH) + 1);
Date start = cal.getTime();
sd.format(start);
return sd.format(start);
}
/**
*
* 获得这个月的最后一天
*/
public String getLastDayOfMonth(String s) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date DateEndTime = null;
try {
DateEndTime = sd.parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cal.setTime(DateEndTime);
cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_MONTH) + 1);
cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DATE, -1);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
Date end = cal.getTime();
sd.format(end);
return sd.format(end);
}
/**
*
* 获得前三天日期
*/
public String conv(String s) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date DateEndTime = null;
try {
DateEndTime = sd.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
cal.setTime(DateEndTime);
cal.add(5, -3);
java.util.Date d = cal.getTime();
sd.format(d);
return sd.format(d);
}
/**
*
* 下个月日期
*/
public String co(String s) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date DateEndTime = null;
try {
DateEndTime = sd.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
cal.setTime(DateEndTime);
cal.add(Calendar.MONTH, +1);
java.util.Date d = cal.getTime();
sd.format(d);
return sd.format(d);
}
public static void main(String[] args) {
String ny = "2010-03-29";
date_count t = new date_count();
t.getFirstDayOfMonth(ny);
t.co(ny);
t.conv(t.getLastDayOfMonth(ny));
}
}