我的应用程序使用的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.
更多推荐
发布评论