tarun verma
tarun verma

Reputation: 221

parse string to date and time but not getting appropriate parsing

i am attaching the code. In this code i am taking a string which is a date from a flat text file. It consist of AM/PM(12 Hour Format). When i am parsing it, it is not parsing well not parsing in 24 hour format. I want the time difference b/w the current time and the string from file. And because of AM/PM its not converting in 24 hour format. So its showing same time difference whether it is PM or AM. So tell me any fruitful suggestion if you have. I ll be really thankful to you guys.

public class Casting {

/**
 * @param args
 */
    static FileReader fw;
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try{
            fw = new FileReader("E://796F_log.txt");
            BufferedReader pw =new BufferedReader(fw);
            String last_Line_From_File="" ; 

            for (String message  = pw.readLine(); message != null ; message = pw.readLine()) {
                    last_Line_From_File = message;
            }

            String[] array_Of_Data = last_Line_From_File.split("\\s") ;
            String time = array_Of_Data[0]+" "+array_Of_Data[1]+" "+array_Of_Data[2] ;

            System.out.println(time);
            DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
            Calendar cal = Calendar.getInstance();
            String current_time = dateFormat.format(cal.getTime());

            Date d1 = dateFormat.parse(time);
            Date d2 = dateFormat.parse(current_time);



            long total_time = d2.getTime()-d1.getTime();
            total_time /= 1000 ;

            System.out.println("current time "+d2.getHours()+":"+d2.getMinutes()+":"+d2.getSeconds()+"\n"+d1.getHours()+":"+d1.getMinutes()+":"+d1.getSeconds());
            if(total_time <= 500)
            {
                System.out.println("working "+total_time);
            }
            else
                System.out.println("nt working   "+total_time);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("did the smart thing or dumb thing");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            try {
                fw.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("we did attempt closing");

            }

        }
    }

}

Upvotes: 0

Views: 125

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500055

The problem is your format:

"MM/dd/yyyy HH:mm:ss a"

The HH here means the 24-hour value. So it's expecting "19" for 7pm. It's almost always wrong to include both "HH" and "a" (the AM/PM designator) in the same format string.

You probably want either

"MM/dd/yyyy hh:mm:ss a"

or

"MM/dd/yyyy h:mm:ss a"

depending on whether you get things like "07:00:00 AM" or "7:00:00 AM".

If you're doing any significant amount of date/time work, I'd recommend using Joda Time instead of Date/Calendar, by the way.

Upvotes: 3

Related Questions