質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

2回答

1621閲覧

1986-1-12 00:00:00.0 と出力されてしまうのを, SimpleDateFormat("yyyy-MM-dd");のパターンに直したい

enenken

総合スコア3

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2023/03/23 05:31

実現したいこと

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

enenken

2023/03/24 08:00

マルチポストの件確認させていただきました. 対応させていただきました.
guest

回答2

0

ベストアンサー

以下はどうでしょうか?

html

1<td><input type="text" name="birthday" 2    th:value="${#temporals.format(userInfo.birthday, 'yyyy-MM-dd')}" /></td>

投稿2023/03/23 17:53

編集2023/03/23 17:55
popura2073

総合スコア17

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

enenken

2023/03/24 04:28

回答いただきありがとうございます! 試してみたのですが,秒数のところまで,出力されてしまう形は変わりませんでした.
enenken

2023/03/24 09:19

お世話になっております. 下記の th:value="${#temporals.format(userInfo.birthday, 'yyyy-MM-dd')}" を th:value="${#dates.format(userInfo.birthday, 'yyyy-MM-dd')}" に変更し対応させていただいたところ,望んだ挙動になりました! ご回答いただきありがとうございました!
guest

0

文字列を "yyyy-MM-dd" でパースして Date 変数に入れたからといって、その変数を(文字列化して)表示すると "yyyy-MM-dd" になるわけではありません。
必要なフォーマットになるように文字列に変換しなければなりません。

投稿2023/03/23 06:27

編集2023/03/23 06:29
jimbe

総合スコア12648

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

enenken

2023/03/23 07:23

申し訳ございません.初心者で認識が追いついていないのですが, 1,htmlから文字列を受け取り, 2,String型⇨Date型へ変換 3,Date型の変数を必要なフォーマットになるように文字列に変換 以上の工程を踏まないと求めているパターンには変換されないという認識でお間違い無かったでしょうか ご確認宜しくお願い致します.
jimbe

2023/03/23 08:01

現状は 3 が無い状態ですね。
enenken

2023/03/23 09:28

申し訳ございません. 3のやり方がわからずでした! ご回答いただきありがとうございます!
jimbe

2023/03/23 11:01

java なら(文字列→Date に使っているのと同じ) SimpleDateFormat の format メソッドで Date→文字列 も出来るのですが、 mypage.html でどう書けば良いのかは分からないので・・・。
enenken

2023/03/24 01:29

ご丁寧にご対応いただきありがとうございます! htmlでの書き方等調査したいと思います! ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問