-1

I have a datepicker on my android studio app. The data has successfully shown as date, but in firebase it became a random number. This is my code

Calendar calendar = Calendar.getInstance();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMMM-YYYY");
    Date date_minimal;
    Date date_maximal;


DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        calendar.set(year, month, dayOfMonth);
                        tgl_daftar.setText(simpleDateFormat.format(calendar.getTime()));
                        tgl_daftar_date = calendar.getTime();


database.child("user").push().setValue(new dataUser(
                            nama,
                            radioButton.getText().toString(),
                            jurusan,
                            tgl_daftar_date.getTime()

When input the data, the date came out as a date like this. screenshot of data in app

But in firebase, it became like this.

screenshot of firebase

I want it to display as a date too in firebase. How do i fix this? Thank you

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

You're call tgl_daftar_date.getTime() to store the Date object. This method is documented as:

returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

So the number that is stored is the number of milliseconds since January 1, 1970, often also referred to as the epoch. This is a very common format to store timestamps in, especially in databases that (like Firebase's Realtime Database) cannot store Java's Date objects natively.

If you store a Date/timestamp right now, the value will be:

console.log(Date.now())

To convert the value from the database back to a Java Date object, see How to convert currentTimeMillis to a date in Java?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807