Date与Calendar

        Date date=new Date();//现在时间
Date date1=new Date(1000);//格林威治时间1997/01/01开始算,后面的是毫秒
Calendar calendar=Calendar.getInstance();//获得一个月历
calendar.set(2016, 0,01);
calendar.setTime(new Date());//setTime的时候才可以用new Date()
int year=calendar.get(Calendar.YEAR),
month=calendar.get(Calendar.MONTH)+1,
day=calendar.get(Calendar.DAY_OF_MONTH),
hour=calendar.get(Calendar.HOUR_OF_DAY),
minute=calendar.get(Calendar.MINUTE),
second=calendar.get(Calendar.SECOND);
System.out.printf("%d,%d,%d,%d,%d,%d\n",year,month,day,hour,minute,second);
System.out.println(date);

计算两个时间差的天数

比如2012/9/1和2016/7/1

        Calendar calendar1=Calendar.getInstance();
Calendar calendar2=Calendar.getInstance();
calendar1.set(2012, 9,1);
calendar2.set(2016, 7,1);
long time1=calendar1.getTimeInMillis();//貌似是获得时间绝对值的函数
long time2=calendar2.getTimeInMillis();
System.out.println(Math.abs((time1-time2)/(1000*60*60*24)));

借助Calendar写一个显示日历的类

class CalendarFuck{
int year,month;
String[] getCalendar(){
String []a=new String[43];
Calendar Calender1=Calendar.getInstance();
Calender1.set(year, month-1,1);
int weekday=Calender1.get(Calender1.DAY_OF_WEEK)-1;
// System.out.println(month);
int day = 0;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
day=31;
}else if(month==4||month==6||month==9|month==11)
day=30;
else if(month==2){
if(year%4==0&&year%100!=0||year%400==0)
day=29;
else
day=28;
}
for(int i=0;i<weekday;i++)
a[i]=" ";
for(int i=weekday,n=1;i<weekday+day;i++){
a[i]=String.valueOf(n);
n++;
}
for(int i=weekday+day;i<a.length;i++){
a[i]=" ";
}
return a;
}
}
上一篇:MS对WCF配置中security节点的解释


下一篇:ES 2015/6 新特性汇总