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 6756457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:31:34+00:00 2026-05-26T13:31:34+00:00

im making an app that writes to a .txt file and at the end

  • 0

im making an app that writes to a .txt file and at the end of each “save” i want to put the date and time.

this is my code that turns the time/date into a string then saves it to file.

public class SaveFile extends Activity {

EditText txtData;
EditText txtData2;
Button btnWriteSDFile;
Button btnDelete;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    Date date = new Date(System.currentTimeMillis());
    final String datetime = dateFormat.format(date);

txtData = (EditText) findViewById(R.id.input1);
txtData2 = (EditText) findViewById(R.id.input2);

btnWriteSDFile = (Button) findViewById(R.id.save);
btnWriteSDFile.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
    // write on SD card file data in the text box

    try {
        BufferedWriter out = new BufferedWriter(new FileWriter("/sdcard/data_file.txt", true));
        out.write(txtData.getText() + "," + txtData2.getText() + "," + datetime.toString());
        out.write("\r\n");
        out.close();
        Toast.makeText(v.getContext(),"Saved",Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
    }


        txtData.setText("");
        txtData2.setText("");


}// onClick
}); // btnWriteSDFile

}// onCreate

}// AndSDcard

The problem im having is every time i save the time does not update.
each row has the same time and date?
can anybody help?
regards

  • 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-26T13:31:34+00:00Added an answer on May 26, 2026 at 1:31 pm

    You always write the time on datetime object that is created only once in the onCreate(). You should get the current time each time in onClick().

    To do this, you should se the formatter as class object and then create new Date every time when writing to file:

    out.write(txtData.getText() + "," + txtData2.getText() +
              "," + dateFormat.format(new Date());
    

    Notice that new Date() automatically gets the current time and date.

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

Sidebar

Related Questions

i'm making app that the main page include TableView but i want when i
I'm making a web app that needs to load and save UTF-8 (Korean, specifically)
I'm currently making an app that needs to print out to a text file,
I am making app that takes a screenshot of a URL requested by the
Im making an app that should let the user see video from the camera
I am making an app that records a destination (a city, state, country, town,
I'm making an app that is requesting data from a Web Service (implementing Soap).
I am making an app that has both a bottom bar and a top
I am making an app that will play sound on events but I can't
I'm making an app that will be installed and run on multiple computers, my

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.