-5

How do I get current date and set it to the string? my code saves the text typed in the text field and loads it, I would like to do the same for date instead.

public class MainActivity extends Activity {
    SharedPreferences sharedpreferences;
    TextView name;
    public static final String mypreference = "mypref";
    public static final String Name = "nameKey";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (TextView) findViewById(R.id.etName);
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);
        if (sharedpreferences.contains(Name)) {
            name.setText(sharedpreferences.getString(Name, ""));
        }
    }

    public void Save(View view) {
        String n = name.getText().toString();
        Editor editor = sharedpreferences.edit();
        editor.putString(Name, n);
        editor.commit();
    }

    public void clear(View view) {
        name = (TextView) findViewById(R.id.etName);
        name.setText("");
    }

    public void Get(View view) {
        name = (TextView) findViewById(R.id.etName);
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);
        if (sharedpreferences.contains(Name)) {
            name.setText(sharedpreferences.getString(Name, ""));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}
user3071284
  • 6,955
  • 6
  • 43
  • 57

1 Answers1

0
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
Malek Hijazi
  • 4,112
  • 1
  • 26
  • 31