今年你因为 YYYY-MM-dd 被锤了吗?

文章通过一个单元测试展示了在使用SimpleDateFormat时,YYYY-MM-dd格式化日期可能导致的问题。当日期为周日且跨年时,YYYY会根据周所属的年份来确定年份,导致年份错误。正确格式应使用yyyy-MM-dd以避免此问题。作者提醒开发者自查代码并分享文章以帮助遇到同样问题的同行。

不过再想想,其实也正常,每一年都有新人,即使是老人,也可能因为跳槽接手已经存在问题的系统,当然也可能没关注DD,没看过之前的推送 图片 ?不管什么原因,也许现在还有其他小伙伴正在修Bug的路上,所以,赶紧起床推一篇,聊聊这个问题吧。

如果你正好碰到的,可以看看了解下原因,做好相应的处理;如果还没碰到的,最好也自查一下。如果你已经很了解这个,那么把这篇文章分享到朋友圈,也提醒下身边其他的同行朋友们吧,也许会炸出几个正在修Bug路上的兄弟 图片

事故现场

我们先来写个单元测试,重现一下这个问题。

测试逻辑:

1、创建两个日期格式化:

  • 一个是存在问题的YYYY-MM-dd
  • 另一个是正确用法yyyy-MM-dd

2、分别去格式化两个不同的日期:2021年12月25日(周六),2020年12月26日(周日) 具体代码如下:

public class Tests { 

    @Test
    public void test() throws Exception {
        SimpleDateFormat df1 = new SimpleDateFormat("YYYY-MM-dd");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, 2021);
        c.set(Calendar.MONTH, 11);

        // 2021年12月25日周六
        c.set(Calendar.DATE, 25);
        System.out.println("YYYY-MM-dd = " + df1.format(c.getTime()));
        System.out.println("yyyy-MM-dd = " + df2.format(c.getTime()));

        // 分割线
        System.out.println("========================");

        // 2021年12月26日 周日
        c.set(Calendar.DATE, 26);
        System.out.println("YYYY-MM-dd = " + df1.format(c.getTime()));
        System.out.println("yyyy-MM-dd = " + df2.format(c.getTime()));
    }

}
复制代码

跑一下测试,可以看到输出结果如下:

YYYY-MM-dd = 2021-12-25
yyyy-MM-dd = 2021-12-25
========================
YYYY-MM-dd = 2022-12-26
yyyy-MM-dd = 2021-12-26
复制代码

2021年12月25日(周六),两种格式化都正确 2021年12月26日(周日),YYYY-MM-dd出了问题,年份到了2022年

问题原因

为什么YYYY-MM-dd格式化2021年12月26日的时候,会到2022年呢?

因为YYYY是week-based-year,表示:当天所在的周属于的年份,一周从周日开始,周六结束,只要本周跨年,那么这周就算入下一年。

所以2021年12月26日那天在这种表述方式下就已经到 2022年了。

而当使用yyyy的时候,就还是 2021 年。

好了,最后把这篇文章分享到朋友圈,提醒下身边其他的小伙伴吧,看看能炸出几个今天再修Bug路上的兄弟 图片

最后的最后,平时喜欢讨论技术的小伙伴也可以加入我们的高质量技术交流群,与优秀的人在一起,自己也会优秀起来

DD正在掘金打榜,如果我的分享曾经帮到过您,可以过来给我投票支持下。与其他博主不同,DD这边老规矩,任何活动打榜奖品DD都不要,最后会送给支持者! 。感兴趣的小伙伴 点击参与活动

我的公众号:程序猿DD,专注分享行业最新消息和前沿技术资讯,关注我!第一时间获取最新前沿,积累技术人弯道超车的资本

作者:程序猿DD
原文链接:https://juejin.cn/post/7045824361881665549

 

