java日期操作

package com.basic;
import java.util.*;
import java.text.*;
public class DateT {

public void simpleDate(){
//Calendar.getInstance().getTime();返回date对象
System.out.println(new Date());
System.out.println(new java.sql.Date(new Date().getTime()));//依照本地数据库
System.out.println(new Date().toLocaleString());//依照本地操作系统
}
//日期格式化[用于程序处理]
public String dateFormate(Date date){
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss SSS");//精确到毫秒[大小写敏感]
String strDate=sf.format(date);
return strDate;
}
//设置具体的时间[缺省项会按当前时间设置],定时器的应用
public Date getSpecificDate(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2010);//
calendar.set(Calendar.MONTH, 11);//0开始计数,加1
calendar.set(Calendar.DAY_OF_MONTH, 32);//Calendar.DATE与之同义
calendar.set(Calendar.HOUR_OF_DAY, 19);//指示一天中的小时,Calendar.HOUR 指示上午或下午的小时
calendar.set(Calendar.MINUTE, 16);//
calendar.set(Calendar.SECOND, 0);//
calendar.set(Calendar.MILLISECOND ,224);//毫秒
//calendar.set(2010,0,6,13,56,20);//注意月要加1
Date time = calendar.getTime();
return time;

}
//显示当前时间[可以得到星期]
public String showCurrentDate(){
String[] weekdays={"","日","一","二","三","四","五","六"};
GregorianCalendar currentDay = new GregorianCalendar();
int year= currentDay.get(Calendar.YEAR);
int month=currentDay.get(Calendar.MONTH);
int today=currentDay.get(Calendar.DAY_OF_MONTH);
int weekday=currentDay.get(Calendar.DAY_OF_WEEK);//1~7,星期第一天星期日
int hour=currentDay.get(Calendar.HOUR_OF_DAY);
int minute=currentDay.get(Calendar.MINUTE);
int second=currentDay.get(Calendar.SECOND);
String hourS=(hour>9)?String.valueOf(hour):"0"+hour;
String minuteS=(minute>9)?String.valueOf(minute):"0"+minute;
String secondS=(second>9)?String.valueOf(second):"0"+second;
String now=year+"年"+(month+1)+"月"+today+"日"+" 星期"+weekdays[weekday]+" "+hourS+":"+minuteS+":"+secondS;
return now;

}
//日期累加,减
public void dateAdd(){
Calendar c = Calendar.getInstance();
//c.add(Calendar.MONTH, 6);半年后的日期
c.add(Calendar.DATE, 183);//和DAY_OF_MONTH 是同义词
String date=dateFormate(c.getTime());
System.out.println(date);
}
//将字符串转为日期
public Date strParseDate(String dstr) throws ParseException{
DateFormat df= new SimpleDateFormat("yyyy-MM-dd");
Date date=df.parse(dstr);
return date;
//-------------------------
//java.sql.Date date = java.sql.Date.valueOf(dstr); dstr只为yyyy-mm-dd形式

}
//日期比较[从数据库取出时间串与当前串比较]
public void dateCompara() throws ParseException{
String end_date = "2009-12-08";
DateFormat df= DateFormat.getDateInstance();
//String dateNowStr=df.format(new Date()) ;
//Date nowDate = df.parse(dateNowStr);
Date endDate = df.parse(end_date);
System.out.println(new Date().after(endDate));//不够精确
//----------------------------
Calendar cdb = Calendar.getInstance();
DateFormat dft= new SimpleDateFormat("yyyy-MM-dd");
Date dbDate=dft.parse(end_date);
cdb.setTime(dbDate);
Calendar cnow = Calendar.getInstance();//当前系统时间
long i=cnow.getTimeInMillis()-cdb.getTimeInMillis();
System.out.println(i);
//----------------------------
long day=cnow.getTime().getTime()-dbDate.getTime();//结果和前相同
int days=(int)(day/24/3600/1000);
System.out.println(days);

}
public static void main(String[] args) throws ParseException {
DateT d=new DateT();
d.simpleDate();
System.out.println("--------------");
System.out.println(d.dateFormate(new Date()));
System.out.println("--------------");
System.out.println(d.dateFormate(d.getSpecificDate()));
System.out.println("--------------");
System.out.println(d.showCurrentDate());
System.out.println("--------------");
d.dateAdd();
System.out.println("--------------");
System.out.println(d.strParseDate("2009-08-17"));
System.out.println("--------------");
d.dateCompara() ;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值