JAVA日期处理函数

public class DateUtil {

 /**
  * get  today
  * for example: 2013-08-13
  * @return
  *      
  */
 public static String getToday() {
  Date now = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  String today = sdf.format(now);
  return today;

 }

 /**
  * get Current month
  * @return
  */
 public static String getCurrenMonth() {
  Date now = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  String month = sdf.format(now);
  return month;

 }
 /**
  * get Current month
  * @return
  */
 public static String getCurrenWeek() {
  Date now = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("w");
  String month = sdf.format(now);
  return month;

 }

 public static String getCurrenYear() {
  Date now = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  String year = sdf.format(now);
  return year;

 }

 /**
  * get the now time
  * for example: 2013-08-28 09:48:04
  * @return
  */
 public static String getNow() {
  Date now = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String today = sdf.format(now);
  return today;

 }

 /**
  * get the Date from today i days
  * @param i
  * @return
  */
 public static String getDateFromNow(int i) {
  Date date = new Date();//取时间
  Calendar calendar = new GregorianCalendar();
  calendar.setTime(date);
  calendar.add(Calendar.DATE, i);//把日期往后增加一天.整数往后推,负数往前移动
  date = calendar.getTime(); //这个时间就是日期往后推一天的结果
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String dateString = formatter.format(date);
  return dateString;
 }

 //  public static void main(String args[]){
 //   System.out.println(DateUtil.getDateFromNow(2));
 //   System.out.println(DateUtil.getToday());
 //  } 

 /**

  * 取得当月天数

  * */

 public static int getCurrentMonthLastDay()

 {

  Calendar a = Calendar.getInstance();

  a.set(Calendar.DATE, 1);//把日期设置为当月第一天

  a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天

  int maxDate = a.get(Calendar.DATE);

  return maxDate;

 }

 /**

  * 得到指定月的天数

  * */

 public static int getMonthLastDay(int year, int month)

 {

  Calendar a = Calendar.getInstance();

  a.set(Calendar.YEAR, year);

  a.set(Calendar.MONTH, month - 1);

  a.set(Calendar.DATE, 1);//把日期设置为当月第一天

  a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天

  int maxDate = a.get(Calendar.DATE);

  return maxDate;

 }

}

你可能感兴趣的:(java)