SimpleDateFormat dateformat = new SimpleDateFormat("mm-dd"); Date current=dateformat.parse(currentDate); Date begin=dateformat.parse(beginDate); Date end=dateformat.parse(endDate); if(current.getTime()>=begin.getTime() && current.getTime()<=end.getTime()) return true;
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * * @date 11/01/2013 */ public class DateCondition {
Date d = FORMAT.parse(date); Date from = FORMAT.parse(date.substring(0, 5) + beginDate); Date to = FORMAT.parse(date.substring(0, 5) + endDate); return (d.equals(from) || d.after(from)) && d.before(to); } catch(ParseException ex) {
throw new IllegalArgumentException(ex); } } }
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat formatmd = new SimpleDateFormat("MM-dd"); String beginDate="09-01"; String endDate="03-01"; String currentDate="2013-01-10"; Date now = null; try{ now =format.parse(currentDate);
Date current=dateformat.parse(currentDate);
Date begin=dateformat.parse(beginDate);
Date end=dateformat.parse(endDate);
if(current.getTime()>=begin.getTime() && current.getTime()<=end.getTime())
return true;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @date 11/01/2013
*/
public class DateCondition {
public static void main(String[] args) {
System.out.println(new DateCondition().match("2013-01-10"));
System.out.println(new DateCondition().match("2013-09-01"));
System.out.println(new DateCondition().match("2013-03-01"));
System.out.println(new DateCondition().match("2013-05-10"));
}
private static final DateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private String beginDate = "03-01";
private String endDate = "09-01";
public boolean match(String date) {
try {
Date d = FORMAT.parse(date);
Date from = FORMAT.parse(date.substring(0, 5) + beginDate);
Date to = FORMAT.parse(date.substring(0, 5) + endDate);
return (d.equals(from) || d.after(from)) && d.before(to);
}
catch(ParseException ex) {
throw new IllegalArgumentException(ex);
}
}
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat formatmd = new SimpleDateFormat("MM-dd");
String beginDate="09-01";
String endDate="03-01";
String currentDate="2013-01-10";
Date now = null;
try{
now =format.parse(currentDate);
}catch(ParseException e){}
String current = formatmd.format(now);
if(current.compareTo(beginDate)>=0 && current.compareTo(endDate)<=0){
System.out.println("yes");
}借助于日期格式与字符串之间的转换