2012-04-24 16 views
1

2012年4月24日の形式の文字列の日付を、24-Apr-12の形式の日付変数に変換する方法は、後でOracleデータベースに渡すことができます。文字列の日付の変換

私はこれを試してみましたが、それは、文字列の日付が解析できないと言うている:

DateFormat format = new SimpleDateFormat("dd-MMM-yy"); 
Date newDate = (Date) format.parse(date); 
+0

...あなたはJavaで日付を扱うことになるだろう場合ジョダ時に見て:私はより良いJoda Timeクラスを使用することをお勧めします。 APIは、オリジナルのDateパッケージよりはるかにクリーンです。 – pcalcao

答えて

1

あなたが後方にこれをやっている:

DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); 
Date newDate = format.parse(date); 

DateFormat formatOutput = new SimpleDateFormat("dd-MMM-yyyy"); 
String output = formatOutput.format(newDate); 

しかし、あなたがやっていることはOracleに日付を渡している場合、あなたは本当にあなたがjava.sql.Dateを使用しようとしないのはなぜDateFormat format = new SimpleDateFormat("dd/MM/yyyy")

1

あなたの日付フォーマットは、「DD/MM/YYYY」

4

である必要があり、私はあなたが構文解析と書式設定を混乱していると思う、してみてくださいこの:

String old_date = "24/04/2012"; 

DateFormat old_format = new SimpleDateFormat("dd/MM/yyyy"); 
Date newDate = (Date) old_format.parse(old_date); 

DateFormat new_format = new SimpleDateFormat("dd-MMM-yy"); 
String date = new_format.format(newDate); 

System.out.println(date); 
0
SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yy"); 
DateTime newDate = sdf.format(date); 
0

を使用する必要があります。例えば

Date newDate = new java.sql.Date(date.getTime()); 

だから、あなただけの日付のための一般的なSQLオブジェクトを与えることができます。

0

を試してみてくださいPreparedStatement

0

多くの場合、JavaのDateクラスは非常に使いにくいです。それは前に言われていないので

DateTimeFormatter oldFormat = DateTimeFormat.forPattern("dd/mm/yyyy"); 
DateTimeFormatter newFormat = DateTimeFormat.forPattern("dd-MMM-yy"); 
DateTime dateTime = oldFormat.parseDateTime("24/04/2012"); 
String newDate = newFormat.print(dateTime); 
関連する問題