java中日期時(shí)間應(yīng)用
Posted on 2007-05-30 18:06 停留的風(fēng) 閱讀(436) 評(píng)論(2) 編輯 收藏 所屬分類: 快速前進(jìn)的Javajava取系統(tǒng)時(shí)間
有兩種方法:
1.SimpleDateFormat sdft=new SimplleDateFormat("yy-MM-dd");
Date today=new Date();
System.out.printiln(sdft.format(today));
2.GregorianCalandar systemtime=new GregorianCalandar();
systemTime.add(GregorianCalandar.MONTH,-3);//隨便你減多少
System.out.println(systemtime.getTime());
時(shí)間比較:
//判斷時(shí)間date1是否在時(shí)間date2之前
//時(shí)間格式 2005-4-21 16:16:34
public static boolean isDateBefore(String date1,String date2){
try{
DateFormat df = DateFormat.getDateTimeInstance();
return df.parse(date1).before(df.parse(date2));
}catch(ParseException e){
System.out.print("[SYS] " + e.getMessage());
return false;
}
}
//判斷當(dāng)前時(shí)間是否在時(shí)間date2之前
//時(shí)間格式 2005-4-21 16:16:34
public static boolean isDateBefore(String date2){
try{
Date date1 = new Date();
DateFormat df = DateFormat.getDateTimeInstance();
return date1.before(df.parse(date2));
}catch(ParseException e){
System.out.print("[SYS] " + e.getMessage());
return false;
}
}
有兩種方法:
1.SimpleDateFormat sdft=new SimplleDateFormat("yy-MM-dd");
Date today=new Date();
System.out.printiln(sdft.format(today));
2.GregorianCalandar systemtime=new GregorianCalandar();
systemTime.add(GregorianCalandar.MONTH,-3);//隨便你減多少
System.out.println(systemtime.getTime());
時(shí)間比較:
//判斷時(shí)間date1是否在時(shí)間date2之前
//時(shí)間格式 2005-4-21 16:16:34
public static boolean isDateBefore(String date1,String date2){
try{
DateFormat df = DateFormat.getDateTimeInstance();
return df.parse(date1).before(df.parse(date2));
}catch(ParseException e){
System.out.print("[SYS] " + e.getMessage());
return false;
}
}
//判斷當(dāng)前時(shí)間是否在時(shí)間date2之前
//時(shí)間格式 2005-4-21 16:16:34
public static boolean isDateBefore(String date2){
try{
Date date1 = new Date();
DateFormat df = DateFormat.getDateTimeInstance();
return date1.before(df.parse(date2));
}catch(ParseException e){
System.out.print("[SYS] " + e.getMessage());
return false;
}
}