@RequestMapping("/qhexcel") @ResponseBody public String qhexcel() { String s3 = null; try { Map<String, String> headers00 = new HashMap<>(); headers00.put("Authorization", token); /** * 强夯-设计文件 * http://platform.unitopmc.com:10032/a/pi/spacetime/basic/constructiontar * getobject?craftType=0&constructionType=8&is3DObj=false&status=0&pageIndex=1&pageSize=10 */ String targetUrl3 = "http://platform.unitopmc.com:10032/api/spacetime/basic/constructiontargetobject?" + "workSectionId=445f0c57&craftType=0&constructionType=8&is3DObj=false&status=0&pageIndex=1&pageSize=100000"; String s = HttpUtils.get(targetUrl3, null, headers00); JSONObject objects = JSON.parseObject(s); JSONArray data = objects.getJSONArray("data"); int i = 0; ArrayList<String> arrayList = new ArrayList<>(); for (Object datum : data) { arrayList.clear(); i++; System.out.println(i); if (i<6){ continue; } JSONObject j = JSON.parseObject(datum.toString()); String name = j.getString("name"); String guid = j.getString("guid"); System.out.print("设计文件Id:" + guid + "---"); System.out.println("设计文件名称:" + name); String workSectionId = j.getString("workSectionId"); //工区id // String urldowm = "https://platform.unitopmc.com/api/spacetime/penning/penningProcessData/ExportExcel?" + // "isExportExcel=true&pageSize=0" + // "&pageIndex=0&constructionObjGUID="+guid+"&ownerSectionId="+workSectionId+"&startDataTime=0&endDataTime=0"; // Map<String, String> headers111 = new HashMap<>(); // headers111.put("Authorization", token); // String s1111 = HttpUtils.get(urldowm, null, headers111); // if (true){ // continue; // } /** * 获取过程数据 */ String url = "http://platform.unitopmc.com:10032/api/spacetime/penning" + "/penningProcessData/ExportExcel?isExportExcel=false&ownerSectionId=" + workSectionId + "&constructionObjGUID=" + guid + "&pageSize=5000000&pageIndex=1&parertId=445f0c57&projectionId=cd19e7928853460b824d083ea461b758"; Map<String, String> headers = new HashMap<>(); headers.put("Authorization", token); String s1 = HttpUtils.get(url, null, headers); JSONObject objects001 = JSON.parseObject(s1); JSONArray data1 = objects001.getJSONArray("data"); JSONArray sortedData = null; //---------------------------------创建excel---------------- Workbook workbook = new XSSFWorkbook(); // 创建Workbook Sheet sheet = workbook.createSheet(name); // 创建Sheet // 创建表头 /** * 点名 H坐标 P坐标 机械编号 施工时间 夯击能 落距 */ Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("设计文件"); headerRow.createCell(1).setCellValue("夯点名称"); headerRow.createCell(2).setCellValue("机械名称"); headerRow.createCell(3).setCellValue("X坐标"); headerRow.createCell(4).setCellValue("Y坐标"); headerRow.createCell(5).setCellValue("经度"); headerRow.createCell(6).setCellValue("纬度"); headerRow.createCell(7).setCellValue("高程"); headerRow.createCell(8).setCellValue("夯击次数"); headerRow.createCell(9).setCellValue("夯重量"); headerRow.createCell(10).setCellValue("落距(m)"); headerRow.createCell(11).setCellValue("夯击能(kn.m)"); headerRow.createCell(12).setCellValue("每次夯沉量(m)"); headerRow.createCell(13).setCellValue("总夯沉量(m)"); headerRow.createCell(14).setCellValue("时间"); int iexcel = 0; //------------------------------------------------- if (data1.size() > 1) { for (Object o : data1) { String s2 = o.toString(); JSONObject jsonObject = JSONObject.parseObject(s2); String constructionObjName = jsonObject.getString("constructionObjName"); String holeId = jsonObject.getString("holeId"); String machineName = jsonObject.getString("machineName"); String x = jsonObject.getString("x");//高程 String y = jsonObject.getString("y");//高程 String longitude = jsonObject.getString("longitude");//高程 String latitude = jsonObject.getString("latitude");//高程 String z = jsonObject.getString("z");//高程 String tampingNumber = jsonObject.getString("tampingNumber");//高程 String ramsWeight = jsonObject.getString("ramsWeight");//高程 String fallingDistance = jsonObject.getString("fallingDistance");//高程 String tampingEnergy = jsonObject.getString("tampingEnergy");//高程 String singleRammingLoad = jsonObject.getString("singleRammingLoad");//高程 String totalDistanceBias = jsonObject.getString("totalDistanceBias");//高程 DecimalFormat df = new DecimalFormat("#.##"); String passTime = jsonObject.getString("passTime");//日期时间 时间戳 long unixTimeStamp = Long.parseLong(passTime); // 1714264320685 是你的时间戳 // 转换为 LocalDateTime LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(unixTimeStamp), ZoneId.systemDefault()); // 格式化日期时间 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String sinkStartTime = dateTime.format(formatter); //---------------------------------写入excel---------------- Row row = sheet.createRow(iexcel + 1); // 从第二行开始写数据 if (iexcel % 200 == 0) { System.out.println("运行到第"+iexcel+1 + "行"); } row.createCell(0).setCellValue(constructionObjName); row.createCell(1).setCellValue(holeId); row.createCell(2).setCellValue(machineName); row.createCell(3).setCellValue(x); row.createCell(4).setCellValue(y); row.createCell(5).setCellValue(longitude); row.createCell(6).setCellValue(latitude); row.createCell(7).setCellValue(z); row.createCell(8).setCellValue(tampingNumber); row.createCell(9).setCellValue(ramsWeight); row.createCell(10).setCellValue(fallingDistance); row.createCell(11).setCellValue(tampingEnergy); row.createCell(12).setCellValue(singleRammingLoad); row.createCell(13).setCellValue(totalDistanceBias); row.createCell(14).setCellValue(sinkStartTime); iexcel++; // } /** * 所有设计文件 * e484d6ae 一工区 * fac03108 二工区 * 4b65f917 三工区 */ if (workSectionId == "e484d6ae"){ // 写入文件 try (FileOutputStream fileOut = new FileOutputStream("一工区\\"+name + ".xlsx")) { workbook.write(fileOut); } catch (IOException e) { e.printStackTrace(); } }else if (workSectionId == "fac03108"){ // 写入文件 try (FileOutputStream fileOut = new FileOutputStream("二工区\\"+name + ".xlsx")) { workbook.write(fileOut); } catch (IOException e) { e.printStackTrace(); } }else { // 写入文件 try (FileOutputStream fileOut = new FileOutputStream("三工区\\"+name + ".xlsx")) { workbook.write(fileOut); } catch (IOException e) { e.printStackTrace(); } } //---------------------------------创建excel---------------- } try { workbook.close(); // 关闭Workbook释放资源 } catch (IOException e) { e.printStackTrace(); } } } } catch (Exception e) { //结束此单元id的所有派遣 e.printStackTrace(); } return s3; }请改进上述程序,目前当 数据量很大的时候存储excel会非常慢
08-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值