-2

this programe should subtract 2 days tare3 and tare4 . tare3 valued from current day (from system ) tare4 value from text file (element [3]) , when i parsed string (element[3]) to date (tare4) its give`me parseexception date saved in file looked like (Thu Jan 24 00:00:00 EET 2018)

package javaapplication3;        
import java.io.*;            `     `
import java.util.*;                  
import java.text.*;


    public static void main(String[] args) throws ParseException, FileNotFoundException {


 private  static String tare5 = new String();
  static String tare2 = new String ();

          DateFormat dateFormat = new SimpleDateFormat(" dd MMM yyyy  ",Locale.ENGLISH );
        Calendar cal = Calendar.getInstance();
        Date tare4 =  new Date (); 
        tare5 = dateFormat.format(cal.getTime());
      Date  tare3 = dateFormat.parse(tare5);

          Scanner scanner;
         File Myfile =  new File("C:\\Users\\mido\\Documents\\NetBeansProjects\\JavaApplication3\\src\\javaapplication3\\products.txt");
       scanner = new Scanner (Myfile);


      while (scanner.hasNext()){

         String line = scanner.nextLine();
      StringTokenizer x = new StringTokenizer(line,"~");
         String [] element= new String [7]; 
         int counter = 0 ;

         while (x.hasMoreElements())
         {
          element[counter]=(String)x.nextElement();
         counter ++;

         }
          tare4 = dateFormat.parse(element[3]);

       if (tare4.getTime()>= tare3.getTime() )
       {
        System.out.println(element[1]+"is expired");
       }
       else {
         long def = tare3.getTime()- tare4.getTime();
         System.out.println(element[1]+"has"+def+"to expire");}
        }

        }

The stacktrace is as :

Exception in thread "main" java.text.ParseException: Unparseable date: "20/10/2015 " at java.text.DateFormat.parse(DateFormat.java:357) at javaapplication3.JavaApplication3.main(JavaApplication3.java:51)  
Naman
  • 23,555
  • 22
  • 173
  • 290

1 Answers1

0

Your input 20/10/2015 doesn't meet the cases to fall under the dateFormat. You might want to change

SimpleDateFormat(" dd MMM yyyy ",Locale.ENGLISH )

to

SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH )

Naman
  • 23,555
  • 22
  • 173
  • 290
  • Thx this error is removed but last question why the new output is – Muhamed Hussam Dec 24 '15 at 15:17
  • 10is expired 50has27388800000to expire 40has19702800000to expire 35is expired 49is expired 98is expired 76is expired 14is expired 99is expired – Muhamed Hussam Dec 24 '15 at 15:17
  • you might want to specify the details of output you expect in that case – Naman Dec 24 '15 at 15:18
  • there is function subtract dates ? – Muhamed Hussam Dec 24 '15 at 15:18
  • aha i want specific output let says the program will subtract 24/11/2016 -24/10/2015 i want the output "1 year and 1 month and 0 days to expire . did u undertand me ? – Muhamed Hussam Dec 24 '15 at 15:20
  • `d1 = format.parse(dateStart); d2 = format.parse(dateStop); //in milliseconds long diff = d2.getTime() - d1.getTime(); long diffSeconds = diff / 1000 % 60; long diffMinutes = diff / (60 * 1000) % 60; long diffHours = diff / (60 * 60 * 1000) % 24; long diffDays = diff / (24 * 60 * 60 * 1000); System.out.print(diffDays + " days, "); System.out.print(diffHours + " hours, "); System.out.print(diffMinutes + " minutes, "); System.out.print(diffSeconds + " seconds."); ` should help you – Naman Dec 24 '15 at 15:21
  • this code is for min hour second i want year month day , – Muhamed Hussam Dec 24 '15 at 15:28