- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在使用 SimpleDateFormat
将字符串解析为日期时遇到了一个非常奇怪的行为。考虑以下单元测试:
@Test
public void testParse() throws ParseException
{
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String dateStr = "2012-12-21";
Date parsedDate = dateFormat.parse(dateStr);
Calendar date = Calendar.getInstance();
date.setTime(parsedDate);
Assert.assertEquals(2012, date.get(Calendar.YEAR));
Assert.assertEquals(11, date.get(Calendar.MONTH)); // yeah, Calendar sucks
Assert.assertEquals(21, date.get(Calendar.DAY_OF_MONTH));
}
可以看出,上面的代码中存在一个故意的错误:SimpleDateFormat
是用"yyyyMMdd"
初始化的,但是字符串要解析的格式为 "yyyy-MM-dd"
。我希望这样的事情会导致 ParseException
,或者至少在尽力而为的基础上正确 进行解析。相反,由于某些奇怪的原因,日期被解析为 2011-11-02
。嗯?!
这是 Not Acceptable ,因为处理输入时的一个错误会导致完全出乎意料/毁灭性的结果。同时切换到 JodaTime,但如果能了解那里出了什么问题就好了。
最佳答案
public void setLenient(boolean lenient)
Specify whether or not date/time parsing is to be lenient. With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format.
With strict parsing, inputs must match this object's format.
如果你将它设置为false,你会得到ParseException
关于java - java.text.SimpleDateFormat 中的奇怪行为期望 yyyyMMdd 给定 yyyy-MM-dd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6859211/
我正在尝试使用这两种格式解析日期 2014-12-03T10:05:59.5646+08:00: yyyy-MM-dd'T'HH:mm:ss yyyy-MM-dd'T'HH:mm:ssXXX 当我使用
Paypal 返回以下格式的时间戳: yyyy-MM-ddTHH:mm:ssZ 我不知道该怎么办... 如何在 php 中使用本地时区将其转换为 yyyy-MM-dd HH:mm:ss? 我很想pre
我正在使用 Excel 2010 或 Excel 2007 导入包含日期/时间信息的 CSV 文件。我的 CSV 文件中的时间戳具有以下格式:yyyy-mm-dd hh:mm:ss。 (例如:2015
这个问题已经有答案了: Separate Date and Time objects (2 个回答) 已关闭 4 年前。 如何从 SimpleDateFormat("MM/dd/yyyy kk:mm"
这个问题已经有答案了: Java string to date conversion (17 个回答) 已关闭 6 年前。 我需要将日期字符串转换为另一种特定格式。 例如:我有一个日期,可以是 YYY
我想将字符串:24/11/2016 04:30 pm 转换为日期时间值:11/24/2016 04:30 pm。 我的代码为: DateTime date = DateTime.ParseExact(
我想使用 linux 将像“26/11/05 06:00:01,057000000”这样的纪元转换为 yyyy-mm-ddThh:mm:ss? 我曾尝试使用以下脚本但没有成功: echo 26/11/
这个问题在这里已经有了答案: mysql YYYY-MM-DDThh:mm:ss (1 个回答) 关闭 6 年前。 我想上传包含 yyyy-mm-ddThh:mm:ss.sssZ 数据的 csv 文
我在“dd-MM-yyyy HH:mm”中有一个字符串,需要将其转换为格式为日期的对象“yyyy-MM-dd HH:mm”。 下面是我用来转换的代码 oldScheduledDate = "16-05
我有一个数据框(df),它有一个日期列(列名:sale_date),它以以下格式存储数据 dd/mm/yy hh:mm:ss 我正在尝试将其转换为 yyyy-mm-dd hh:mm:ss。尝试了以下但
我的数据库中有日期时间列(格式为 YYYY-mm-dd hh:mm:ss)。 我需要将其转换为 dd-mm-YYYY hh:mm:ss 格式。 我该怎么办?帮助我。 最佳答案 不确定如何在 javas
想知道它们是否代表不同的格式或本质上相同(只是新与旧的演示文稿)。 最佳答案 基于DateTimeFormatter : Offset X and x: This formats the offset
如标题所示,我有一个问题。我需要将 LocalDataTime yyyy-MM-ssThh-mm-ss 解析为 LocalDataTime yyyy-MM-ss hh-mm-ss 但是当我这样做时 S
我想用 mySQL 将我的数据从“yyyy-mm-ddThh-mm-ss.sssZ”转换为“yyyy-mm-dd hh-mm-ss”。 我尝试使用 convert_tz: mysql> SELECT
这个问题在这里已经有了答案: LOAD DATA INFILE easily convert YYYYMMDD to YYYY-MM-DD? (1 个回答) 关闭 6 年前。 我正在尝试将 CSV
我需要更改 string 的日期格式。原始字符串的格式如下: var timeStamp = '2014/07/30 - 14:15:36' 这是我想要实现的日期格式: var timeStampAr
我正在尝试将 yyyy-MM-dd'T'HH:mm:ss.SSSz 格式的日期格式化为 yyyy-mm-dd HH:mm:ss,这应该很容易,但我无法获得它可以工作。 需要解析的日期格式为:2012-
我正在尝试通过传递时间戳作为命令行参数来使用 Synapse 管道运行 Spark 作业。在与 Spark 作业相关的代码运行之前,synapse 正在将字符串命令行参数值从 ISO 格式 2019-
我试过下面的代码: String created_Date = "25-Nov-15 14:23:34"; SimpleDateFormat sdf = new SimpleDateFormat("d
我创建了一个函数,它以与原始格式不同的格式返回日期。基本上,我正在使用此 Select MonthSub('2014-04-10',2)# 语句进行测试,它应该返回2014-02,而不是 2014-0
我是一名优秀的程序员,十分优秀!