JodaTime DateTime 和使用 AM/PM 处理区域设置

发布于 2024-11-30 08:10:45 字数 1344 浏览 1 评论 0原文

有人可以向我解释一下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ζ澈沫 2024-12-07 08:10:45

首先,我会开始使用 LocalDateTime 除非您实际上有一个时区。

但是您可以通过给定任何小时来创建DateTimeLocalDateTime,然后使用:

// I'm assuming amPm is 0 for am and 1 for pm
result = original.withField(DateTimeFieldType.hourOfHalfDay(), hour)
                 .withField(DateTimeFieldType.halfDayOfDay(), amPm);

我想我个人会 可能我自己会执行到“24 小时一天中的小时”的转换。

Firstly, I would start off using LocalDateTime unless you've actually got a time zone.

But you could create either a DateTime or a LocalDateTime by giving it any hour, and then using:

// I'm assuming amPm is 0 for am and 1 for pm
result = original.withField(DateTimeFieldType.hourOfHalfDay(), hour)
                 .withField(DateTimeFieldType.halfDayOfDay(), amPm);

I think I'd personally probably perform the conversion to "hour of 24-hour day" myself though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文