-1

I am building an IOS and Android version and in my IOS, the date is formatted this way "June 23, 2017 at 8:58 AM".

In Android I am getting the date in numbers and then I assign them like this:

 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

 date_time = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
 timePicker();
}

and here is the timePicker() method:

private void timePicker(){
        // Get Current Time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        // Launch Time Picker Dialog
        TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                        mHour = hourOfDay;
                        mMinute = minute;

                        et_show_date_time.setText(date_time+" "+hourOfDay + ":" + minute);
                    }
                }, mHour, mMinute, false);
        timePickerDialog.show();
    }

which result to something like this "27-6-2017 16:55"

How can I formatted similar to the IOS version?

user3420180
  • 254
  • 2
  • 9
  • Use a Calendar object the whole time. Then, you can format it when you set the text. Trying to store individual pieces of datetime will be harder to debug – OneCricketeer Oct 01 '17 at 12:07

1 Answers1

0

You can do this by SimpleDateFormat

Date currentTime = Calendar.getInstance().getTime();
    String newFormat="";
    try
    {

        SimpleDateFormat form = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
        newFormat = form.format(currentTime);
        tvAppliedDate.setText(newFormat);


    }
    catch (Exception e)
    {

        e.printStackTrace();
        Toast.makeText(this, "parse error", Toast.LENGTH_SHORT).show();
    }
Apoorv Singh
  • 1,295
  • 1
  • 14
  • 26
  • I don't understand some of it, what is "tvAppliedDate", and what is the current time? – user3420180 Oct 01 '17 at 15:00
  • current time is the date instance variable which gives the current time , tv AppliedDate is the textview i was using , you can get the current date from picker as you are using and for time use Calendar.getInstance().getTime() – Apoorv Singh Oct 01 '17 at 18:57
  • and format it as SimpleDateFormat form = new SimpleDateFormat(HH:mm:ss"); newFormat = form.format(currentTime); // newFormat is string containing time – Apoorv Singh Oct 01 '17 at 18:57