如何在java中的空格后删除字符串中的任何内容

Neh*_*eha 1 java android

我有一个这样的字符串:12/16/2011 12:00:00 AM
现在我想只显示日期部分,即12/16/2011在Textview上
删除其他部分.我需要做什么?

任何帮助将被appricated谢谢.

duf*_*ymo 6

使用java.text.DateFormat将String解析为Date,然后重新格式化以根据需要显示另一个DateFormat:

DateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
inputFormat.setLenient(false);
DateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy");
outputFormat.setLenient(false);
Date d = inputFormat.parse("12/16/2011 12:00:00 AM");
String s = outputFormat.format(d);
Run Code Online (Sandbox Code Playgroud)


Kri*_*lal 5

String str = "11/12/2011 12:20:10 AM";

    int i = str.indexOf(" ");
    str = str.substring(0,i);
    Log.i("TAG", str);
Run Code Online (Sandbox Code Playgroud)