“日期键”的日期为 Int
我想知道如何在 Java 中创建一个表达式,该表达式采用格式为“1999-12-30 12:34:45”的日期变量并将其转换为包含“19991230”的 Int。
这可能是其他人会欣赏的,尤其是提取和清理数据以加载到数据仓库中的人们。
我的猜测是需要更改日期模式,转换为字符串,截断第一部分(8 个字符)并转换为 int。
有想法吗?
(更新) 我不好解释这个问题。 生成的 int 将用作日期维度的键/Fk id。应该执行类似的操作来创建“时间维度”的键。 int 将减少存储这些 int 键的事实表中消耗的大小。查看相关答案:https://stackoverflow.com/a/8416548/1132571
I wonder how to go about creating an expression in Java that takes a date variable formatted as "1999-12-30 12:34:45" and converts it to an Int holding "19991230".
This is probably something others would appreciate, especially people extracting and cleaning up data for loading into a Data Warehouse.
My guess is one need to change date pattern, convert to string, truncate first part (8 chars) and convert to int.
Ideas?
(update)
My bad in explaining the issue.
The resulting int will service as a key/Fk id to a Date Dimension. A similar operation should be done for creating a key to a Time-of-Day Dimension. The int will reduce the size consumed in the facts table, in which these int keys would be stored. See related answer: https://stackoverflow.com/a/8416548/1132571
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用 SimpleDateFormat 类很容易做到这一点。然而,正如其他人提到的,您可能应该将纪元时间戳存储为数据库时间戳列,而不是将日期存储在 int 形式中。当你以后用 SQL 操作数据时,它会引起问题。
Pretty easy to do this using the SimpleDateFormat class. However, as others mentioned you probably should be storing the epoch timestamp as Database TimeStamp column instead of storing the date in an int form. It will cause problem later when you are manipulating the data with SQL.
只需删除特殊字符,然后
转换为 int
Integer.parseInt("1999-12-30 12:34:45".replaceAll("[- :]", ""));
根据您的情况 你只需要日期,而不是时间,所以它是:
Integer.parseInt("1999-12-30 12:34:45".substring(0,10).replaceAll("-", ""));
我应该提到,这只有在原始格式正确的情况下才有效......而且它实际上相当丑陋,我宁愿使用 long 和 getTime - 也许还有 simpleDateFormat。您真的不想丢弃有用的数据!
Simply remove special chars and then convert to int
Integer.parseInt("1999-12-30 12:34:45".replaceAll("[- :]", ""));
in your case you want only the date, not the time so it would be:
Integer.parseInt("1999-12-30 12:34:45".substring(0,10).replaceAll("-", ""));
I should mention that this only works if the original format is correct... and it is actually quite ugly, I would rather use a long and getTime - and maybe simpleDateFormat. You really do not want to throw away useful data!
getTime 会给你一个 long 的信息,int 不会足够大。
http://docs.oracle.com /javase/1.4.2/docs/api/java/util/Date.html#getTime()
getTime will give you a long with this information, int won't be large enough.
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Date.html#getTime()
虽然我同意评论者认为这很奇怪的观点,但如果我真的需要将日期转换为 long,我会使用
date.getTime()
它返回一个 long 表示自 01.01 以来的毫秒数.1970。好处是它可以很容易地转回日期(Java 有一个构造函数,我怀疑其他语言也这样做)
While I agree with the commenters finding this is strange, if I really needed to transform a date into a long I'd use the
date.getTime()
which returns a long representing the amount of millisec since 01.01.1970.The good stuff is that it can easily be turned back into a date (Java has a constructor for this, I suspect other languages do as well)
假设格式保持不变:
打印:19991230
Assuming that the format remains the same:
Prints: 19991230
你可以使用类似的东西:
然后你需要做的就是获取日期对象的值,如下所示:
这将为你提供自 1970 年以来的毫秒数,这是大多数计算机系统使用的通用时间系统。
转换回使用:
you could just use something like:
and then all you need to do is get the value of the date object, like so:
this will give you milliseconds since 1970, a universal time system which is used by most computer systems.
to convert back use:
使用 SimpleDateFormat、SimpleDateFormat.parse、SimpleDateFormat.format 和 Integer.parseInt
[ http://docs.oracle.com/javase/ 7/docs/api/java/text/SimpleDateFormat.html ]
1) 创建一个 SimpleDateFormat 将字符串解析为 Date 对象
2) 创建一个 SimpleDateFormat 将 Date 对象格式化为 in 的 String 表示形式
3)使用Integer.parseInt将新格式化的String更改为int。
然而,将日期存储为 int 进行仓储对我来说没有多大意义。
我猜想有一些限制阻止您使用 ISO 8601 格式的日期或字符串。
Use SimpleDateFormat, SimpleDateFormat.parse, SimpleDateFormat.format and Integer.parseInt
[ http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html ]
1) create a SimpleDateFormat to parse the string into a Date object
2) create a SimpleDateFormat to format the Date object into the String representation of the in
3) use Integer.parseInt to change the newly formatted String into an int.
However, storing the date as an int for warehousing does not make much sense to me.
I guess that there is some restriction preventing you using either Date or a String in ISO 8601 format.
还有另一个。 (我不喜欢解析字符串,日期太容易出错。;-))
And another one. (I don't like parsing strings, too error prone with dates. ;-))
感谢所有的建议、意见和建议。
这就是我正在寻找的:
其中
row.date
(来自 Talend Open Studio)保存我想要转换为 int 日期键的日期。Thanks for all suggestions, input and suggestions.
This was what I was looking for:
Where
row.date
(from Talend Open Studio) holds the date I wanted to transform into an int date key.在许多方面,您最好将日期键保留为日期,尽管您可能希望截断键的天数。如果您还需要实际的日期时间,请将其单独存储。造成这种情况的原因有很多,主要与日期算术有关。
计算事实表上的日期范围可以在不加入日期维度的情况下完成。
查询将返回日期 - 如果您只是在表上执行临时查询,这非常有效。
可以选择“之前”、“之后”、“错误”的特殊值,因此它们仍然相当明显(“1800-01-01”、“9000-01-01”、“6666-01-01”等)。
如果您不必涉及日期维度,则按日期或日期范围自联接会更高效。
最重要的是,日期算术可以直接在表上完成,而无需针对维度进行连接。计算“2012-01-15”之前 180 天的日期比计算 20120115 更容易。此外,SQL 没有从 20120015 到相应日期的本机转换,无需在维度中查找。
In many ways you're better off leaving date keys as dates, although you may wish to truncate at days for the key. If you need the actual date-time as well, store it separately. There are a number of reasons for this, mainly to do with date arithmetic.
Calculating date ranges on the fact table can be done without joining against the date dimension.
Queries will return the date - this is quite efficient if you're just doing an ad-hoc query on the table.
Special values for 'before', 'after', 'error' can be selected so they're still fairly obvious ('1800-01-01', '9000-01-01' '6666-01-01' etc.).
Self-joins by date or date range are more efficient if you don't have to involve a date dimension.
Most importantly, date arithmetic can be done directly on the table without having to join against the dimension. Calculating a date 180 days before '2012-01-15' is easier than doing it for 20120115. Also, SQL does not have a native conversion from 20120015 to the corresponding date without looking it up in a dimension.