-6

I am using this code to get current date to string, however, the date it gets is 1899-12-31 how is this possible?

    // save date
                String date = new SimpleDateFormat("yyyy-MM-dd")
                        .format(new Date(0, 0, 0));
                file.writeToSD("Date: " + date.toString());
Nicholas
  • 7,403
  • 10
  • 48
  • 76
Cassie
  • 223
  • 2
  • 12
  • Don't use `Date`. Use a `Calendar` instead. If you want to know why you get that seemingly odd output, have a look at [the docs](https://developer.android.com/reference/java/util/Date.html#Date(int,%20int,%20int)). – Mike M. Jul 09 '16 at 05:02
  • 3
    Uh, why? Just use a `Calendar`. There are plenty of examples. http://stackoverflow.com/questions/8654990/how-can-i-get-current-date-in-android – Mike M. Jul 09 '16 at 05:06

2 Answers2

1

0,0,0 is the epoc -- in this case the year 1900

try date();

better take a look at the date page

Reenactor Rob
  • 1,508
  • 1
  • 11
  • 20
0

If you want to retrieve current date then use only use newDate(). Its return current millisecond

In your case you should use bellow code

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String currentDate = dateFormat.format(new Date());
file.writeToSD("Date = " + currenDate);
Sumit Bhatt
  • 718
  • 4
  • 19