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

  • Home
  • SEARCH
  • 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 6783561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:52:44+00:00 2026-05-26T16:52:44+00:00

I am reading in a String from a text file which contains a date

  • 0

I am reading in a String from a text file which contains a date in the form of yyMMdd I then want to convert it to type date but when I do that it loses its format. here is an exmaple of what I have tried

String strDate = matcher.group(10); // strDate would contain 111107

SimpleDateFormat formatter1 = new SimpleDateFormat("yyMMdd");
Date date = formatter1.parse(strDate); // after parsing it, the format changes to Thu Nov 03 00:00:00 EDT 2011

But if I take date and put it back into a string like so String tDate = formatter1.format(date); the string then contains the date in the format Id like to see 111107.

Is there a way I can do this? maybe if I could some how call the format function to return a date object instead of String, thanks.

Edit
I read a list of dates in from a file and load them into a map I then compare those dates to the current date which is also in the yyMMdd format and then if the date from the map is more then a week earlier than the current date I prompt the user for input and then write the date and other related info to a file, in the yyMMdd format. The reason I use a map is each line of the text file contains some information and a unique name, and I compared the date for that specific line of data so I do a map.get(aName)

here is the code, hope it helps

dbConnect();
            stmt = conn.createStatement();
            String query = "select * from OBJECT_BAC_EV where instance = 12";//VALUE <> 'Normal'";
            rslt = stmt.executeQuery(query);

            Calendar currentDate = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd");
            String dateNow = formatter.format(currentDate.getTime());
            Date curDate = (Date)formatter.parse(dateNow);
            currentDate.setTime(curDate);
            currentDate.add(Calendar.DAY_OF_MONTH, -7);
            dateNow = formatter.format(currentDate.getTime());
            curDate = (Date)formatter.parse(dateNow);

            Calendar cDate = Calendar.getInstance();
            SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
            String strDate = f.format(cDate.getTime());

            while(rslt.next())
            {
                System.out.println(rslt.getRow());

                String aValue = rslt.getString("VALUE");
                String aName = rslt.getString("NAME");
                String aObjRef = rslt.getString("ObjRef");


                if(aNoteMap.containsKey(aName))
                {

                    String n = aNoteMap.get(aName);
                    if(aDateMap.get(aName).before(curDate))
                    {
                        int answer = JOptionPane.showConfirmDialog(null, "Would you like to use last weeks note? " + n, "Hey", JOptionPane.YES_NO_OPTION);
                        if( answer == JOptionPane.YES_OPTION)
                        {
                            output.write(aName + "    "  + aObjRef + "    "  + aValue + "    "  + aDateMap.get(aName) + "    "
                                     + n  + (System.getProperty("line.separator")));
                        }
                        else if( answer == JOptionPane.NO_OPTION)
                        {
                            String newNote = JOptionPane.showInputDialog("Enter new note");
                            output.write(aName + "    "  + aObjRef + "    "  + aValue + "    "  + aDateMap.get(aName) + "    "
                                     + newNote  + (System.getProperty("line.separator")));
                        }
                    }
                    else
                        output.write(aName + "    "  + aObjRef + "    "  + aValue + "    "  + aDateMap.get(aName) + "    "
                                 + n  + (System.getProperty("line.separator")));
                }
                else
                    output.write(aName + "    "  + aObjRef + "    "  + aValue + "    "  + strDate + "    "
                            + ""  + (System.getProperty("line.separator")));
            }
            System.out.println("its closing output..");
            output.close();
        }
  • 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-05-26T16:52:45+00:00Added an answer on May 26, 2026 at 4:52 pm

    The Date class has no “internal” format, it only represents date elements. To output it using a specific format, you need to do the way you did: String tDate = formatter1.format(date);

    The reason why you think it has the “wrong format” is probably because when you try to output it, it does a toString() by default, which doesn’t format it the way you want.

    If you give us more details about how you want to use that date (include your code), then we might be able to provide suggestions on how to inject the formatter into it.

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

Sidebar

Related Questions

I have a text file in my computer which I am reading form my
I have a text file which contains content scraped from webpages. The text file
I want to read from the text file which is placed in res/raw/text i
Consider the 2 following methods of reading a string from a file: NSString *path
Using Python, how would I go about reading in (be from a string, file
Im having trouble reading from a CSV file final String DELIMITER = ,; Scanner
I am reading from a text file line by line. StreamReader reader = new
I want to read a text file which has text as follows(sample) abc ip-32-56-198-18.com
I'm reading positional records from a text file, for examle, it looks like this:
Am trying to get values from a text file in which entries are delimited

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.