JodaTime DateTime and handling of Locales with AM/PM
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, dayO开发者_运维问答fMonth,
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());
}
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.
精彩评论