import Java.text.*;
import Java.util.*;
public class TimeCompare{
public static void main(String[] args){
boolean flag = isDateBefore("2004-09-09 12:12:12","2005-09-09 16:00:00");
System.out.println(flag);
flag = isDateBefore("2006-09-09 01:01:01","2005-09-09 16:00:00");
System.out.println(flag);
flag = isDateBefore("2005-09-09 01:01:01");
System.out.println(flag);
}
//判斷時間date1是否在時間date2之前
//時間格式 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;
}
}
//判斷當前時間是否在時間date2之前
//時間格式 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;
}
}
}