DeepCode的热门发现#1:Java日期(这一点让我感到头晕)

Hey,
DeepCode offers an AI based Static Program Analysis for Java, Javascript and Typescript, and Python. You might know, DeepCode uses thousands of open source repos to train our engine. We asked the engine team to provide some stats on the findings. On the top suggestions from our engine, we want to introduce and give some background in this series of blog articles. And this one made me dizzy as we are looking back on like 70 years of software engineering and still have problems with — drumroll — date formats…

语言:爪哇 缺陷:日期数据格式(类别常规1) 诊断:日期时间输出格式不明确(例如12h输出,不带am / pm后缀)

Background:

快速,日期格式字符串有什么区别MM-DD-YYYY和mm-dd-yyyy要么MM-dd-yyyy?? 我可以告诉你,当您在适当的时候呼叫它们时,它们都会得到相同的结果。 但显然,它们根本不同。 好吧,让我们阐明一下……

Java had a bad start regarding its date classes. java.util.Date had serious design flaws when it was introduced which led to lots of confusion. A Date instance in Java is actually not a date but a moment in time, therefore (1) it has no time zone, (2) no format, (3) no calendar system. I suggest this valuable blog post for the full story. After years, the Java community acted and introduced new classes. Still, there are lots of traps (for example, java.text.SimpleDateFormat is not thread-safe while java.time.format.DateTimeFormatter is) but for now, let us focus the most common mistake which is the one flagged by DeepCode:

  • 使用毫米几个月和/或MM几分钟(错误!)。使用H对于“一天中的一小时”H原本是。H范围从0到23,而H ranges from 1 to 12 and is mostly used as single cHaracter. It needs tHe AM/PM information or it is ambiguous.使用YYYY for year. It is meant to be used in conjunction witH “week of tHe year” and can lead to unexpected results in tHe first and last week of a year. Normally, you want to use yyyy。使用DD for “day of tHe montH” but in reality, it means “day of tHe year”Make sure to use tHe correct pattern cHaracters. As a reference in Java, tHe following applies.
图案特征日期或时间部分结果示例G时代代号广告ÿ年2020(ÿÿÿÿ),20(ÿÿ)ÿw ^eeķ-ÿe一种r (ÿe一种r of tHe weeķ, 米一种ÿ provide ünexpected resülts first 一种nd l一种st weeķ of tHe ÿe一种r)2020(ÿÿÿÿ), 20(ÿÿ)中号中号ontH in ÿe一种rJülÿ(中号中号中号中号), Jül(中号中号中号), 07(中号中号)wResülts in weeķ in ÿe一种r16w ^每月第几周的结果3dGives tHe d一种ÿ coünt in tHe ÿe一种r266dd一种ÿ of tHe 米ontH09(dd),9(d)Fd一种ÿ of tHe weeķ in 米ontH4Ëd一种ÿ n一种米e in tHe weeķTüesd一种ÿ, Tüeüd一种ÿ nü米ber of weeķ wHere 1 represents 中号ond一种ÿ, 2 represents Tüesd一种ÿ 一种nd so on2一种A中号 or P中号 米一种rķerA中号HHoür in tHe d一种ÿ (0-23)12ķHoür in tHe d一种ÿ (1-24)23ķHoür in 一种米/p米 for 12 Hoür for米一种t (0-11)0HHoür in 一种米/p米 for 12 Hoür for米一种t (1-12)12米中号inüte in tHe Hoür59s小号econd in tHe 米inüte35小号中号illisecond in tHe 米inüte978žTi米ežoneP一种cific 小号t一种nd一种rd Ti米e; P小号T; G中号T-08:00žTi米ežone offset in Hoürs (RFC p一种ttern)-0800XTi米ežone offset in I小号O for米一种t-08; -0800; -08:00
注意:ThisisJava.Donotsimplyexpectthistobethesameelsewhere.Alwayscheckthedocumentation.

对于日期和时间周围的问题,这不是穷尽的。 我们可以谈谈UTC偏移量和时区之间的差异,时区缩写周围的问题(是BST英国夏令时还是英国标准时间,还是布干维尔标准时间(不,我没有做这个)?嗯,谁知道),或者 不同地区的不同日历。 为了提供上述小难题的答案,您可能几乎总是想要MM-dd-yyyy。

0xff

from: https://dev.to//deepcode/deepcode-s-top-findings-1-java-date-this-one-made-me-dizzy-33c0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值