26

我有两个字符串日期时间如下:

String Date1 = "05-09-2013 10:46:10"
String Date2 = "06-09-2013 10:46:10"

我需要比较这些日期时间,我需要结果。

4

10 回答 10

52
try{

     SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");


     String str1 = "12/10/2013";
     Date date1 = formatter.parse(str1);

     String str2 = "13/10/2013";
     Date date2 = formatter.parse(str2);

     if (date1.compareTo(date2)<0)
      {
         System.out.println("date2 is Greater than my date1");                         
      }

    }catch (ParseException e1){
        e1.printStackTrace();
    }
于 2013-09-10T12:37:14.360 回答
11

您可以使用以下内容:

    public static boolean CheckDates(String startDate, String endDate) {

    SimpleDateFormat dfDate = new SimpleDateFormat("dd-MMM-yyyy");

    boolean b = false;

    try {
        if (dfDate.parse(startDate).before(dfDate.parse(endDate))) {
            b = true;  // If start date is before end date.
        } else if (dfDate.parse(startDate).equals(dfDate.parse(endDate))) {
            b = true;  // If two dates are equal.
        } else {
            b = false; // If start date is after the end date.
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return b;
}
于 2013-09-10T12:39:57.427 回答
8
String pattern = "dd-MM-yyyy HH:mm:ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
Date one = dateFormat.parse(Date1String);
Date two = dateFormat.parse(Date2String);

现在你有两个Date对象,你可以比较它们。

于 2013-09-10T12:20:58.163 回答
2

您可以像这样将字符串转换为日期:

    String str = "12/12/1912";
   SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
   Date date = formatter.parse(str);

回到字符串

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
   System.out.println("Date is : " + formatter.format(date));

Date 有 before 和 after 方法,可以相互比较。

对了还有一个叫 Joda 的图书馆,你也可以去看看。

于 2013-09-10T12:18:45.067 回答
2

尝试Joda 时间以获得最佳效果。它的使用非常简单。

于 2013-09-10T12:22:04.597 回答
1
public static String CompareDates(String date1, String date2) {
try{
String pattern = "dd-MMM-yyyy HH:mm:ss";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
Date Date1 = formatter.parse(date1);
Date Date2 = formatter.parse(date2);
if (Date1 != null && Date2 != null) {
if (Date1 .equals(Date2 ))
{ 
//Both dates are equal.
} 
else{
//both date are not equal("use after and before to check occurrence of dates")
}
}catch (Exception e1){
e1.printStackTrace();
}
}
于 2015-11-30T10:13:46.030 回答
0

这个片段解释了当我们以字符串格式比较两个日期时,换句话说,当日期大于从日期时,不允许使用日期。

 private void setTodate() {
            // TODO Auto-generated method stub
            // Process to get Current Date
            int fYear, fMonth, fDay;
            final Calendar c = Calendar.getInstance();
            fYear = c.get(Calendar.YEAR);
            fMonth = c.get(Calendar.MONTH);
            fDay = c.get(Calendar.DAY_OF_MONTH);
            // Launch Date Picker Dialog
            DatePickerDialog dpd = new DatePickerDialog(RemindMeDetails.this,
                    new DatePickerDialog.OnDateSetListener() {
                        @SuppressLint("SimpleDateFormat")
                        @Override
                        public void onDateSet(DatePicker view, int year,
                                int monthOfYear, int dayOfMonth) {                  
                        try{                        
                            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");        
                            String fdate=recr_edit_Fromdate.getText().toString();                       
                            Date dateFD = formatter.parse(fdate);                       
                            String tdate=(dayOfMonth + "-"
                                    + (monthOfYear + 1) + "-" + year).toString();
                            Date dateTD = formatter.parse(tdate);                       
                            if (dateFD.compareTo(dateTD)<=0)
                              {
                                /* System.out.println("tdate is Greater than my fdate");  
                                 recr_edit_Todate.setText(dayOfMonth + "-"
                                            + (monthOfYear + 1) + "-" + year);*/
                                recr_edit_Todate.setText(tdate);
                              }
                            else{
                                showmessage("Alert", "Start date is not greater than End date");                            
                        }catch (ParseException e1){
                            e1.printStackTrace();
                        }   
                        }
                    }, fYear, fMonth, fDay);
            dpd.show();
        }// method closed
于 2014-12-16T12:36:23.540 回答
0

简单代码

fun CompareDates(str_date1:String,str_date2:String):Int {
    lateinit var date1:Date
    lateinit var date2:Date

    if (!str_date1.isEmpty()&&!str_date2.isEmpty()) {
        val formatter = SimpleDateFormat("dd/MM/yyyy")
        date1 = formatter.parse(str_date1)
        date2 = formatter.parse(str_date2)
    }

    return date1.compareTo(date2)
}

整数返回类型

if (CompareDates() > 0) {
 // comes when date1 is higher then date2
} else {
 // comes when date2 is higher then date1
}
于 2021-03-10T06:45:28.027 回答
0
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = sdf.parse("2010-02-22");
            Date date2 = sdf.parse("2010-03-23");
            int i = date1.compareTo(date2);
            switch (i){
                case -1: //date1<date2 = -1
                    return sDate2;
                case 1: //date1>date2 = 1
                    return sDate1;
                case 0: //date1==date2= 0
                default:
                    return sDate2;
            }
        } catch (ParseException e) {
            return sDate1;
        }
于 2018-07-04T16:38:04.340 回答
-1

尝试这个,

String pattern = "<yourPattern>";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
try {
 Date one = dateFormat.parse(<yourDate>);
 Date two = dateFormat.parse(<yourDate>);

}
catch (ParseException e) {}
于 2013-09-10T12:18:08.430 回答