如何将字符串日期以24/04/2012的形式转换为格式为24-Apr-12的日期变量,该日期变量稍后可以传递到我的Oracle数据库中。将字符串日期转换为日期
我都试过,但它说,该字符串日期不可分析:
DateFormat format = new SimpleDateFormat("dd-MMM-yy");
Date newDate = (Date) format.parse(date);
如何将字符串日期以24/04/2012的形式转换为格式为24-Apr-12的日期变量,该日期变量稍后可以传递到我的Oracle数据库中。将字符串日期转换为日期
我都试过,但它说,该字符串日期不可分析:
DateFormat format = new SimpleDateFormat("dd-MMM-yy");
Date newDate = (Date) format.parse(date);
你向后这样做:
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,你应该使用PreparedStatement
你的日期格式应为“DD/MM/YYYY”
我想你混淆了解析和格式化,尝试这样的:
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);
SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yy");
DateTime newDate = sdf.format(date);
尝试DateFormat format = new SimpleDateFormat("dd/MM/yyyy")
你为什么不尝试使用java.sql.Date。
例如:
Date newDate = new java.sql.Date(date.getTime());
所以你可以只给出一个日期一般SQL对象。
Java的Date类在很多情况下很难使用。我建议使用更好Joda Time类:
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);
由于以前没有......看看乔达时间,如果你打算用Java处理日期说。这个API比原来的Date包更清洁得多。 – pcalcao 2012-04-24 15:21:25