我的要求是以 hh:mm 格式在网页上显示日期。但是,如果它是一位数字,则在小时值之前不应显示零。
例如 - 应显示为:上午 11:30、上午 9:15、下午 1:00。
我试图解决这个问题,但这里唯一的问题是从 1 位数小时值中删除多余的 0。
输入时间的格式为 - hh:mm:ss
。
日期值最初是一个字符串。首先将其解析为以下格式的日期 -
final SimpleDateFormat dfParse = new SimpleDateFormat("HH:mm:ss");
startTimeFmt = dfParse.parse(startTime);
然后将其格式化为以下格式 -
final SimpleDateFormat dfFormat = new SimpleDateFormat("hh:mm a");
startTime = dfFormat.format(startTimeFmt);
最佳答案
试试这个:
Date date = new SimpleDateFormat("hh:mm:ss").parse("07:15:45");
String newDate = new SimpleDateFormat("h:mm a").format(date);
这将打印 7:15 AM
关于java - java中使用SimpleDateFormat进行时间格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807839/