英文:
How to retrieve Time value from JSP form and"inject it" into db entity object
问题
One of entities in my application has Time member (TIME column in MySQL db). I'd like to be able to read that time value from JSTL form (just like any other string), so Spring can inject it into that time member using setter (as for other members). Moreover, I'd like to be able to apply some filtering to it, so I can enter both 01:00 and 1:00, and transform it to 01:00.
What Time class should I use? LocalTime, java.sql.Time, or any other?
How do I apply pattern to the value read? Should I do it in the controller, or can I do it inside the setter or so?
I've come across multiple ways of reading time from JSP, but I can't seem to find a solution for applying the pattern.
英文:
One of entities in my application has Time member (TIME column in mysql db). I'd like to be able to read that time value from jstl form (just like any other string), so Spring can inject it into that time member using setter (as for other members). Moreover, i'd like to be able to apply some filtering to it, so i can enter both 01:00 and 1:00, and transform it to 01:00.
What Time class should I use? LocalTime, java.sql.Time or any other?
How do I apply pattern to value read? Should I do it in controller, or can I do it inside setter or so?
I've come across multiple ways of reading time from jsp, but i can't seem to find solution for applying pattern.
答案1
得分: 1
- 由于您想以自定义格式节省时间,我建议您将列类型更改为字符类型(例如
CHAR
或VARCHAR
)。 - 接下来,您可以使用
DateTimeFormatter
定义输入(从 JSP)和输出(到数据库)字符串的格式。
演示:
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// 测试字符串
String[] times = { "1:00", "01:00", "1:00 pm", "1:00 am", "01:00 am", "01:00 pm" };
// 定义格式化程序
DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("[h:m a][H:m]")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("HH:mm");
for (String strTime : times) {
LocalTime lt = LocalTime.parse(strTime, inputFormatter);
// 要保存到数据库的字符串
String formattedTime = lt.format(outputFormatter);
System.out.println(formattedTime);
}
}
}
输出:
01:00
01:00
13:00
01:00
01:00
13:00
注意:
- 您可以通过将它们放在
[ ]
中来添加更多模式,就像上面所示的那样。 - 对于此演示,我假设您想以
HH:mm
(即24小时)格式保存时间。
英文:
- Since you want to save time in a custom format, I suggest you change the column type to a character type (e.g.
CHAR
orVARCHAR
). - Next, you can define formats for input (from JSP) and output (to DB) strings using
DateTimeFormatter
.
Demo:
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Test strings
String[] times = { "1:00", "01:00", "1:00 pm", "1:00 am", "01:00 am", "01:00 pm" };
// Define the formatter
DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("[h:m a][H:m]")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("HH:mm");
for (String strTime : times) {
LocalTime lt = LocalTime.parse(strTime, inputFormatter);
// String to be saved to the database
String formattedTime = lt.format(outputFormatter);
System.out.println(formattedTime);
}
}
}
Output:
01:00
01:00
13:00
01:00
01:00
13:00
Notes:
- You can add more patterns by putting them inside
[ ]
as shown above. - For this demo, I've assumed that you want to save the time in
HH:mm
(i.e. 24-hour) format.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论