问题
i need to compare two string dates with SimpleDateformat like EE,MMM dd yyyy but when i compare it it will validate only the first value "EE" only other month,date and year not validating if anyone know this problem solution please help me to solve and thanks in advance to all
回答1:
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy"/*or whatever your format is*/, Locale.US);
try {
Date actualDate = format.parse(actualDate);
Date dateToCompare = format.parse(dateToCompare);
if (actualDate.after(dateToCompare) || actualDate.before(dateToCompare)) {
// Toast.makeText(context,""+actualDate.toString(),Toast.LENGTH_LONG).show();
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Compare it with date format and then use the String result.
回答2:
try{
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy");
Date date1 = sdf.parse("Mon Oct 17 2016");
Date date2 = sdf.parse("Tue Oct 18 2016");
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if(date1.compareTo(date2)>0){
System.out.println("Date1 is after Date2");
}else if(date1.compareTo(date2)<0){
System.out.println("Date1 is before Date2");
}else if(date1.compareTo(date2)==0){
System.out.println("Date1 is equal to Date2");
}else{
System.out.println("How to get here?");
}
}catch(ParseException ex){
ex.printStackTrace();
}
回答3:
Try this :
public static void main( String[] args )
{
try{
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy");
Date date1 = sdf.parse("date1 example");
Date date2 = sdf.parse("date2 example");
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if(date1.compareTo(date2)>0){
System.out.println("Date1 is > Date2");
}else if(date1.compareTo(date2)<0){
System.out.println("Date1 is <Date2");
}else if(date1.compareTo(date2)==0){
System.out.println("Date1 is = to Date2");
}else{
System.out.println("!!!");
}
}catch(ParseException ex){
ex.printStackTrace();
}
}
Update
If you wanna use Calendar
:
public static void main( String[] args )
{
try{
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy");
Date date1 = sdf.parse("date1 example");
Date date2 = sdf.parse("date2 example");
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
if(cal1.after(cal2)){
System.out.println("Date1 is > Date2");
}
if(cal1.before(cal2)){
System.out.println("Date1 is < Date2");
}
if(cal1.equals(cal2)){
System.out.println("Date1 is = Date2");
}
}catch(ParseException ex){
ex.printStackTrace();
}
}
If you wanna add days to your date do the following :
String date = "your date"; // your date
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
c.add(Calendar.DATE, 3); // 3 days to add
date= sdf.format(c.getTime()); // put the new date in date variable
Hope it helps
来源:https://stackoverflow.com/questions/40083129/two-string-date-compare-with-simple-dateformat-like-ee-mmm-dd-yyyy