JodaTime DateTime 和使用 AM/PM 处理区域设置
有人可以向我解释一下 DateTime 对于 AM/PM 的工作原理吗?
public DateTime(
int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond,
DateTimeZone zone) {
super(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, zone);
}
我知道我可以通过 DateFormat 和解析字符串来处理 AM/PM,但我对这种方法感兴趣。我无法弄清楚这一点。因为 DateTimeZone 不直接与 Locale 相关,所以 Chronology 也不直接相关。
我是否必须手动将基于 AM/PM 信息的 hourOfDay 更改为 24 小时类型?
我正在使用 UI html 表单小部件 >> http请求6个参数,基于Locale (y,m,d,h,m,AM/PM) / (y,m,d,h,m)>>现在我想通过 JodaTime 的 DateTime 对象从这 5/6 值创建 Date 对象。我是否可以手动将 (y,m,d,h,m,AM/PM) 转换为 (y,m,d,h,m) ?
手动我会这样做
public static Date getDateSinceUTC2(Target orderBean, TimeZone tz) {
int ap = orderBean.getDeadLineAmPm();
int year = orderBean.getDeadLineYear();
int month = orderBean.getDeadLineMonth();
int day = orderBean.getDeadLineDay();
int hour = orderBean.getDeadLineHour();
int minute = orderBean.getDeadLineMinute();
if (ap == 1) {
hour += 12;
}
month++;
DateTime dt = new DateTime(year, month, day, hour, minute, 0, 0, DateTimeZone.forID(tz.getID()));
return new Date(dt.getMillis());
}
Could please anybody explain to me, how DateTime works in regards to AM/PM ?
public DateTime(
int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond,
DateTimeZone zone) {
super(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, zone);
}
I know that I could handle AM/PM via DateFormat and parsing string, but I'm interested in this method. I can't figure this out. Because DateTimeZone doesn't relate directly to Locale, Chronology does not too.
Do I have to manually change the hourOfDay based on AM/PM information into the 24 hours type ?
I'm working with a UI html form widget >> http request 6 parameters, based on Locale (y,m,d,h,m,AM/PM) / (y,m,d,h,m) >> now I'd like to create Date object from these 5/6 values via JodaTime's DateTime object. Is it possible without me manually having to convert (y,m,d,h,m,AM/PM) to (y,m,d,h,m) ?
Manually I'd do that like this
public static Date getDateSinceUTC2(Target orderBean, TimeZone tz) {
int ap = orderBean.getDeadLineAmPm();
int year = orderBean.getDeadLineYear();
int month = orderBean.getDeadLineMonth();
int day = orderBean.getDeadLineDay();
int hour = orderBean.getDeadLineHour();
int minute = orderBean.getDeadLineMinute();
if (ap == 1) {
hour += 12;
}
month++;
DateTime dt = new DateTime(year, month, day, hour, minute, 0, 0, DateTimeZone.forID(tz.getID()));
return new Date(dt.getMillis());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我会开始使用
LocalDateTime
除非您实际上有一个时区。但是您可以通过给定任何小时来创建
DateTime
或LocalDateTime
,然后使用:我想我个人会 可能我自己会执行到“24 小时一天中的小时”的转换。
Firstly, I would start off using
LocalDateTime
unless you've actually got a time zone.But you could create either a
DateTime
or aLocalDateTime
by giving it any hour, and then using:I think I'd personally probably perform the conversion to "hour of 24-hour day" myself though.