Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:37:40+00:00 2026-06-02T13:37:40+00:00

i am attaching the code. In this code i am taking a string which

  • 0

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");

            }

        }
    }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:37:41+00:00Added an answer on June 2, 2026 at 1:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using the macro from this blog entry for attaching the Visual Studio
Here I am attaching code in which store image in Drawable and pass to
How does one go about attaching a bunch of code to an onkeydown event,
I am attaching a method to the post_save signal of my Django model. This
So, I've got this code: $(document).ready(function(){ $('form').attr('autocomplete', 'off'); }); It works great for all
Currently, what I'm doing is this: Using the built-in .NET PrintPreviewDialog Attaching my own
How can I update an existing LINQ entity? This code gives me: Cannot insert
I have written some code which produces simple animations based on cycling through a
I'm a beginner in this field and I'm facing this problem which says :[NSMutableArray
This code fails to actually save any changes: // // POST: /SomeType/Edit/5 [AcceptVerbs(HttpVerbs.Post)] public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.