分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

HttpClient DateUtils 的时区问题

发布时间:2023-09-06 02:24责任编辑:傅花花关键词:暂无标签

问题:

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

  

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

  

  解析工具使用的是httpClient 4.5

想法:

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

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

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

  

1 ????????????SimpleDateFormat format = formats.get(pattern);2 ????????????if (format == null) {3 ????????????????format = new SimpleDateFormat(pattern, Locale.US);4 ????????????????format.setTimeZone(TimeZone.getTimeZone("GMT"));5 ????????????????formats.put(pattern, format);6 ????????????}

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

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

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

HttpClient DateUtils 的时区问题

原文地址:https://www.cnblogs.com/souyoulang/p/10023757.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved