您好我必须根据用户选择的时间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
请帮帮我 谢谢
答案 0 :(得分:0)
Calendar.getInstance();
返回在默认时区中使用当前时间初始化的Calendar
实例。您应该使用setTimeInMillis(long millis)
或setTime(Date date)
来更改Calendar
个实例中的时间。
当你解雇你的DatePiker
时int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
答案 1 :(得分:0)
使用此:
TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
Date mydate = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat(
"MMM dd yyyy hh:mm:ss a");
String time = formatter.format(mydate);
// Toast.makeText(MainActivity.this, time,
// Toast.LENGTH_SHORT).show();
// textExpiry.setText(formatter.format(dateTime.getTime()));
mTextTime.setText(time);
}
});
}
};
t.schedule(scanTask, 1000, 1000);