Reputation:
I'm working on a project in which there a edittext in which user enter date and the date is store in firestore in form of timestamp but i have a problem how to save it in form of timestamp .
Upvotes: 0
Views: 102
Reputation: 529
I reproduce your scenario and I was able to cast a String to Timestamp using this:
try {
String input = "10-02-2019"; //For example 10-02-2019
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
Date parsedDate = dateFormat.parse(input);
java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println("timestamp: " + timestamp);
} catch (ParseException ex) {
Logger.getLogger(JavaApplication2.class.getName()).log(Level.SEVERE, null, ex);
}
Upvotes: 0
Reputation: 210
If your EditText has a specified input like day/month/year, you can parse your string into a date format and create a new Timestamp from that.
e.g.
String input = editText.getText().toString() //For example 10-02-2019
//Parse your String into the format and return the time in long by 'getTime()'
Timestamp tp = new Timestamp(new SimpleDateFormat("dd-MM-yyyy").parse(input).getTime());
Now you should be able to save this Timestamp (tp) in your firestore db.
Upvotes: 1