在Android中仅显示日期而不显示时间

5
我正在尝试在Android中显示一个日期,其中年、月和日是使用DatePicker选择的。 我遇到的问题是将所选的年、月和日以正确的本地化格式显示在屏幕上,而不显示时间。
目前的代码可以正确显示日期,但也会显示时间。
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是一个TextView。

我无法弄清楚如何去掉同时显示的时间。我已经进行了大量搜索和阅读文档,但仍然无法解决。任何帮助都将不胜感激。

5个回答

8
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getContext());
mDateDisplay.setText(dateFormat.format(date))

这将考虑用户的语言环境设置,并仅显示日期。

相关文档: getDateFormat(Context context)


2
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));

1
这并没有考虑到手机的语言环境。 - Lukazoid

1
尝试这段代码。
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);

0

我花了好几个小时,终于找到了解决方案。

 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
这对我有效:
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);
        }
    });       
}

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接