我试图将日期添加到textview自动,但它给我一个旧的日期,如1/1/1970 不是当前日期
这是代码
private long date;
private DateFormat format;
String currentDateandTime;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleDateFormat datef = new SimpleDateFormat("dd/MM/yyyy");
String currentDateandTime = datef.format(new Date(date));
TextView set_date = (TextView) findViewById(R.id.set_date);
Calendar ca = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
try {
set_date.setText(format.format(new Date(date)));
} catch (ParseException e) {
e.printStackTrace();
}
那么如何让这段代码加载设备的当前日期而不是旧日期呢?
答案 0 :(得分:0)
更改自:
try {
set_date.setText(format.format(new Date(date)));
} catch (ParseException e) {
e.printStackTrace();
}
到
try {
set_date.setText(format.format(ca.getTime()));
} catch (ParseException e) {
e.printStackTrace();
}