JAVA日期操作

/**
* 获取当月最大天数
* 2010-9-20上午04:51:50
* hepeng
*/
public static int getCurrentMonthMaxDay(){
  Calendar a=Calendar.getInstance();
  a.set(Calendar.DATE, 1);
  a.roll(Calendar.DATE, -1);
  return a.get(Calendar.DATE);
}

/**
* 获取今天是当月的第几天
* 2010-9-20上午04:53:10
* hepeng
*/
public static int getCurrentDay(){
Calendar c=Calendar.getInstance();
return c.get(Calendar.DAY_OF_MONTH);
}


/**
* 返回當前時間和結算開始時間的天數
* 2010-9-20上午04:53:10
* hepeng
* @throws Exception
*/
public static long getDateDays(Date d1,Date d2) throws Exception{

long dTime1 = (d1.getTime()/1000);
long  dTime2 = (d2.getTime() / 1000);
long  days = (dTime1 - dTime2) / (1 * 60 * 60 * 24);
return days;

}
/**
* 返回當月第一天的日期
* 2010-9-20上午04:53:10
* hepeng
* @throws Exception
*/
public static Date  MonthBDate() throws Exception{
  Calendar a=Calendar.getInstance();
  a.set(GregorianCalendar.DAY_OF_MONTH, 1);
  return  a.getTime();
}

/**
* 返回當月最後一天的日期
* 2010-9-20上午04:53:10
* hepeng
* @throws Exception
*/
public static Date MonthEDate() throws Exception{
  Calendar a=Calendar.getInstance();
  a.set(Calendar.DATE, 1);
  a.roll(Calendar.DATE, -1);
  return a.getTime();
}

/**
* 获取当前月份
* 2010-9-20上午04:53:10
* hepeng
* @throws Exception
*/
public static int Month() throws Exception{
Calendar c=Calendar.getInstance();
return c.get(Calendar.MONTH)+1;
}

/**
* 将yyyyMMddHHmmss的格式转化为yyyy-MM-dd HH:mm:ss
* 2010-9-20上午04:53:10
* hepeng
* @throws Exception
*/
public static Date castTimeFormat(String date) throws Exception{
Date d = Dates.parseDate(date, "yyyyMMddHHmmss");
date = Dates.formatDate(d, "yyyy-MM-dd HH:mm:ss");
    return Dates.parseDate(date);
 
}

/**
     * 返回毫秒
     * @param date 日期
     * @return 返回毫秒
     */
    public static long getMillis(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }
   
/**
* 返回日期加上多少天后的日期
* 2010-9-23上午02:25:34
* hepeng
*/
public static Date  MonthAfter(Date d,long days){
   java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTimeInMillis(getMillis(d) + ((long) days) * 24 * 3600 * 1000);
        return c.getTime();
}

/**2010-10-13下午01:54:28
* hepeng
*/
public static int Year() {
Calendar c=Calendar.getInstance();
return c.get(Calendar.YEAR);
}

你可能感兴趣的:(java,C++,c,C#)