0

I want to retrieve data from a particular date from Firebase. They are stored in the following format:

enter image description here

I am using a datePicker to choose a date. But I am not able to figure out how I can change the date chosen to the above format.

For example: I choose a date say 28th March 2018. I would use the code:

int day = datePicker.getDayOfMonth();
String month = datePicker.getMonth() + "";
String year = datePicker.getYear() + "";

But I am not able to figure out how to change it to the following format, Wed Mar 28 2018 00:00:00 GMT+530 (IST)

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Vishist Varugeese
  • 1,500
  • 1
  • 17
  • 30
  • a best practice is to store in your database the number of milliseconds since 1970/01/01. Have a look at: https://stackoverflow.com/questions/49390629/how-can-i-sort-data-by-field-stored-string-as-date-format-yyyy-mm-dd-hhmmss/49391814#49391814 – Renaud Tarnec Mar 26 '18 at 21:12
  • I agree with Renaud, you have to store time in the form of milliseconds, then the any conversion will be possible. – Niamatullah Bakhshi Mar 27 '18 at 00:12
  • Actually, since you're storing a *date* and not a timestamp, you can also consider storing them date in a more readable format such as `2018-12-01`. – Frank van Puffelen Mar 27 '18 at 03:33
  • I am getting the date from Google Spreadsheet which is automatically converting the date to this format. – Vishist Varugeese Mar 27 '18 at 04:34

1 Answers1

1

After spending hours, trying to figure out a way, I have finally found a solution and have realized that this was a pretty silly question to ask. Nevertheless, this is what I have come up with,

int day = datePicker.getDayOfMonth();
int year = datePicker.getYear();
int month = datePicker.getMonth() + 1;

try {
     SimpleDateFormat sdf = new SimpleDateFormat("d-M-yyyy");
     Date date = sdf.parse(day + "-" + month + "-" + year);

     sdf = new SimpleDateFormat("EEE MMM dd yyyy");
     String strDate = sdf.format(date) + " 00:00:00 GMT+0530 (IST)";

     Log.d("date_simple", strDate);

} catch (ParseException e) {
     Log.d("date_simple", e + "");
     e.printStackTrace();
}
Vishist Varugeese
  • 1,500
  • 1
  • 17
  • 30