2016-04-22 56 views
-4

我在字符串對象中有日期。我想轉換成Date對象。創建一個接收字符串對象並以dd-MM-yyyy格式返回Date對象的函數

Date getDateFmString(String dateString) 
{ 

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
Date convertedCurrentDate = sdf.parse(dateString); 
return convertedCurrentDate ; 
} 

上面的函數返回下面的輸出。

Fri Apr 22 00:00:00 IST 2016 
  • ,但我想在這個格式 '2016年3月1日' 只有
  • 功能輸出應該只需要字符串。
  • 函數應該返回Date對象。
+0

它是不是已經在正確的格式? – 4castle

+0

這是字符串對象我希望它在Date對象中 –

+0

但是爲了什麼目的?是驗證字符串的格式? – 4castle

回答

2

我對網絡做了大量的研究,但是我從一位專家那裏得到了解決方案。

Date getDateFrmString(String dDate) 
    {  

    java.sql.Date dDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(sDate).getTime()); 
    return dDate; 
    } 

這就是我想要的。

0

變化從

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

日期格式

SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy"); 

希望這個作品

+0

您必須使用大寫'MM'來表示一個月份。否則,小'mm'代表分鐘。 – 4castle

+0

仍然我越來越像這樣星期五Jan 22 00:04:00 IST 2016只能是MM –

0

你用錯誤的格式解析嘗試

String dateString="01-01-2016"; 
SimpleDateFormat sdfP = new SimpleDateFormat("dd-MM-yyyy"); 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
Date convertedCurrentDate = sdfP .parse(dateString); 
String date=sdf.format(convertedCurrentDate); 
System.out.println(date); 

輸出:

2016-01-01 

DEMO1

如果你想要的格式,以dd-MM-yyyy則無需定義單獨SimpleDateFormat對象。

String dateString="01-01-2016"; 
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
Date convertedCurrentDate = sdf.parse(dateString); 
String date=sdf.format(convertedCurrentDate); 
System.out.println(date); 

OUTPUT:

01-01-2016 

DEMO2

要格式化您必須使用日期的同一格式字符串有那麼格式第一解析字符串Date對象的字符串日期它使用上述代碼中所示的所需格式。

+0

這個問題表明輸出應格式化爲dd-MM-yyyy' – 4castle

+0

我在Date對象中需要這種格式而不是在字符串對象 –

+0

@shimbushambu日期沒有任何格式檢查http://stackoverflow.com/questions/19207477/is-it-possible-to-get-java-util-date-object-with-specific-format – silentprogrammer

0

見這個例子

public Class DateFormatDemo{ 

public static void main (String args[]) { 

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy); 
String dateInString = "01/01/2015"; 

try{ 

Date date = formatter.parse(dateInString); 
System.out.println(date); 
System.out.println(formatter.format(date)); 

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

link可以幫助你與字符串到日期對象轉換

0

Date對象沒有的格式。只有String A Date對象將以任何格式輸出告訴它將被格式化爲。這一切都取決於當您撥打.format()DateFormat對象的格式。在Date對象上調用toString()方法使用DateFormat"dow mon dd hh:mm:ss zzz yyyy"

0

讓我們做一步一步來:

  1. 你有DD-MM-yyyy格式的日期爲String。
  2. 您想將其轉換爲日期。 (爲此,您正在使用SimpleDateFormat)
  3. 現在您正在打印日期。這裏的問題是你打印轉換後的日期對象或輸入字符串? 如果它的日期對象然後toString方法被稱爲日期類。

按照評論java.util.Date類是:

dow mon dd hh:mm:ss zzz yyyy 
similar to 
Fri Apr 22 00:00:00 IST 2016 

讓你在輸出獲得第二個方法是什麼一致。但是,代碼甚至是如何運行的很奇怪。

String inputStr = "11-11-2012"; 
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
Date inputDate = dateFormat.parse(input); 

未定義變量「輸入」。

什麼是可能的解決方案:

  1. 雖然打印日期,使用的SimpleDateFormat按要求將其轉換回字符串。

    Date d =new Date(); 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
    String dStr = sdf.format(dateString); 
    System.out.printn(dStr); 
    
  2. 擴展類java.util.Date並覆蓋toString,但這是一個壞主意。
+0

我想編寫一個接受字符串並以dd-MM-yyyy格式返回日期的函數。 –

+0

@shimbushambu你的評論沒有意義。 Date是一個對象,String是可以應用格式的地方。所以你需要將日期轉換爲字符串才能顯示它。否則Date類將使用您已經看到的默認值。 –

相關問題