我用时间选择器来选择时间。现在我想要的是我想比较两次。 时间选择器代码。
public class TimePickerFragment extends DialogFragment implements
TimePickerDialog.OnTimeSetListener {
public int hour;
public int min;
public String time;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
// Create a new instance of DatePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, min, true);
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
time = String.valueOf(hourOfDay) + ":" + String.valueOf(minute);
Intent intent = new Intent();
intent.putExtra("TIME", time);
getTargetFragment().onActivityResult(getTargetRequestCode(), 1,
intent);
getDialog().hide();
}
public String onTimeSelected() {
return time;
}
}
现在意图我正在以thuis格式计算时间(" 22.00")。比较时间我想要这种格式的时间(" yyyy-MM-dd HH:mm:ss&# 34)。 那么我如何将(" 22:00")转换为(" yyyy-MM-dd HH:mm:ss")????
我做了什么
try {
String time = data.getStringExtra("TIME");
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date timeCompare;
timeCompare = sdf.parse(time);
Date currentTime = new Date();
int comareResult = timeCompare.compareTo(currentTime);
if (comareResult == -1) {
time = new SimpleDateFormat(" HH:mm").format(date.getTime());
etStartTime.setText(time);
} else {
Toast.makeText(mContext, "Your cannot select future time",
Toast.LENGTH_SHORT).show();
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 0 :(得分:1)
这样做,
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = String.valueof(sdf.format(c.getTime()));
您将获得所需格式selected
的时间。
答案 1 :(得分:1)
更改此代码,
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
time = String.valueOf(hourOfDay) + ":" + String.valueOf(minute);
Intent intent = new Intent();
intent.putExtra("TIME", time);
getTargetFragment().onActivityResult(getTargetRequestCode(), 1,
intent);
getDialog().hide();
}
通过
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
Calendar date = Calendar.getInstance();
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
date.set(Calendar.AM_PM, date.get(Calendar.AM_PM));
String time = new SimpleDateFormat("HH:mm").format(date.getTime());
String time1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date.getTime());
Intent intent = new Intent();
intent.putExtra("TIME", time);
getTargetFragment().onActivityResult(getTargetRequestCode(), 1,
intent);
getDialog().hide();
}
希望它会帮助你
答案 2 :(得分:0)
试试如下:
private String getDate(String time) {
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();//get your local time zone.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
sdf.setTimeZone(tz);//set time zone.
String localTime = sdf.format(new Date(time) * 1000));
Date date = new Date();
try {
date = sdf.parse(localTime);//get local date
} catch (ParseException e) {
e.printStackTrace();
}
return date.toString();
}
希望这会对你有所帮助。
答案 3 :(得分:0)
从您看到的代码中,您要求用户输入时间(仅限小时和分钟),然后将意图中的返回值作为String传递。但是你期待一个完整的日期,包括年,月,日,小时,分钟和秒。为此,您应该要求用户分别使用DatePicker和TimePicker输入日期和时间。然后使用Hirak Chhatbar的代码,您可以获得所需的输出格式。