2013-06-03 75 views
-4

将此日期格式转换成2013-01-15 06:20:00转换为此格式2013-01-15。只需简单地删除时间日期。我用转换日期至特定格式

SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-mm-dd"); 
      //Date convertedDate = new Date(); 
      Date convertedDate = null; 
      try { 
       convertedDate = dateFormat.parse(dateString); 
      } catch (ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

如何获取或删除时间的日期。

回答

1

如果要分析它仅用于显示的目的,你可以使用这一个:

String input = "2013-01-15 06:20:00"; 
System.out.println(input.split(" ")[0]); 
+0

好的答案也有效。只有两条线。大。 – URAndroid

0

尝试使用以下格式:

的SimpleDateFormat DATEFORMAT =新的SimpleDateFormat( “YYYY-MM-DD”)

,然后分析一个新的日期的ala:

字符串s = dateFormat.format(new Date())

+0

我也使用这种格式,但它的生成是这样的:苏n Jul 06 00:00:00 IST 1975 – URAndroid

+0

那么日期仍然会有一个时间分量,你不能摆脱它。格式只是决定输出结果的样子,因为在这些情况下这通常很重要。如果你得到上面的输出,你不使用我提到的格式。 – Scorpio

+0

因此,对于这个问题的任何其他解决方案。 – URAndroid

0

Just use

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
      Date convertedDate = null; 
      try { 
       convertedDate = dateFormat.parse(dateString); 
      } catch (ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
0

使用

DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM , Locale.ENGLISH); 

输出 二月20,1991

+0

这会发出错误的日期格式。再试一次。 – michaelb958

1
import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

public class DateFormatting { 
    public static void main(String[] args) throws ParseException { 
     String input = "2013-01-15 06:20:00"; 
     DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
     Date theDate = dateFormat.parse(input); 
     String output = dateFormat.format(theDate); 
     System.out.println(output); // prints 2013-01-15 
    } 
}