how to set date in textview automatic - android
我试图将日期添加到textview自动,但它给我一个旧日期,如1/1/1970
不是当前的日期
这是代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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(); } |
那么如何让这段代码加载设备的当前日期而不是这个旧日期呢?
改变自:
1 2 3 4 5 | try { set_date.setText(format.format(new Date(date))); } catch (ParseException e) { e.printStackTrace(); } |
至
1 2 3 4 5 | try { set_date.setText(format.format(ca.getTime())); } catch (ParseException e) { e.printStackTrace(); } |