-1

I have a test that checks to see if a file upload was uploaded "today". I am checking the string by comparing it to a date object I am making via Javas SimpleDateFormat. This was working for a while until the site code changed and removed 0's from the day. Here is what I have below:

Date for this file is: 8/3/2016

Todays date is: 8/03/2016

Because of this zero in the day my assert no longer comes back as valid. I am currently using a contains() function to verify if the day was correct for a file upload.

I need to understand how to account for this missing zero in the day now. Any suggestions? My date date function is below. All I do is grab a string on the page and compare.

public static String currentDate(){

    Date date = new Date();
    String modifiedDate = new SimpleDateFormat("MM/dd/yyyy").format(date);

    return modifiedDate;
}
Joe C
  • 184
  • 1
  • 15
  • This is a dup of http://stackoverflow.com/questions/4216745/java-string-to-date-conversion – JeffC Aug 04 '16 at 02:44

2 Answers2

1

You can use this to get rid of leading 0 in the month. This is one way. Joda might have built in methods. Also, other way may be to normalize your file date to include 0.

    String month = today.get(Calendar.MONTH)+"";


    int monthInt = (int ) Integer.parseInt(month)+1;
    System.out.println(monthInt);

    String modifiedDate = monthInt+"/"+today.get(Calendar.DATE)+"/"+today.get(Calendar.YEAR);
    System.out.println(modifiedDate);
    return modifiedDate;
Anon
  • 2,608
  • 6
  • 26
  • 38
1

Don't use strings for comparing dates.

For Java 8 create LocalDate objects and compare them instead.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • I am trying that now, but I think in this case the format for today is going to be M/d/yyyy and will be until the 10th of this month which it will change to M/dd/yyyy. I am getting a java.time.format.DateTimeParseException because LocalDate wont account for this it seems. I have to feed it a test string starting with 08 vs the 8 I get from the element otherwise it fails. – Joe C Aug 03 '16 at 15:28
  • 1
    Spend more time learning how to teach Java to parse your date strigns. – Thorbjørn Ravn Andersen Aug 03 '16 at 16:04