2011-05-17 205 views
1

可能重复:
String to Date in Different Format in Java日期格式为日期

您好,

我M个接收日期在MM/dd/yyyy格式字符串数据类型并且需要将数据格式为yyyy-MM-dd的数据类型转换为数据库。 由于前请先

+0

你到目前为止尝试过什么? – Bobby 2011-05-17 07:32:22

+0

这是一个共同发病? http://stackoverflow.com/questions/6027681/parse-short-us-date-into-yyyy-mm-dd-java 5分钟后。 ;) – 2011-05-17 07:42:29

回答

2

可以使用的SimpleDateFormat:

SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy"); 
SimpleDateFormat destinationFormat = new SimpleDateFormat("yyyy-MM-dd"); 

String result = sourceFormat.format(destinationFormat.parse(input)); 
0

使用格式解析日期以特定类型

String date="07/25/2011"; 
    SimpleDateFormat dateFormatter=new SimpleDateFormat("yyyy-MM-dd"); 
      try { 
       Date d=dateFormatter.parse(date); 

      } catch (ParseException e) { 
        e.printStackTrace(); 
      } 
+0

呃,试试看。我是一个非常愚蠢的想法,使ParseException检查。现在我们必须忍受它:( – 2011-05-17 07:35:39

0

你也可以像下面这样做

String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss"; 

//Instance of the calender class in the utill package 
Calendar cal = Calendar.getInstance(); 

//A class that was used to get the date time stamp 
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); 

打印出你说的时间

System.out.println(sdf.format(cal.getTime())); 

干杯