我有一個用戶輸入日期,輸入的日期是一個SimpleDateFormat。 DD-MM-YYYY。 我將它保存在一個Class對象中,然後想要顯示它。 當我在JOptionPane消息框中顯示它時。它顯示了這一點。 [email protected] 什麼可能導致這種情況,或者如何將它轉換爲字符串並顯示它? 下面是我將從字符串輸入的日期轉換爲日期的代碼。 現在我將fDate存儲到SimpleDateFormat flightDate中;在對象中。SimpleDateFormat轉換爲字符串
try
{
SimpleDateFormat fDate = new SimpleDateFormat("dd-MM-yyyy");
fDate.setLenient(false);
fDate.parse(dateText);
Main.flightObjects[Main.flightCount].setFlightDate(fDate);
}
catch(java.text.ParseException d)
{
JOptionPane.showMessageDialog(null,
"Please make sure your date is in the correct format! dd-mm-yyyy\n e.g. 16-03-2013",
"Date Error 1",1);
}
String dateS;
dateS = (String)flightDate.format(flightDate);
String output = "Flight num: " + flightNumber + "\nDate: " + dateS + "\nDeparting City: " + departCity + "\nArrival City: " + arriveCity + "\nAvailable Seats: " + seatsAvailable + "\nSold Seats: " + seatsSold + "\nSeat Price: R" + seatPrice;
return output;
這^是我想要顯示的日期。我將如何轉換回字符串? flightDate被聲明爲SimpleDateFormat flightDate;並且在try catch中的代碼中分配了日期。 謝謝。
'SimpleDateFormat'就是一個類從日期轉換爲字符串 –