1

i trying to add date to textview automatic but its give me a old date like 1/1/1970 not the current date

here is the code

     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();  
    }

so how can i make this code load the current date of the device not this old date ?

user3425815
  • 25
  • 1
  • 8

1 Answers1

0

Change from:

try {  
     set_date.setText(format.format(new Date(date)));
} catch (ParseException e) {
    e.printStackTrace();  
}

to

try {  
    set_date.setText(format.format(ca.getTime()));
} catch (ParseException e) {
        e.printStackTrace();  
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305