常用日期操作Calendar

 
package com.xumian.util.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DataUtil {
 
 
 /**
  * 获取0点日期
  * @param day
  * @return
  */
 public static Calendar getDateBeginning(Calendar day){
  Calendar cal=day;
  if(cal==null){
   cal=Calendar.getInstance();
  }
  SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
  String time=format.format(cal.getTime());
  Date date;
  try{
   date=format.parse(time);
   cal.setTime(date);
  }catch(Exception e){
  }
  return cal;
 }
 
 /**
  * 获取一个月第几天的日期
  * @param day
  * @param num
  * @return
  */
 public static Calendar getDateOfMonth(Calendar day,int num){
  Calendar cal= Calendar.getInstance();
  cal.setTime(day.getTime());
  int min=cal.getActualMinimum(Calendar.DATE);
  int max=cal.getActualMaximum(Calendar.DATE);
  if(num<min){
   cal.set(Calendar.DATE, min);
  }else if(num>max){
   cal.set(Calendar.DATE, max);
  }else{
   cal.set(Calendar.DATE, num);
  }
  return cal;
 }
 
 /**
  * 获得指定周之后的日期
  * @param day
  * @param num
  * @return
  */
 public static Calendar getDayAfterWeeks(Calendar day,int num){
  Calendar cal=Calendar.getInstance();
  cal.setTime(day.getTime());
  cal.add(Calendar.WEEK_OF_MONTH, num);
  return cal;
 }
 
 /**
  * 获得指定月数之后的日期
  * @param day
  * @param num
  * @return
  */
 public static Calendar getDayAfterMonthes(Calendar day,int num){
  Calendar cal=Calendar.getInstance();
  cal.setTime(day.getTime());
  cal.add(Calendar.MONTH, num);
  return cal;
 }
 
 public static String getFirstxq(String riqi) {
  Date d = new Date();
  SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
  try {
   d = sd.parse(riqi);
  } catch (Exception e) {
   e.printStackTrace();
  }
  int n = d.getDay();
  n = (8 - n) % 7;
  long t = d.getTime() / 1000;
  t += 3600 * 24 * n;
  d.setTime(t * 1000);
  return sd.format(d);
 }

 /**
  * 获取一年中指定月的天数
  * @param year
  * @param month
  * @return
  */
 public static int getDatesOfMonth(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;
 }

 public static String getdatexq(String riqi, int ts) {
  SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  Date d = null;
  try {
   d = sf.parse(riqi);
  } catch (ParseException ex) {
  }
  Calendar c = Calendar.getInstance();
  c.setTime(d);
  c.add(c.DATE, ts);
  Date zdrq = c.getTime();
  return sf.format(zdrq);
 }

 public static String getNextDate(String dd,int n) {
  SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  Date d1 = null;
  try {
   Date d = sf.parse(dd);
   Calendar c = Calendar.getInstance();
   c.setTime(d);
   c.add(c.DATE, n);
   d1=c.getTime();
  } catch (ParseException ex) {
  }
  if(d1!=null)
   return sf.format(d1);
  else
   return "";
 }
 
 public static String getNextMouth(String cm, int n) {
  SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  Date d = null;
  try {
   d = sf.parse(cm);
  } catch (ParseException e) {
  }
  Calendar c = Calendar.getInstance();
  c.setTime(d);
  c.add(Calendar.MONTH, n);
  Date d1 = c.getTime();
  String dd1 = sf.format(d1);
  return dd1.toString();
 }
 
 public static String getTwoDay(String sj1, String sj2) {    
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");    
        long day = 0;    
        try {    
         java.util.Date date = myFormatter.parse(sj1);    
         java.util.Date mydate = myFormatter.parse(sj2);    
         day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);    
        } catch (Exception e) {    
         return "";    
        }    
        return day + "";    
    }     
 
 public static void main(String[] args){
  
  System.out.println(DataUtil.getNextDate("2011-02-23",64));
 }


}


 

你可能感兴趣的:(Date,exception,String,calendar,null,Class)