java时间日期计算方法

来源:互联网 发布:机构运动简图 软件 编辑:程序博客网 时间:2024/05/17 06:31

这个类已经对时间日期的处理已经很方便了。我因为用来去一个月的天数。所以对这个类作了些稍微的修改。
希望对其他人有帮助
package com.date.util;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class FormatDate{
private static  final String [] WEEKNAME={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
private static  final String [] DATENAME={"年","月","日","时","分","秒"};
Calendar calendar=null;
public static  final int YEAR=0;//用来标识年
public static  final int MONTH=1;
public static  final int DAY=2;
public static  final int HOUR=3;
public static  final int MINUTE=4;
public static  final int SECOND=5;

//构造函数,产生一个日历对象

public FormatDate(){
calendar = new GregorianCalendar();

}
public FormatDate(Calendar calendar){
this.calendar=calendar;
}
//年
public int getYear(){
return calendar.get(Calendar.YEAR);
}
//月
public int getMonth(){
return calendar.get(Calendar.MONTH)+1;
}
//日
public int getDay(){
return calendar.get(Calendar.DAY_OF_MONTH);
}
//得到数字型的星期数
public int getWeek(){
return calendar.get(Calendar.DAY_OF_WEEK)-1;
}
//得到汉字型的星期数
public String getChweek(){
int week= getWeek();
return WEEKNAME[week];
}
public int getHour(){
return calendar.get(Calendar.HOUR_OF_DAY);
}
public int getMinute(){
return calendar.get(Calendar.MINUTE);
}
public int getSecond(){
return calendar.get(Calendar. SECOND);
}

//得到年月日时分秒格式
public String getDate(){
return getYear()+"年"+getMonth()+"月"+getDay()+"日";
}
//得到月份的天数
public int getMonthDay()
 {
 return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
 }
/*
得到某种格式的时间样式
@param style 时间的样式 如:"-" ,":"等
*/
public String getDate(String style){
if(style==null){
return getDate();
}
return getYear()+style+getMonth()+style+getDay();

}
public String getTime(){
return getHour()+"时"+getMinute()+"分"+getSecond()+"秒";
}
public String getTime(String style){
if(style==null){
return getTime();
}
return getHour()+style+getMinute()+style+getSecond();

}
/*
用来对年月日时分秒进行增加
@param times 时间的标识,如:FormatDate.YEAR或0则为在现有的年上进行增加
@param num   表示增加的数量,
*/
public void add(int times,int num){
switch(times){
case YEAR:
calendar.add(Calendar.YEAR,num);break;
case MONTH:
calendar.add(Calendar.MONTH,num);break;
case DAY:
calendar.add(Calendar.DAY_OF_MONTH,num);break;
case HOUR:
calendar.add(Calendar.HOUR_OF_DAY,num);break;
case MINUTE:
calendar.add(Calendar.MINUTE,num);break;
case SECOND:
calendar.add(Calendar.SECOND,num);break;
default :
System.out.println("函数的第一个参数不对");
}
}
public String toString(){
return getDate()+" "+getTime();

}
public static void main(String[] args){
//注意月份的使用1为二月,向前累加,
//指定一个日期
GregorianCalendar calendar = new GregorianCalendar(2004,11,1);
FormatDate fd=new FormatDate(calendar);


//使用当前日期,取系统日期
//FormatDate fd=new FormatDate();
//fd.add(1,2);
System.out.println(fd.getYear());
System.out.println(fd.getMonth());

System.out.println(fd.getDay());
System.out.println(fd.getWeek());
System.out.print(fd.getDate("-")+" ");
System.out.println(fd.getChweek());
System.out.println(fd.getHour());
System.out.println(fd.getMinute());
System.out.println(fd.getSecond());
System.out.println(fd.getMonthDay());
System.out.println(fd.getTime(":"));
System.out.println(fd.toString());

}


}