HttpClient DateUtils 的时区问题

问题:

  今天在做测试发现传入的时间为 "2018-11-26" 在格式化后变成了"20181125"

DateUtils.formatDate(c.getTime(), "yyyyMMdd")

  解析工具使用的是httpClient 4.5

想法:

  1、不应该啊,这应该是比较常见的API

  2、这种常见的时间解析问题出错,那么应该是时区设置错误

  3、debug到 工具类内部,发现

11 SimpleDateFormat format = formats.get(pattern); 22 if (format == null) { 33 format = new SimpleDateFormat(pattern, Locale.US); 44 format.setTimeZone(TimeZone.getTimeZone("GMT")); 55 formats.put(pattern, format); 66 }

  这使用了GMT 时区,和我们本地的时区差了8个小时;

  这个感觉设置得不是很友好,查看了下SimpleDateFormat 的初始化,感觉要人性化一点;

1 1 private void initializeCalendar(Locale loc) { 2 2 if (calendar == null) { 3 3 assert loc != null; 4 4 // The format object must be constructed using the symbols for this zone. 5 5 // However, the calendar should use the current default TimeZone. 6 6 // If this is not contained in the locale zone strings, then the zone 7 7 // will be formatted using generic GMT+/-H:MM nomenclature. 8 8 calendar = Calendar.getInstance(TimeZone.getDefault(), loc); 9 9 } 1010 }
点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java日期时间API系列31

  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前

​一篇文章总结一下Python库中关于时间的常见操作

前言本次来总结一下关于Python时间的相关操作,有一个有趣的问题。如果你的业务用不到时间相关的操作,你的业务基本上会一直用不到。但是如果你的业务一旦用到了时间操作,你就会发现,淦,到处都是时间操作。。。所以思来想去,还是总结一下吧,本次会采用类型注解方式。time包importtime时间戳从1970年1月1日00:00:00标准时区诞生到现在

Swagger如何测试Date类型参数

问题Swagger测试时,参数直接输入日期格式化后的类型,会报参数日期转换错误:ConversionFailedException解决网上说在参数上添加注解@DateTimeFormat(pattern“yyyyMMdd”)或者是添加@JsonFormat(pattern”yyyyMMddHH:mm:

HIVE 时间操作函数

日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec

Java日期时间API系列30

  实际使用中,经常需要使用不同精确度的Date,比如保留到天2020042300:00:00,保留到小时,保留到分钟,保留到秒等,常见的方法是通过格式化到指定精确度(比如:yyyyMMdd),然后再解析为Date。Java8中可以用更多的方法来实现这个需求,下面使用三种方法:使用Format方法、 使用Of方法和使用With方法,性能对比,使用

HttpClient DateUtils 的时区问题 - HelloWorld