為什么獲取的當(dāng)前時(shí)間不對
大神幫看看我寫的這個(gè)代碼為什么獲取的當(dāng)前小時(shí)數(shù)不對,還有顯示的日歷總多出一豎列?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:paddingBottom="@dimen/activity_vertical_margin"
? ? android:paddingLeft="@dimen/activity_horizontal_margin"
? ? android:paddingRight="@dimen/activity_horizontal_margin"
? ? android:paddingTop="@dimen/activity_vertical_margin"
? ? tools:context="com.example.datetime.MainActivity"?
? ? android:orientation="vertical">
? ? <DatePicker
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:id="@+id/date" />
? ? <TimePicker
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:id="@+id/time" />
</LinearLayout>
.Java文件:
public class MainActivity extends ActionBarActivity {
DatePicker date;
TimePicker time;
int my_year,my_month,my_day,my_hour,my_minute;
Calendar my_calendar;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? date=(DatePicker) findViewById(R.id.date);
? ? ? ? time=(TimePicker) findViewById(R.id.time);
? ? ? ? my_calendar=Calendar.getInstance(Locale.CHINA);
? ? ? ? my_year=my_calendar.get(Calendar.YEAR);
? ? ? ? my_month=my_calendar.get(Calendar.MONTH);
? ? ? ? my_day=my_calendar.get(Calendar.DAY_OF_MONTH);
? ? ? ? my_hour=my_calendar.get(Calendar.HOUR_OF_DAY);
? ? ? ? my_minute=my_calendar.get(Calendar.MINUTE);
? ? ? ??
? ? ? ? time.setIs24HourView(true);
? ? ? ? date.init(my_year,my_month, my_day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
my_year=year;
my_month=monthOfYear;
my_day=dayOfMonth;
}
});
? ? ? ? time.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
my_hour=hourOfDay;
my_minute=minute;
}
});
? ? ? ??
? ? }
}
2016-11-29
因?yàn)闀r(shí)區(qū)的設(shè)置問題,顯示時(shí)間與模擬器上的時(shí)間相同就可以了
2015-08-22
和你的模擬器(虛擬機(jī))上設(shè)置的時(shí)間是一致的嗎?