Setting TimeZone in DatePicker (Android)

前端 未结 3 453
日久生厌
日久生厌 2021-01-16 09:34

I\'m using DatePicker in my Android Application to display available dates that the user can pick. I want to display the dates in the GMT Time Zone so that all users see the

3条回答
  •  春和景丽
    2021-01-16 10:06

    public void GetDate(final String flag) {
    
        final Calendar c = Calendar.getInstance();
    
        c.setTimeZone(TimeZone.getTimeZone("Australia/Canberra"));// here is your code
    
        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) {
    
                        userentered_Date = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
                        Date date = new GregorianCalendar(year, monthOfYear, dayOfMonth).getTime();
                        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        DateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
                        String strDate = df.format(date);
                        String e = sdf.format(date);
    
    
                        GetTime(strDate, e, flag);
    
                    }
                }, mYear, mMonth, mDay);
    
    
        datePickerDialog.show();
    }
    

    This should get you working.

提交回复
热议问题