1

Im working on a program which tells if the last modified date of a file is in the range of the date From and date To and if it is inside the range it will copy but im having an error

      File src = new File(sourcefile + File.separator + strErrorFile[i]);
    if(sourcefile.isDirectory())
    {
        ArrayList<Integer> alDateList = date(strList1);
        int intDateFrom1 = alDateList.get(0);
        int intDateTo1 = alDateList.get(1);
         SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        System.out.println("After Format : " + sdf.format(src.lastModified()));
    try
    {
        lastDate = Integer.parseInt(sdf.format(src.lastModified())); //line 362
        } catch (NumberFormatException e) {
          e.printStackTrace();
    }
        if(intDateFrom1 <= lastDate && intDateTo1 >= lastDate)
    {
             //copy
    }
    }

error

java.lang.NumberFormatException: For input string: "09/10/2015"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at org.eclipse.wb.swt.FortryApplication$4.widgetSelected(FortryApplication.java:362)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at org.eclipse.wb.swt.FortryApplication.open(FortryApplication.java:56)
at org.eclipse.wb.swt.FortryApplication.main(FortryApplication.java:610)
  • 2
    this is not a valid integer --> "09/10/2015", what you can do it to split the string with the delimiter "/" and then concatenate each element within the array to make the number "091015". Note if you convert this to integer the leading zero will be removed. – Ousmane D. Apr 11 '17 at 02:51
  • @OusmaneMahyDiaw how can i change it? Sorry just new – abrakadabara hastags Apr 11 '17 at 02:53
  • see my answer for further details. – Ousmane D. Apr 11 '17 at 02:57
  • Okay, if you're new, you need to go read the API docs on each of objects you're using. What is SimpleDateFormat#format actually do? What is File#lastModified actually returning? Rather the just guessing and playing code together, try and understand what each of these are doing – MadProgrammer Apr 11 '17 at 02:57

2 Answers2

2

You need to take a step back and look at what you need and what you have.

You don't want to convert the lastModified value to a String (via DateFormatter) because it doesn't give you anything of real value, not when you consider there is an entire API dedicated to working with date/time

Let's start by having a look at File#lastModified

The JavaDocs state that it returns:

A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs

Okay, this is actually good, because you can use this value to generate a LocalDateTime object...

LocalDateTime ldt = LocalDateTime.ofInstant(new Date(src.lastModified()).toInstant(), ZoneId.systemDefault());

why would you want to do this? Because LocalDateTime has methods which makes it easy to compare against other LocalDateTime objects...

    LocalDateTime from = ...;
    LocalDateTime to = ...;

    if (ldt.isAfter(from) && ldt.isBefore(to)) {
        //between...
    }

You can also use LocalDateTime#equals to compare if the two dates are equal.

If you don't need the "time" component, you can use LocalDateTime#toLocalDate to get a date (without time) based object, but the comparison process is basically the same

You can have a look at this answer which contains the overall logic for determining if a date/time values is between two given date/time values

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

java.lang.NumberFormatException: For input string: "09/10/2015"

this is not a valid integer, hence the error.

One approach which you can take is to use the String.replaceAll() method to replace every instance of / with the empty string "" which should essentially leave us with 09102015.

note - when you parse this to an integer the leading zero (0) will be removed.

Example:

String data = sdf.format(src.lastModified());

then you can do:

lastDate = Integer.parseInt(data.replaceAll("/",""));
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • but how can i do it without the StringBuilder? – abrakadabara hastags Apr 11 '17 at 02:59
  • 1
    you can, but concatenating strings "+=" within a for loop is an overhead. StringBuilder is simply a mutable string, so, I don't see a problem with it and anyway we're turning the StringBuilder back to string for parsing. – Ousmane D. Apr 11 '17 at 03:01
  • 1
    So, you have used `String#replaceAll` instead of a loop – MadProgrammer Apr 11 '17 at 03:15
  • @MadProgrammer true that is definitely a better solution, I'll update my post. – Ousmane D. Apr 11 '17 at 03:16
  • @abrakadabarahastags see update for another solution without using StringBuilder – Ousmane D. Apr 11 '17 at 03:19
  • @OusmaneMahyDiaw I tried that before using replaceAll but i dont know how to add 0 "9102015" – abrakadabara hastags Apr 11 '17 at 04:04
  • @abrakadabarahastags the answer is no you cannot add a leading 0 to an integer, you can either represent the date as a string and keep the leading 0 or use integer and lose the leading 0 that's the only two options you have. Also if the solution has solved your problem, don't forget to mark as accepted :). – Ousmane D. Apr 11 '17 at 04:06