简体   繁体   中英

How to compare between two dates in java?

Passing my salute to every single hard working developer,

and cheers to every struggler out there.

Actually i am here because i got stuck on a very basic problem.

I am building a client-server application, and i am confused about how to compare between two dates extracted from jtable (eventually i've never got across any operations on dates in general).

I've used this code:

public static final String DATE_FORMAT_NOW = "yyyy-MM-dd";

  public static String now() {
   Calendar cal = Calendar.getInstance();
   SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
   return sdf.format(cal.getTime());

   }

 public static void ColorTheCell2(JTable jTable2){
          jTable2.getColumn("Date d'expiration").setCellRenderer(
    new DefaultTableCellRenderer() {

        @Override
    public Component getTableCellRendererComponent
    (JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column){

    Calendar datetable=Calendar.getInstance();

    String date=value.toString();
    String day=""+date.substring(8).toString();
    String month=""+date.substring(5, 7).toString();
    String year=""+date.substring(0,4).toString();

   datetable.set(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day));
    Calendar curdate=Calendar.getInstance();

     String date1=now();
     String day1=""+date1.substring(8).toString();
     String month1=""+date1.substring(5, 7).toString();
     String year1=""+date1.substring(0,4).toString();
                curdate.set(Integer.parseInt(year1),Integer.parseInt(month1)+1,Integer.parseInt(day1));


            if(datetable.before(curdate)){

                setText(value.toString());
                setBackground(Color.red);
            }
            else{
                 setText(value.toString());
            }

return this;
 }
  });
    }

Thank you for your Time. Best regards

You don't tell what the type of the value in the cell is. If it's a String, then you're doing it wrong: it should be a Date, and the renderer should use a DateFormat to render the date (in addition to setting the appropriate background. This will, for example, allow sorting the table chronologically rather than lexicographically.

If it's already a Date, then just compare it with the current date, using its compareTo method.

Your now() method is really strange, since it seems to return a String rather than a Date. The appropriate type to represent a date is Date, not String. Use a Date and format it using a DateFormat each time you need it as a String. Don't do the reverse (using a String and parsing it - manually - each time you need a Date).

Take a look at compareTo() in Calendar class

In general , I prefer using Joda Time API for dates and time related stuff in java.

If in Object value value is of type Date then cast it to Date -

Date valDate = (Date) value;

Or, if it's String then parse Date out of it -

Date valDate = new SimpleDateFormat("pattern in value").parse((String) value);

Get current date -

Date currDate = new Date();

And then you can use any of the following methods in Date class -

  1. boolean after(Date when)
  2. boolean before(Date when)
  3. int compareTo(Date anotherDate)

eg

if(valDate.before(currDate)) {

    //...

API doc: Date , SimpleDateFormat

Date implements the Comparable interface, therefore you can use the method compareTo() to compare between two dates. Same thing for Calendar , just use its compareTo() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM