Show only date and time in Android

I am trying to show a date in android with the selected year, month and day using DatePicker. I have problems with choosing the year, month and day and displaying them on the screen in the correct format for the phone locale without displaying the time.

At the moment, the code will correctly display the date, but also displays the time.

StringBuilder string = new StringBuilder()
    // Month is 0 based so add 1
    .append(mYear).append("-")
    .append(mMonth + 1).append("-")
    .append(mDay);

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    try {   
        Date date = format.parse(string.toString());
        mDateDisplay.setText(date.toLocaleString());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

mDateDisplay is a TextView.

I cannot figure out how to get rid of the time, which is also displayed. I made a lot of documents for searching and reading, and I could not figure it out. Any help would be appreciated.

+5
source share
5 answers
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getContext());
mDateDisplay.setText(dateFormat.format(date))

This will take into account user locale settings and display only the date.

: getDateFormat ( )

+6
android.text.format.DateFormat df = new android.text.format.DateFormat();
df.format("yyyy-MM-dd", new java.util.Date());

android.text.format.DateFormat.format("yyyy-MM-dd", new java.util.Date());

LINK

::

formatter, , :

mDateDisplay.setText(String.format("%1$tY %1$tb %1$td", date));
+3

try this code

long date = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat timeonly = new SimpleDateFormat("hh:mm");
        String dateString = sdf.format(date);
        String timeString = timeonly.format(date);
        //show present date
        textView.setText(dateString);

        //show present time
        TextView.setText(timeString);
+1
source

I will spend an hour on this finally found solution.

 String datemillis = cursor.getString(cursor.getColumnIndex("field name on table"));
            SimpleDateFormat  df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");



            try {
                Date date = null;
                date = df.parse(datemillis);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String shortTimeStr = sdf.format(date);
                System.out.println(shortTimeStr);
                Y=shortTimeStr;
            } catch (ParseException e) {
                // To change body of catch statement use File | Settings | File Templates.
                e.printStackTrace();
            }
                Toast.makeText(getApplicationContext(),Y, Toast.LENGTH_LONG).show();
0
source

This works for me:

public class MainActivity extends AppCompatActivity {
TextView txt, txt1;

Button click;
Calendar c = Calendar.getInstance();


SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txt1 = (TextView) findViewById(R.id.txt1);

    click = (Button) findViewById(R.id.click);
    c.setTimeZone(TimeZone.getTimeZone("GMT"));

    click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String f=df.format(c.getTime());

            txt1.setText(f);
        }
    });       
}

}
0
source

All Articles