2010-10-05 155 views
1

我想格式化上午/下午格式的分钟/小时的endTime。有问题的地区似乎在早上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(); 
} 

回答

3

使用SimpleDateFormat与h:mm a怎么样?

如:

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

没有一个日期作为输入,只需在小时/分钟时间 – user339108 2010-10-05 08:15:47

+0

然后创建一个见。编辑。 – Joey 2010-10-05 08:18:06

+0

使用日历而不是日期构造日期 – user339108 2010-10-05 08:23:48

0

也许尝试(提供您的日期,而不是new Date()

public final String getEndTime() { 
    StringBuffer buf = new StringBuffer(); 
    new SimpleDateFormat("h:m a").format(new Date(), buf, 0); 
    return buf.toString(); 
} 
相关问题