Setting TimeZone in DatePicker (Android)

前端 未结 3 433
日久生厌
日久生厌 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:00

    Here is the code i used in my project for adjusting time to use it.

    time_tab.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                final Calendar c = getInstance();
                mHour = c.get(HOUR_OF_DAY);
                mMinute = c.get(MINUTE);
                mYear = c.get(YEAR);
                mMonth = "" + (c.get    (MONTH) + 1);
                mDate = c.get(DATE);
    
                // Launch Time Picker Dialog
                TimePickerDialog tpd = new 
                                    TimePickerDialog(NotificationActivity.this,
                        new TimePickerDialog.OnTimeSetListener() {
    
                            @Override
                            public void onTimeSet(TimePicker view, int 
                                                      hourOfDay,
                                                  int minute) {
    
                                Hour = hourOfDay;
                                Minute = minute;
    
                                int r = minute / 10;
                                if (r == 0) {
                                    new_minute = "0" + minute;
                                } else {
                                    new_minute = minute + "";
                                }
                                if (Hour > 12) {
                                    new_hour = (hourOfDay - 12);
    
                  notification_time.setText(newStringBuilder().append(new_hour)
                                            .append(":").append(new_minute));
                                    notification_am_pm.setText("PM");
                                } else if (Hour == 12) {
    
                                    new_hour = hourOfDay;
                                    notification_time.setText(new 
                                   StringBuilder().append(new_hour)
                                            .append(":").append(new_minute));
                                    notification_am_pm.setText("PM");
                                } else {
                                    new_hour = hourOfDay;
                                    notification_time.setText(new StringBuilder().append(new_hour)
                                            .append(":").append(new_minute));
                                    notification_am_pm.setText("AM");
                                }
    
    
                                int timestamp;
                                long time = 0;
                                try {
    
            SimpleDateFormat df = newSimpleDateFormat("dd-MM-yyyy HH:mm");
           String dateStr = mDate + "-" + mMonth + "-" + mYear + " " + hourOfDay + ":" + minute;
    
                       df.setTimeZone(TimeZone.getDefault());
                       Date date = df.parse(dateStr);
                       time = date.getTime();
                       timestamp = (int) time;
                       String newtimeStamp = "" + timestamp;
            }, mHour, mMinute, false);
                tpd.show();
      }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-16 10:18

    You specify setMinDate and setMaxDate with a long primitive. There is no way to include a specific timezone during the assignment. The method will always use the devices locale settings. It's in the documentation.

    You have to first create the min and max dates you want to use then adjust for the local timezone of your device and then use their respective long's, (getTimeInMillis()), and assign it to min/max date properties of the DatePicker.

    0 讨论(0)
提交回复
热议问题