0

I am working in an app where i am displaying a DatePicker followed by a TimePicker.

Now as per my client requirement, I want to show the Datepicker and TimePicker based on the country name.

For example, If the country name is India, India's present Date and Time should be shown and if it USA, USA's present date and time should be shown.

I came across only one thread in stackoverflow regarding this and that too is unanswered.

Can you guys please help me in this !

My code for Date and Time Picker

 private void datePicker() {
        // Get Current Date
        TextView mobileTxt = (TextView) findViewById(R.id.mobile);
        String countryName = getCountryNameOfPhoneNumber((String) mobileTxt.getText());

        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        date_time = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
                        //*************Call Time Picker Here ********************
                        tiemPicker();
                    }
                }, mYear, mMonth, mDay);
        datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
        datePickerDialog.show();
    }

    private void tiemPicker() {
        // 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;
                        if (mHour <= c.get(Calendar.HOUR) && mMinute <= c.get(Calendar.MINUTE)) {
                            Toast.makeText(SoundRecord.this, "Please Select future time !", Toast.LENGTH_SHORT).show();
                        } else {
                            EditText et_show_date_time = (EditText) findViewById(R.id.et_show_date_time);
                            et_show_date_time.setText(date_time + " " + hourOfDay + ":" + minute);
                        }
                    }
                }, mHour, mMinute, false);


        timePickerDialog.show();
    }

Kindly looking forward for some help!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
KarthikKPN
  • 653
  • 12
  • 22
  • 2
    "USA's present date and time should be shown" -- there is no single date and time in the USA, or in any other country that spans time zones. – CommonsWare Jan 11 '18 at 14:48

1 Answers1

1

You can use GMT and the below code.

TimeZone tz = TimeZone.getTimeZone("GMT+05:30"); 
Calendar c = Calendar.getInstance(tz); 
String mHour = c.get(Calendar.HOUR_OF_DAY);
String mMinute c.get(Calendar.MINUTE);
AA Shakil
  • 538
  • 4
  • 14