実現したいこと
1986-1-12 00:00:00.0 と出力されてしまうのを,
SimpleDateFormat("yyyy-MM-dd");のパターンに直したい
前提
社員情報管理システムの開発をしており,生年月日情報を出力をしている際に,エラーは発生しませんが,上記のように出力されてしまいました.
発生している問題・エラーメッセージ
SimpleDateFormat("yyyy-MM-dd");のパターンで出力したいが, 1986-1-12 00:00:00.0 と出力されてしまう
該当のソースコード
package jp.co.sss.sys.controller; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.SessionAttributes; import jp.co.sss.sys.entity.Employee; import jp.co.sss.sys.form.LoginForm; import jp.co.sss.sys.repository.EmployeeRepository; /** * * */ @Controller @SessionAttributes(types = Employee.class) public class IndexController { @Autowired EmployeeRepository empRepository; LoginForm loginform; /** * ログイン画面を表示する * @param loginForm * @return login.html */ @RequestMapping(path = "/login", method = RequestMethod.GET) public String login( LoginForm loginForm,BindingResult br,Model model) { return "login"; } @Autowired HttpSession session; // 処理 /** * 入力された値を元にログイン認証し、トップ画面に遷移する * * @param req * @param res * @param loginForm * @return top.html */ @RequestMapping(path = "/top", method = RequestMethod.POST) public String login(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) { //ログインした人の情報 String empId = req.getParameter("empId"); String password = req.getParameter("password"); Employee employee = empRepository.findByEmpIdAndPassword(empId, password); //セッションデータ設定 session.setAttribute("userInfo",employee); //ログインユーザー情報 model.addAttribute("employee",employee); //ログインチェック if(employee == null) { //存在しない場合 return "login"; }else { //存在した場合 //社員情報一覧 List<Employee> empAll= empRepository.findAll(); model.addAttribute("empAll",empAll); return "top"; } } @RequestMapping(path = "/top", method = RequestMethod.GET) public String top(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) { List<Employee> empAll= empRepository.findAll(); model.addAttribute("empAll",empAll); return "top"; } //ユーザー更新入力情報 th:object empPost @RequestMapping(path = "/mypage", method = RequestMethod.POST) public String empUser(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) throws ParseException { session = req.getSession(); String empName = req.getParameter("empName"); String password = req.getParameter("password"); String strDate = req.getParameter("birthday"); String savegender = req.getParameter("gender"); SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd"); Date birthday = sdFormat.parse(strDate); int gender = Integer.parseInt(savegender); Employee userInfo = (Employee) session.getAttribute("userInfo"); userInfo.setEmpName(empName); userInfo.setPassword(password); userInfo.setBirthday(birthday); userInfo.setGender(gender); Employee updateEmployee = empRepository.save(userInfo); return "edit_fin"; } // TODO 自動生成されたメソッド・スタブ //ログインユーザー情報 //マイページリンク押下,既存情報の出力 @RequestMapping(path = "/mypage", method = RequestMethod.GET) public String empLink(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) { session = req.getSession(); Object userInfo=session.getAttribute("userInfo"); model.addAttribute("userInfo",userInfo); return "mypage"; } }
Employee.java
1package jp.co.sss.sys.entity; 2import java.util.Date; 3 4import javax.persistence.Column; 5import javax.persistence.Entity; 6import javax.persistence.Id; 7import javax.persistence.Table; 8import javax.validation.constraints.NotEmpty; 9import javax.validation.constraints.Size; 10 11/** 12 * 13 * 14 */ 15@Entity 16 17 18 19@Table(name = "employee") 20public class Employee { 21 22 23 /* 24 * 社員番号 25 */ 26 @Id 27 @Column(name ="emp_id" ) 28 @NotEmpty(message = "社員番号は入力必須項目です") 29 @Size(max = 5, message = "社員番号は5文字以内で入力してください") 30 private String empId; 31 32 @Column (name="emp_name") 33 @NotEmpty(message = "パスワードは入力必須項目です") 34 @Size(max = 16, message = "パスワードは16文字以内で入力してください") 35 private String empName; 36 37 @Column (name="password") 38 private String password; 39 40 @Column (name="birthday") 41 private Date birthday; 42 43 @Column (name="gender") 44 private int gender; 45 46 @Column (name="delete_at") 47 private Boolean delete; 48 49 public void setEmpId(String empId) { 50 this.empId = empId; 51 } 52 public String getEmpId() { 53 return empId; 54 } 55 56 57 public void setEmpName(String empName) { 58 this.empName = empName; 59 } 60 public String getEmpName() { 61 return empName; 62 } 63 64 65 public void setPassword(String password) { 66 this.password = password; 67 } 68 public String getPassword() { 69 return password; 70 } 71 72 73 public void setBirthday(Date birthday) { 74 this.birthday = birthday; 75 } 76 public Date getBirthday() { 77 return birthday; 78 } 79 80 81 public void setGender(int gender) { 82 this.gender = gender; 83 } 84 public int getGender() { 85 return gender; 86 } 87 88 89 public void setDelete(Boolean delete) { 90 this.delete = delete; 91 } 92 public Boolean getDelete() { 93 return delete; 94 } 95} 96
mypage.html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="UTF-8" /> 5<link th:href="@{/css/style.css}" rel="stylesheet" /> 6<link th:href="@{/css/layout.css}" rel="stylesheet" /> 7<title>マイページ変更・確認画面</title> 8</head> 9<body> 10 <!-- ヘッダー --> 11 12 <header th:include="layout/header :: head"> 13 14 15 </header> 16 17 <!-- サイドバー --> 18 <aside th:include="layout/aside :: side"></aside> 19 <!-- メイン --> 20 <article class="mypage"> 21 <form action="" th:action="@{/mypage}" th:field="${userUpdate}" 22 method="post"> 23 <h3 class="page_title">マイページ変更・確認画面</h3> 24 25 26 <table class="table employee"> 27 28 <tr th:each="userInfo : ${userInfo}" th:object="${userInfo}"> 29 <tr> 30 <th class="cell_title">社員番号</th> 31 <td th:text="*{userInfo.empId}"></td> 32 </tr> 33 <tr> 34 <th class="cell_title">名前</th> 35 <td><input type="text" name="empName" 36 th:value="${userInfo.empName}" /></td> 37 38 </tr> 39 <tr> 40 <th class="cell_title">パスワード</th> 41 <td><input type="password" name="password" 42 th:value="${userInfo.password}" /></td> 43 44 </tr> 45 <tr> 46 <th class="cell_title">生年月日</th> 47 <td><input type="text" name="birthday" 48 th:value="${userInfo.birthday}" /></td> 49 50 </tr> 51 <tr> 52 <th class="cell_title">性別</th> 53 <td><input type="radio" name="gender" 54 th:value="${userInfo.gender}" 55 th:switch="*{userInfo.gender == 1}" 56 th:checked="${userInfo.gender == 1}" /> 男 <input type="radio" 57 name="gender" th:switch="*{userInfo.gender == 2}" 58 th:checked="${userInfo.gender == 2}"> 女</td> 59 60 61 </tr> 62 </table> 63 <div class="btn_area_center"> 64 <input type="submit" value="変更確定" class="btn"> 65 </div> 66 67 68 69 70 </form> 71 </article> 72 <!-- フッダー --> 73 <footer th:include="layout/footer :: foot"></footer> 74 75</body> 76</html>
試したこと
"yyyy-MM-dd"の部分を変えてはいるがいずれも同じ出力になります.
補足情報(FW/ツールのバージョンなど)
eclipce2022
javaSE16
PostgreSQL
回答2件
あなたの回答
tips
プレビュー