如何从JSP表单中检索时间值并将其“注入”到数据库实体对象中。

huangapple go评论43阅读模式
英文:

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

  1. 由于您想以自定义格式节省时间,我建议您将列类型更改为字符类型(例如 CHARVARCHAR)。
  2. 接下来,您可以使用 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

注意:

  1. 您可以通过将它们放在 [ ] 中来添加更多模式,就像上面所示的那样。
  2. 对于此演示,我假设您想以 HH:mm(即24小时)格式保存时间。
英文:
  1. Since you want to save time in a custom format, I suggest you change the column type to a character type (e.g. CHAR or VARCHAR).
  2. 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:

  1. You can add more patterns by putting them inside [ ] as shown above.
  2. For this demo, I've assumed that you want to save the time in HH:mm (i.e. 24-hour) format.

huangapple
  • 本文由 发表于 2020年8月2日 00:11:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63207334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定