java 如何创建自定义日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7018518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do i create a custom date?
提问by iCodeLikeImDrunk
need some help! this is sort of a 2 part question..
需要一些帮助!这是一个由两部分组成的问题。
ive done a decent amount of programming but ive never really worked with dates, ive googled and all that but finding nothing.
我做了大量的编程,但我从来没有真正处理过日期,我用谷歌搜索过,但什么也没找到。
part 1:
第1部分:
so lets say i have some variable: Public Date adStartTime
所以可以说我有一些变量:Public Date adStartTime
i want it to be in this format: yyyy/mm/dd hh:mm:ss so i just do adStartTime = (some date formatter) + (2011/08/08 08:08:08) *which dont work
我希望它采用这种格式:yyyy/mm/dd hh:mm:ss 所以我只做 adStartTime = (some date formatter) + (2011/08/08 08:08:08) *which don't work
whats the proper way to get it to be that way?? i keep getting errors and such.. id prefer to do everything in Date instead of using strings/int..
让它变成那样的正确方法是什么?我不断收到错误等..我更喜欢在Date中做所有事情而不是使用strings/int..
part 2:
第2部分:
once i get the date in that format i will need to insert that into db, can i insert the date in that format? or will i need to change it?
一旦我得到那种格式的日期,我就需要把它插入到数据库中,我可以用那种格式插入日期吗?还是我需要改变它?
reason i will need the time is because i am setting up automated ads for new company securities and sometimes the ad will run for one day or maybe 1/2 day so i will need to set the timer correctly.
我需要时间的原因是因为我正在为新公司证券设置自动广告,有时广告会运行一天或 1/2 天,因此我需要正确设置计时器。
thanks for any help..
谢谢你的帮助..
回答by Rasmus Kaj
For part 1, use a java.text.SimpleDateFormat
, like so:
对于第 1 部分,使用 a java.text.SimpleDateFormat
,如下所示:
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(datestring);
If you have code that looks anything like this, and still get errors, please edit your question to include your actual code.
如果您的代码看起来像这样,但仍然出现错误,请编辑您的问题以包含您的实际代码。
For part 2, you should insert the date in the database as a Date, not as a String, so there is no format.
对于第 2 部分,您应该将日期作为日期而不是字符串插入数据库中,因此没有格式。
回答by stimpy
1.) For working with dates I suggest you look at Joda time
1.) 对于日期,我建议您查看Joda 时间
which allows exactly that type of conversion .
这完全允许这种类型的转换。
for example
例如
public boolean isAfterPayDay(DateTime datetime) {
if (datetime.getMonthOfYear() == 2) { // February is month 2!!
return datetime.getDayOfMonth() > 26;
}
return datetime.getDayOfMonth() > 28;
}
2.) for storing dateTimes in a DB there are several camps which hold different things
2.) 为了在数据库中存储日期时间,有几个阵营可以容纳不同的东西
Some say you should store an int and convert . Joda makes that easy . Others ( like some oracle DBAs) seem to prefer a Date type. Joda handles that as well. There is a discussion of some of the issues here Date storage discussion
有人说你应该存储一个 int 和 convert 。Joda 让这一切变得简单。其他人(如某些 Oracle DBA)似乎更喜欢 Date 类型。Joda 也处理这个问题。这里有一些问题的讨论 日期存储讨论
How you actually do it can depend on your database type and the preferences of your DBA ( if you have one )
您实际如何操作取决于您的数据库类型和 DBA 的偏好(如果您有)
I would strongly suggest that you store datetime with a storage of Timezone .
我强烈建议您使用 Timezone 存储来存储日期时间。