如何获取特定国家/地区的日期时间?

时间:2018-01-11 14:43:33

标签: java android

我正在一个应用程序中工作,我正在显示一个DatePicker,后跟一个TimePicker。

现在根据我的客户要求,我想根据国家/地区名称显示Datepicker和TimePicker。

例如,如果国家/地区名称为印度,则应显示印度的当前日期和时间,如果是美国,则应显示美国的当前日期和时间。

我在stackoverflow中只遇到过一个与此相关的线程,而且也没有答案。

你能帮助我吗?

我的日期和时间选择器代码

 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();
    }

请期待一些帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用GMT和以下代码。

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);