package com.printxls.test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateFormatTest {
public static void main(String[] args) {
String datadate = "2013-01-12";
DateFormatTest test = new DateFormatTest();
try {
System.out.println("第一天=" + test.getFirstDay(datadate));
System.out.println("最后一天=" + test.getLastDay(datadate));
} catch (Exception e) {
e.printStackTrace();
}
}
public String getLastDay(String datadate)throws Exception{
Date date = null;
String day_last = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
date = format.parse(datadate);
//创建日历
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1); //加一个月
calendar.set(Calendar.DATE, 1); //设置为该月第一天
calendar.add(Calendar.DATE, -1); //再减一天即为上个月最后一天
day_last = format.format(calendar.getTime());
return day_last;
}
public String getFirstDay(String datadate)throws Exception{
Date date = null;
String day_first = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
date = format.parse(datadate);
//创建日历
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
day_first = format.format(calendar.getTime());
return day_first;
}
}