Java:将日历转换为字符串并返回的最简单方法[关闭](Java: Easiest way to convert Calendar to String and back [closed])

编程入门 行业动态 更新时间:2024-10-07 10:21:40
Java:将日历转换为字符串并返回的最简单方法[关闭](Java: Easiest way to convert Calendar to String and back [closed])

我的应用程序使用的API只接受Map<String, String> 。 因此,必须将所有对象都转换为此表单,然后才能将其传递给API。

对于Boolean, BigDecimal等简单类来说Boolean, BigDecimal这是相当简单的。但是它对于转换Calendar对象来说有点问题。

我不太熟悉Java中日历/日期等的所有细节,因此,如果将我的日历转换为字符串然后再返回,我将不胜感激。

当我的应用程序收到请求时,它已经构建了Calendar。 然后,它必须将其转换为String(1),将其传递给API(2),然后将其转换回另一端的Calendar(3)。

所以我控制着1和3,但不是2。

基于此,转换为String的最简单方法是什么?

谢谢

My application uses an API that accepts only a Map<String, String>. Therefore, all objects must be converted to this form before they can be passed to the API.

This is quite trivial for simple classes like Boolean, BigDecimal etc. But it is proving somewhat problematic for converting a Calendar object.

I am not overly familiar with all the nuances of Calendars/Dates etc in Java so would appreciate some advice on converting my Calendar to a String and then back again.

When my application receives a request, it already has the Calendar constructed. It must then convert this to a String (1), pass it through the API (2), and then convert it back to a Calendar (3) on the other side.

So I am in control of 1 and 3, but not 2.

Based on this, what is the easiest way to convert to and then from a String?

Thanks

最满意答案

可能最好的办法是使用Calendar.getTimeInMillis() ,将其存储为该long值的String版本,然后将Calendar.getInstance().setTimeInMillis(Long.parseLong(Map.get(strIndex)))以获取日历回来。

Probably the best thing to do is use Calendar.getTimeInMillis(), store it as a String version of that long value, and then Calendar.getInstance().setTimeInMillis(Long.parseLong(Map.get(strIndex))) to get the Calendar back.

更多推荐

本文发布于:2023-07-27 08:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287743.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最简单   转换为   字符串   日历   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!