Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在将 a 转换为Calendar格式为 hh:mm+-UTCoffset 的字符串,例如13:37-0500. 如何格式化时区限制为 2 位数的日历?(此外:4位数字是什么?时区差异不总是2位数字吗?)
Calendar
13:37-0500
要截断最后两位数字,这是我目前的方法:
DateFormatUtils.format(date, "HH:mmZ").substring(0, 8);
但这不能做得更优雅吗?我认为子串不是在这里做的正确的事情......
使用Xinsted 的Z。看示例代码:
X
Z
SimpleDateFormat sdf=new SimpleDateFormat("HH:mmX"); Date date=new Date(); System.out.println(sdf.format(date));
输出:18:44+06
此答案仅适用于您的第二个问题:
4位数字有什么用?时区差异不总是2位数吗?
至少有一个时区,即伊朗使用的中东时间,定义为 GMT +3.5,因此只有两位数字不适合该时区。