发布网友
共3个回答
热心网友
public static void main(String[] args) {
try {
String dbURL = "";//这里填入你的数据库的URL地址
String dbUser = "";//这里填入你的数据库的登陆用户名
String dbPsd = "";//这里填入你的数据库的登陆密码
String dbCS = "";//这里填入你的数据库的登陆连接语句
String dbName = "";//这里填入你的数据库表名
String dbSQL = "";//这里填入你的数据库的select语句
String birthStr = "";
Class.forName(dbCS);
Connection conn = DriverManager.getConnection(dbURL + "; DatabaseName= " + dbName, dbUser, dbPsd);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(dbSQL);
while (rs.next()) {
String dbBirthFielName = "";//这里填入你的数据库获取date的那个Field名字
birthStr = rs.getString("dbBirthFielName");
}
rs.close();
stmt.close();
conn.close();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date birthDate = df.parse(birthStr);
int birthMon = birthDate.getMonth();
int birthDay = birthDate.getDay();
int birthsum = birthMon + birthDay;
Date now = new Date();
int nowMon = now.getMonth();
int nowDay = now.getDay();
int nowhsum = nowMon + nowDay;
if ((birthsum - nowhsum) < 7) {
System.out.println("Happy birthday");
}
} catch (ParseException e) {
// TODO Auto-generated catch block
System.out.println("get parameter is not in conformity with the format ,Pls check the db ");
e.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
源代码在上面,看看合适不,不合适说声,我改code
热心网友
用字符串的截取方法substring
热心网友
转换为Date格式的,在获取月份和天数