好的,这只是一个例子......试试看
public class StackExampleDate {
Date pubdate;
public static void main(String[] args){
try {
setDate("23 mai 2013 11:05:01 GMT");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Date getDate() {
return pubdate;
}
public static void setDate(String date) throws ParseException {
//MMMM = Month as text , z = time zone, ' = character for text
SimpleDateFormat fmt_in = new SimpleDateFormat("dd MMMM yyyy HH:mm:ss z");
SimpleDateFormat fmt_out = new SimpleDateFormat("'at' H'h'mm");
Date date_in = fmt_in.parse(date);
String output_date_string = fmt_out.format(date_in);
if(isToday(date_in)){
System.out.println("today "+output_date_string);
}else{
System.out.println("Some day "+output_date_string);
}
}
public static boolean isToday(Date date) {
return isSameDay(date, Calendar.getInstance().getTime());
}
public static boolean isSameDay(Date date1, Date date2) {
if (date1 == null || date2 == null) {
throw new IllegalArgumentException("The dates must not be null");
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
return isSameDay(cal1, cal2);
}
public static boolean isSameDay(Calendar cal1, Calendar cal2) {
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The dates must not be null");
}
return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}
}
//如果结果时间不是您给定的时间..查看您的时区 GMT+n