我正在尝试将以分钟/小时提供的endTime格式化为AM / PM格式。问题区域似乎在早上00.00-1.00和下午12.00-1.00之间。不确定这是正确的方法,还是有更简单的方法来实现?

public final String getEndTime() {
    StringBuffer buf = new StringBuffer();
    buf.append(this.getEndTimeInHrs() < 13 ? this.getEndTimeInHrs() : this
            .getEndTimeInHrs() - 13).append(COLON);

    // this check is needed to prefix an additional string in front of the
    // minute (e.g. for 6 hrs 5 minutes, it will be displayed as 6:05)
    if (this.getEndTimeInMinutes() < 10) {
        buf.append(0);
    }
    buf.append(this.getEndTimeInMinutes());

    if (getEndTimeInHrs() < 12) {
        buf.append(SPACE).append(AM);
    } else if (getEndTimeInHrs() > 12) {
        buf.append(SPACE).append(PM);
    }

    return buf.toString();
}

最佳答案

将SimpleDateFormat与h:mm a一起使用怎么办?

例如。:

DateFormat df = new SimpleDateFormat("h:mm a");
Date date = new Date(0, 0, 0, getHours(), getMinutes());
return df.format(date);

09-09 22:28
查看更多