更新时间:2023-02-26 18:05:09
您的问题是您应该使用自己的自定义数据格式化程序.我喜欢使用 Joda Time library 及其 DateTime 类而不是内置的 Java Date 和 Calendar 类.因此,我建议您使用 SimpleDateFormatrel="noreferrer">DateTimeFormat 创建一个 DateTimeFormatter,然后您将使用它来将您的字符串转换为 日期时间.每个输入和输出都需要一个 DateTimeFormatter.例如:
Your problem is screaming out that you should be using your own custom data formatter. I like using the Joda Time library and its DateTime class instead of the built-in Java Date and Calendar classes. Therefore, instead of SimpleDateFormat, I recommend that you use a DateTimeFormat to create a DateTimeFormatter, which you will then use to convert your String into a DateTime. You will need one DateTimeFormatter for each of input and output. For example:
String rawTimestamp = "2300"; // For example
DateTimeFormatter inputFormatter = DateTimeFormat.forPattern("HHmm");
DateTimeFormatter outputFormatter = DateTimeFormat.forPattern("hh:mm a");
DateTime dateTime = inputFormatter.parseDateTime(rawTimestamp);
String formattedTimestamp = outputFormatter.print(dateTime.getMillis());
return formattedTimestamp;
试试这个!