JavaWeb Experiment (2019 Summer) – Day 4

目标要求

  1. 解决昨日遗留bug

1. 解决昨日遗留bug

1.1 时间不显示的问题

查看控制台发现抱错Unparsable date巴拉巴拉的东西,对准时间格式的格式化上,修改DiaryDao的时间相关的代码。使用SimpleDateFormat 来格式化时间

结果:时间正常显示

图 4.1 时间正确显示

代码如下:

public List<Diary> queryDiary(String sql){
		ResultSet rs=conn.executeQuery(sql);
		List<Diary> list=new ArrayList<Diary>();
		try {
			while(rs.next()) {
				Diary diary=new Diary();
				diary.setId(rs.getInt(1));
				diary.setTitle(rs.getString(2));
				diary.setAddress(rs.getString(3));
				Date date;
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					date=sdf.parse(rs.getString(4));
					diary.setWriteTime(date);
				}catch(ParseException e) {
					e.printStackTrace();
				}
				diary.setUserid(rs.getInt(5));
				diary.setUsername(rs.getString(6));
				list.add(diary);
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}finally {
			conn.close();
		}
		return list;
	}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.