Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

java - Month issue in SimpleDateFormat class

String expiryDate = "2016-03-14";
private String formatDateTime(String expiryDate, String expiryTime) {
    SimpleDateFormat input = new SimpleDateFormat("yyyy-mm-dd");
    SimpleDateFormat output = new SimpleDateFormat("MMM, dd, yyyy");
    try {
        String formattedDate = output.format(input.parse(expiryDate ));// parse input
        return  formattedDate + ", " + expiryTime;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

However when it returns it gives Jan, 14, 2016.

The desired output was Mar,14,2016

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

mm should be capital MM. Beacuse

mm represent minute MM represent Month

So your format should be like

SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...