java.sql.date 字符串,字符串到java.sql.Date

I realize this has been asked a lot. I did actually look. I've spent hours looking around and trying to figure this out. I'm supposed to be making a program that stores what amounts to a list of appointments in a database, with a description, date, start time, and end time. It has to take input from the user to add or cancel appointments, so as far as I know that means I need to convert a string to a date.

These are my imports:

import java.io.File;

import java.io.IOException;

import java.sql.Connection;

import java.sql.Date;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Time;

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Scanner;

As you can see, no java.util.Date there. Here is the bit where I'm getting the error:

private static java.sql.Date getDay()

{

Scanner in = new Scanner(System.in);

String input;

Date apptDay = null;

DateFormat df = new SimpleDateFormat("yyyy/MM/dd");

java.sql.Date sqlDate;

System.out.println("\nPlease enter the date of the appointment, format: yyyy/mm/dd");

while(apptDay == null)

{

try

{

input = in.next();

apptDay = (Date) df.parse(input);

}

catch(ParseException e)

{

System.out.println("Please enter a valid date! Format is yyyy/mm/dd");

}

}

sqlDate = new Date(apptDay.getTime());

return sqlDate;

}

I've added java.sql.Dates to it and mucked about with it a bunch trying to get it to work, but it's still giving me this:

Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

at Calendar.getDay(Calendar.java:47)

Any ideas on what I'm doing wrong or how to make this work would be very much appreciated.

Edit: I thought perhaps it would help if I added the bit of code that is calling this so maybe it will be more clear how I am trying to use it, so here is the addAppointment() method, so you can see where getDay() is being called and where it's going.

public static void addAppointment() throws SQLException

{

//get the info

String desc = getDesc();

java.sql.Date apptDay = getDay();

Time[] times = getTime();

Time startTime = times[0];

Time endTime = times[1];

int key;

Connection conn = SimpleDataSource.getConnection(); //connect to the database

try

{

PreparedStatement max = conn.prepareStatement("SELECT MAX(ID) FROM Calendar");

ResultSet result = max.executeQuery();

key = result.getInt("ID") + 1;

PreparedStatement stat = conn.prepareStatement(

"INSERT INTO Calendar " +

"VALUES (?, ?, ?, ?, ?)");

stat.setInt(1, key);

stat.setString(2, desc);

stat.setDate(3, apptDay);

stat.setTime(4, startTime);

stat.setTime(5, endTime);

stat.execute();

System.out.println("\nAppointment added!\n");

}

finally

{

conn.close(); //finished with the database

}

}

解决方案

It would be much simpler to change the input format to yyyy-MM-dd and use java.sql.Date.valueOf(String date) method which converts a string in the above format to a java.sql.Date value directly.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值