你好,我要設置日期爲每個用戶選擇的時間AM/PM如何更改日期自動按時間AM/PM
我的時間
private TimePickerDialog.OnTimeSetListener timeListener1 =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hours1=hourOfDay;
min1=minute;
SimpleDateFormat formate = new SimpleDateFormat("HH:mm");
String newTime = formate.format(System.currentTimeMillis());
updateTime1();
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
int minutes = c.get(Calendar.MINUTE);
int hours = c.get(Calendar.HOUR);
int years = c.get(Calendar.YEAR);
int months = 1 + c.get(Calendar.MONTH);
int days = c.get(Calendar.DAY_OF_MONTH);
int AM_orPM = c.get(Calendar.AM_PM);
try{
if ((c.get(Calendar.AM_PM) == 1))
{
String PM = "";
if (AM_orPM == 1)
{
PM = "PM";
}
populateSetDate2(days, months, years);
Cabdate.setText("Refreshed on " + days + "-"
+ months + "-" + years + PM);
}
else if ((c.get(Calendar.AM_PM) == 0))
{
String AM = "";
if (AM_orPM == 0)
{
AM = "AM";
}
populateSetDate2(ddd1, mmm1, ddd1);
Cabdate.setText("Refreshed on " + ddd1 + "-"
+ mmm1 + "-" + yyy1 + AM);
}
}
catch (Exception e){}
/* if(hours1>=hours)
{
hours=hours1+5;
Toast.makeText(AirlinecabBooking.this, "invalid",Toast.LENGTH_LONG).show();
}*/
}
};
對於日期代碼:
@SuppressLint({ "ValidFragment", "NewApi" })
public class SelectDateFragment2 extends DialogFragment implements
OnDateSetListener {
@SuppressLint("NewApi")
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar calendar = Calendar.getInstance();
yyy1 = calendar.get(Calendar.YEAR);
mmm1 = calendar.get(Calendar.MONTH);
ddd1 = calendar.get(Calendar.DAY_OF_MONTH);
mmm1= mmm1 + 1;
Calendar plus10days = Calendar.getInstance();
plus10days.add(Calendar.DAY_OF_YEAR, 31);
d = plus10days.get(calendar.DAY_OF_YEAR);
calendar.get(Calendar.AM_PM);
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
@Override
public void onDateSet(DatePicker arg0, int yy, int mm, int dd) {
// TODO Auto-generated method stub
populateSetDate2(yy, mm + 1, dd);
}
}
public void populateSetDate2(int year, int month, int day) {
if (year == yyy1) {
if ((month >= mmm1) && (year == yyy1)) {
if(day > ddd1 && day < d)
{
Cabdate.setText(day + "/" + month + "/" + year);
final_date=year+"-"+month+"-"+day;
System.out.println(final_date);
}
else
{
Toast.makeText(getApplicationContext(),
"Sorry Problem with date. ", Toast.LENGTH_SHORT)
.show();
}
}
else if (month >= mmm1 && year == yyy1)
{
Cabdate.setText(day + "/" + month + "/" + year);
final_date=year+"-"+month+"-"+day;
System.out.println(final_date);
}
else
{
Toast.makeText(getApplicationContext(),
"Sorry Problem with month. ", Toast.LENGTH_SHORT)
.show();
}
}
else
{
Toast.makeText(getApplicationContext(),
"Sorry Problem with year. ", Toast.LENGTH_SHORT).show();
}
問題是,當我選擇任何時間格式AM/PM它設置日期作爲當前的日期,即使我選擇未來的日期..
這裏Cabdate
是我迄今爲止textview
請幫我 謝謝
plzzz給我解釋一下我在哪裏initilalize setTimeInMillis(長米利斯)M迷惑plz幫助我 – user2306201 2013-04-22 07:32:39
看到我的編輯.... – Blackbelt 2013-04-22 07:35:13