如何获取当前日期并将其设置为字符串?我的代码保存在文本字段中输入的文本并加载它,我想对日期做同样的事情。
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;
}
}
答案 0 :(得分:0)
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());