我在Android应用程序中使用DatePicker来显示用户可以选择的可用日期。我想在GMT时区显示日期,以便所有用户看到相同的日期。我还没有找到修改DatePicker的TimeZone的方法。我看到DatePicker使用默认的TimeZone选择TimeZone。所以我将默认的TimeZone更改为GMT并且它工作但是现在整个TimeZone已经改变,所以不是最好的解决方案。任何人对某事更好吗?谢谢
答案 0 :(得分:3)
使用long原语指定setMinDate和setMaxDate。在分配期间无法包含特定时区。该方法将始终使用设备区域设置。 It's in the documentation。
您必须首先创建要使用的最小和最大日期,然后调整设备的本地时区,然后使用各自的长,(getTimeInMillis()),并将其指定为min / max DatePicker的日期属性。
答案 1 :(得分:0)
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();
}
这应该让你工作。
答案 2 :(得分:0)
以下是我在项目中用于调整使用时间的代码。
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();
}