MostDope
MostDope

Reputation: 13

How can show just the date and not the time in android studio

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

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

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

        mDatePicker = (DatePicker) v.findViewById(R.id.dialog_date_picker);
        mDatePicker.init(year, month, day, null);

        return new AlertDialog.Builder(getActivity())
            .setView(v)
            .setTitle(R.string.date_picker_title)
            .setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, 
                                          int      which) {
                             int year = mDatePicker.getYear();
                            int month = mDatePicker.getMonth();
                            int day = mDatePicker.getDayOfMonth();
                            Date date = new GregorianCalendar(year, 
                                                  month,  day).getTime(); 

                            sendResult(Activity.RESULT_OK, date);
                        }
                    })
            .setNegativeButton(android.R.string.cancel, null)

            .create();
}

I feel like getTime is what My problem is, but I cant fix it I just want to display the date and not the time at all but it wont let me. It always comes back as MM DD nn hhmmss zzz. All I want is the time to be displayed. here is where I want to display it

 private TextView mTitleTextView;
        private TextView mDateTextView;
        private Crime mCrime;

        public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_crime, parent, false));
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return false;
                }
            });

            mTitleTextView = (TextView) itemView.findViewById
  (R.id.crime_tile);
            mDateTextView = (TextView) itemView.findViewById
  (R.id.crime_date);


        }
            public void bind(Crime crime) {
                mCrime = crime;
                mTitleTextView.setText(mCrime.getTitle());
                mDateTextView.setText(mCrime.getDate().toString());


            }

In my list view here it always shows the format with the time in it and I just want the day, month, day of month, and year. Not sure if this makes sense, but does anyone have any ideas?

Upvotes: 0

Views: 100

Answers (1)

CCT
CCT

Reputation: 70

This is how I handle dates:

public static String DATE_FORMAT_NOW="MMM d yyyy"

final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
final Calendar cal = Calendar.getInstance();
final String timestamp = "Last Modified: " + sdf.format(cal.getTime());

You can change the DATE_FORMAT_NOW to your needs This particular format will put the date as: Jul 15 2017

SimpleDateFormat might have a lint warning, you can add SuppressLint to it if you want. I've never had any issue doing it this way

Upvotes: 2

Related Questions