0

Currently I am trying to create firestore documents from my android device. I am having some problems as two of my fields on firestore are of type TimeStamp. Currently I do not know how to add a timestamp from android to firestore. Only strings or ints. Here is my code with startTime and endTime set to type String. I wish to change these so I can add timestamps to these fields on my firestore docuements.

public class NewSessionActivity extends AppCompatActivity {

private EditText editTextModule, editTextTitle, editTextDate, editTextStart, editTextEnd, editTextID;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_session);

    editTextModule = findViewById(R.id.edit_text_Module1);
    editTextTitle = findViewById(R.id.edit_text_Title1);
    editTextDate = findViewById(R.id.edit_text_Date1);
    editTextStart = findViewById(R.id.edit_text_Start1);
    editTextEnd = findViewById(R.id.edit_text_End1);
    editTextID = findViewById(R.id.edit_text_docID);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.new_session_menu,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.save_session:
            saveSession();
            return true;
            default:
                return super.onOptionsItemSelected(item);
    }
    }

    private void saveSession(){
    String module = editTextModule.getText().toString();
    String title = editTextTitle.getText().toString();
    String date = editTextDate.getText().toString();
    String  startTime = editTextStart.getText().toString();
    String endTime = editTextEnd.getText().toString();
    String docID = editTextID.getText().toString();

    if (module.trim().isEmpty()||title.trim().isEmpty()||date.trim().isEmpty()){
        Toast.makeText(this,"Please fill all fields",Toast.LENGTH_SHORT).show();
        return;
    }
        CollectionReference sessionRef = FirebaseFirestore.getInstance()
                .collection("Session");
    sessionRef.add(new Session(module,title,date,startTime,endTime, docID));
    Toast.makeText(this,"Session Added",Toast.LENGTH_SHORT).show();
    finish();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I'm not sure I understand. Are you asking how to convert `String startTime = editTextStart.getText().toString();` to a date that you can then store and query in Firestore? If so, Firestore stores Java's regular `Date` class, so have a look at this https://stackoverflow.com/questions/4216745/java-string-to-date-conversion – Frank van Puffelen Aug 31 '18 at 14:29
  • On firestore my field is type TimeStamp, how do I input a timestamp from android application to my firestore database –  Aug 31 '18 at 14:32
  • Does Firestore store Java's regular Date class in TimeStamp fields? –  Aug 31 '18 at 14:37
  • Firestore can store as one of its [types](https://firebase.google.com/docs/firestore/manage-data/data-types): "Date and time - When stored in Cloud Firestore, precise only to microseconds; any additional precision is rounded down." The Android SDK converts from the internal format to/from a Java `Date`. – Frank van Puffelen Aug 31 '18 at 14:52
  • @SeanGallagher You can also take a look at **[this](https://stackoverflow.com/questions/48474957/servertimestamp-is-allways-null-on-firebase-firestore/48475027)**. – Alex Mamo Sep 01 '18 at 08:22

1 Answers1

0

It looks like you're depending on a class called Session (which you're not showing here) to help serialize all your values to Firestore. If you want some fields of the document to be Timestamp objects, you'll have to modify Session to contain java Date or Timestamp objects instead of strings. Properties of these types will become Timestamp field in the written documents. This means you will have to figure out how to parse the values of your strings to create a Date or Timestamp. If you're confused how to do this, you should ask a different question about parsing date strings (and be specific about what you're trying to convert).

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441