I'm still suck on Challenge 1

you Guys may be find a new way out in Challenge 2. but i’m really suck on challenge 1. How to pick a Time back .Im tried almost 5 ways ,but still cant find a solution. Can anyone help me out??? thanks a lot with true heart.

1 Like

To be honest, I found converting between the Date and Calendar objects cumbersome. So I switched over to using Calendar objects entirely and it made it easier to do the challenge.

The Java Date object seems to be a real mess, many of the methods have been deprecated in favour of methods in the Calendar object.

In the Crime object, setDate and setTime should only update the date and time components respectively of the date time (Calendar) object:

    public String getDateString() {
        return android.text.format.DateFormat.format("EEEE, MMM dd, yyyy", mDate).toString();

    public String getTimeString() {
        return android.text.format.DateFormat.format("hh:mm a", mDate).toString();

    public void setDate(Calendar date) {
        mDate.set(Calendar.YEAR, date.get(Calendar.YEAR));
        mDate.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH));

    public void setTime(Calendar time) {
        mDate.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
        mDate.set(Calendar.MINUTE, time.get(Calendar.MINUTE));

Then in CrimeFragment.java pass the date and time around as Calendar objects:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {

        if (requestCode == REQUEST_DATE) {
            Calendar date = (Calendar) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);

        if (requestCode == REQUEST_TIME) {
            Calendar time = (Calendar) data.getSerializableExtra(TimePickerFragment.EXTRA_TIME);

    private void updateDate() {

    private void updateTime() {

TimePickerFragment.java is mostly a copy paste of DatePicker fragment but you are dealing with the TimePicker object and hour and minute, but the structure is identical.

Thanks a lot, l will try yours solution

I think it would be more useful if you post your code here

In my solution I declared Calendar as member variable.
private Calendar mCalendar;
Used it in all necessary operations and give mCalendar.getTime() as 2nd parameter in sendResult(int resultCode, Date date).

public class TimePickerFragment extends DialogFragment {

    public static final String EXTRA_TIME = "com.bignerdranch.android.criminalintent.time";

    private static final String ARG_DATE = "date";

    private TimePicker mTimePicker;
    private Calendar mCalendar;

    public static TimePickerFragment newInstance(Date date) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_DATE, date);

        TimePickerFragment fragment = new TimePickerFragment();
        return fragment;

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Date date = (Date) getArguments().getSerializable(ARG_DATE);

        mCalendar= Calendar.getInstance();

        int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
        int minute = mCalendar.get(Calendar.MINUTE);

        View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_time, null);

        mTimePicker = v.findViewById(R.id.dialog_time_picker);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        } else {

        return new AlertDialog.Builder(getActivity())
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        int hour, minute;
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            hour = mTimePicker.getHour();
                            minute = mTimePicker.getMinute();
                        } else {
                            hour = mTimePicker.getCurrentHour();
                            minute = mTimePicker.getCurrentMinute();

                        mCalendar.set(Calendar.HOUR_OF_DAY, hour);
                        mCalendar.set(Calendar.MINUTE, minute);

                        sendResult(Activity.RESULT_OK, mCalendar.getTime());

    private void sendResult(int resultCode, Date date) {
        if (getTargetFragment() == null) {

        Intent intent = new Intent();
        intent.putExtra(EXTRA_TIME, date);

        getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);