How to allow JSON post (and later get) only "time" (not date/timestamp) property of resource

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

How to allow JSON post (and later get) only "time" (not date/timestamp) property of resource

问题

创建API,允许以JSON格式提交“资源”,并将其持久化到后端。

其中有一个属性基本上是一天中的“时间”(格式为2:00 PM、5:00 PM等)。正在尝试探索各种选项,无论是将其作为普通字符串或文本(来自REST客户端)传递,还是应该采用特定的“时间”格式,以便API实现可以轻松接受它(易于映射/解析)。寻求建议?

示例JSON:

{
"name" : "Mark",
"age" : 15,
"cutOffTime": "2:00 PM" (????)
........
}

英文:

Creating API to allow JSON POST of "resource" with persistence to backend.

Have one of the property which is basically a "time" of the day (format 2.00 PM, 5.00 PM etc). Trying to explore various options of either passing it as plain String or text (from rest client) OR should be of any specific "time" format easy for api implementation to accept it(easy mapping/parsing). Looking for suggestions ?

Sample Json:

{

   "name" : "Mark",
   "age" : 15,
   "cutOffTime": "2.00 PM" (????)
   ........

}

答案1

得分: 1

使用 LocalTime

现代日期时间 API 首次引入于 Java SE 8,并在后续版本中进一步丰富,提供了丰富的 API 来管理日期、时间及其格式化。下面是一个示例:

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[] strTimes = { "1:00", "01:00", "1:00 pm", "1:00 am", "01:00 am", "01:00 pm", "13:10", "8:9", "8:9 am" };

        // 定义格式化器
        DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern("[h:m a][H:m]")
                .toFormatter(Locale.ENGLISH);

        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("HH:mm");

        for (String strTime : strTimes) {
            LocalTime time = LocalTime.parse(strTime, inputFormatter);

            // 要显示或保存到数据库的字符串
            String formattedTime = time.format(outputFormatter);
            System.out.println(formattedTime);
        }
    }
}

输出:

01:00
01:00
13:00
01:00
01:00
13:00
13:10
08:09
08:09

使用传统的日期时间 API 来执行相同的操作既容易出错又复杂。根据您的要求,我建议您使用 LocalTime(这也是我在示例中选择的原因)。您可以从Trail: Date Time中了解更多有关现代日期时间 API 的信息。

英文:

Go for LocalTime

The modern date-time API which was first introduced with Java SE 8 and has been further enriched in the subsequent releases, provides a rich set of API to manage date, time and their formatting. Given below is a demo of the same:

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[] strTimes = { "1:00", "01:00", "1:00 pm", "1:00 am", "01:00 am", "01:00 pm", "13:10", "8:9", "8:9 am" };

		// 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 : strTimes) {
			LocalTime time = LocalTime.parse(strTime, inputFormatter);

			// String to be displayed or saved into the database
			String formattedTime = time.format(outputFormatter);
			System.out.println(formattedTime);
		}
	}
}

Output:

01:00
01:00
13:00
01:00
01:00
13:00
13:10
08:09
08:09

It was error-prone as well as complex to do the same thing using the legacy date-time API. For your requirement, I recommend you use LocalTime (this is why I've chosen LocalTime in the demo). You can learn more about the modern date-time API from Trail: Date Time

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

发表评论

匿名网友

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

确定