开发者

How to convert 12/31/2008 3:28p using Java's SimpleDateFormat

I'm using a java based tool and there's a config option for a dateformat which I've set to "MM/dd/yy hh:mma" but it's apparently expected 'am' or 'pm', not 'a' or 'p' because the input dates aren't being recognized. e.g. 12/31/2008 3:2开发者_如何学编程8p

Is there such a SimpleDateFormat specification for this particular format? (Assumption on my part that this Java method is being utilized.) I'll ask the tool vendor next but SO seems to be 'the' place to go!

TIA


you can do this

public static void main(String[] args) throws ParseException {
    DateFormatSymbols instance = DateFormatSymbols.getInstance();
    instance.setAmPmStrings(new String[]{"a","p"});
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yy hh:mma",instance);
    Date parsedDate = format.parse("12/31/2008 3:28p");
    System.out.println(parsedDate);

}


This is not the answer you want to hear, but I'm afraid, if you are only allowed to enter the dateformat, there's nothing much you can do about it, unless you can somehow append a 'M'. Based on the example in the javadoc for SimpleDateFormat, a single 'a' accepts either AM or PM.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