简体   繁体   English

将任何字符串解析为Sql date

[英]Parse any string to Sql date

I wonder if it's possible to parse any string (at least to try) to sql Date without specifing the string format? 我想知道是否有可能在不指定字符串格式的情况下将任何字符串(至少尝试)解析为sql Date吗? In other words I want to make a generic method who take as input a string and return an sql Date. 换句话说,我想创建一个通用方法,将一个字符串作为输入并返回一个sql Date。

For instance I have: 例如,我有:

String date1="31/12/2099";
String date2="31-12-2099";

and call parseToSqlDate(date1) and parseToSqlDate(date2) which will returns sql dates. 并调用parseToSqlDate(date1)和parseToSqlDate(date2),这将返回sql日期。

Short answer: No 简短答案:否

Why: Parsing any string to a valid date is a task you as an intelligent being could not do (there is no "logical" way to determine the correct date), so you cannot "tell" a computer(program) to do that for you (see JGrice's comment, and there we still have 4-digit years). 原因:将任何字符串解析为有效日期是您作为聪明人无法完成的任务(没有“逻辑”方法来确定正确的日期),因此您无法“告诉”计算机(程序)来执行此操作您(请参阅JGrice的评论,那里还有4位数字的年份)。

Long answer: Maybe, if you are willed to either take risks or do not need a high rate of success. 长答案:也许,如果您愿意冒险或不需要很高的成功率。

How: 怎么样:

  1. Define your minimal (format) requirements of a date. 定义日期的最低要求(格式)。 Eg "a minimal date contains 1-8 numbers; 01/01/2001 , 01-01-01 , 01.01 (+current year) , 1.1 (+current year), 1 (+current month + current year) and/or "..contains 1-6 numbers and the letters for months"; 01-Jan-2001 and so on. 例如,“最小日期包含1-8个数字; 01/01/2001,01-01-01,01.01(+当年),1.1(+当年),1(+当月+当年)和/或” ..包含1-6个数字和几个月的字母”; 2001年1月1日,依此类推。

  2. Split the input along any non-number/non-month-name characters, with a regex like [^0-9a-zA-Z] (quick thought, may hold some pitfalls) 使用[^ 0-9a-zA-Z]之类的正则表达式将输入沿任何非数字/非月份名称字符进行分割(快速想到,可能会有一些陷阱)

  3. You now have 1 to 3 (actually more if eg the time is included) separate numbers + 1 month name which can be aligned for year/month/day any way you like 您现在拥有1到3个(如果包含例如时间,实际上更多)个单独的数字+ 1个月份的名称,可以按照自己喜欢的方式将其与年/月/日对齐

For this "alignment", there are several possibilities: 对于这种“对齐”,有几种可能性:

  • Try a fixed format at first, if it "fits", take it, else try another (or fail) 首先尝试固定格式,如果“合适”,则采用它,否则尝试另一种(或失败)
  • (only of you get more than one entry at a time) guess the format by assuming all entries have the same (eg any number block containing values > 12 is not a month and > 31 is not a day) (一次只有一个用户获得多个条目),通过假设所有条目都相同来猜测格式(例如,任何包含值> 12的数字块都不是一个月,而> 31则不是一天)

BUT , and this is a big one, you can expect any such method to become a major PITA at some point, because you can never fully "trust" it to guess correctly (you can never be sure to have missed some special format or introduced some ambiguous interpretation). 但是 ,这是一个很大的方法,您可以期望任何这样的方法在某个时候都会成为主要的PITA,因为您永远无法完全“信任”该方法以正确猜测(您永远不能确定错过了某些特殊格式或引入了这种格式)一些模棱两可的解释)。 I outlined some cases/format, but definitely not all of them, so you will refine that method very often if you actually use it. 我概述了一些案例/格式,但绝对不是全部,因此,如果您实际使用它,您将经常改进该方法。

Appendix to your comment: "May be to add another parameter and in this way to know where goes day , month and so on?" 您的评论的附录:“可能是添加另一个参数,并以此方式知道星期几,星期几等等?” So you are willed to add "pseudo-format-string" parameter specifying the order of day, month and year; 因此,您愿意添加“伪格式字符串”参数来指定日,月和年的顺序; that would make it a lot easier (as "simply" filtering out the delimiters can be achieved). 这将使其变得更加容易(因为可以“简单地”过滤出定界符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM