如何使用SimpleDateFormat将String转换为Date?

Sha*_*mar 62 java datetime date-format simpledateformat

我有这段代码:

DateFormat formatter1;
formatter1 = new SimpleDateFormat("mm/DD/yyyy");
System.out.println((Date)formatter1.parse("08/16/2011"));
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我将其作为输出:

Sun Jan 16 00:10:00 IST 2011
Run Code Online (Sandbox Code Playgroud)

我期望:

Tue Aug 16 "Whatever Time" IST 2011
Run Code Online (Sandbox Code Playgroud)

我的意思是说我没有达到预期的月份.这是什么错误?

Boh*_*ian 91

试试这个:

new SimpleDateFormat("MM/dd/yyyy")
Run Code Online (Sandbox Code Playgroud)
  • MM是"月"(不是mm)
  • dd是"天"(不是DD)

这一切都在SimpleDateFormatjavadoc中


仅供参考,您的格式仍然是有效日期格式的原因是:

  • mm 是"分钟"
  • DD 是"一年一天"

此外,你不需要投地Date......它已经一个Date(或它爆炸):

public static void main(String[] args) throws ParseException {
    System.out.println(new SimpleDateFormat("MM/dd/yyyy").parse("08/16/2011"));
}
Run Code Online (Sandbox Code Playgroud)

输出:

Tue Aug 16 00:00:00 EST 2011
Run Code Online (Sandbox Code Playgroud)

瞧!

  • @Cricket将值成功解析为2月的第40天,只需在3月份滚动到一天以适应超出范围的值.为防止这种情况,请在格式对象上调用[`setLenient(false);`](http://docs.oracle.com/javase/6/docs/api/java/text/DateFormat.html#setLenient(boolean))解析之前(默认情况下,leniency为true). (4认同)

小智 45

m - min M - 月

Letter  Date or Time Component  Presentation    Examples
G       Era designator          Text                AD
y       Year                    Year                1996; 96
M       Month in year           Month               July; Jul; 07
w       Week in year            Number              27
W       Week in month           Number              2
D       Day in year             Number              189
d       Day in month            Number              10
F       Day of week in month    Number              2
E       Day in week             Text                Tuesday; Tue
a       Am/pm marker            Text                PM
H       Hour in day (0-23)      Number              0
k       Hour in day (1-24)      Number              24
K       Hour in am/pm (0-11)    Number              0
h       Hour in am/pm (1-12)    Number              12
m       Minute in hour          Number              30
s       Second in minute        Number              55
S       Millisecond             Number              978
z       Time zone               General time zone   Pacific Standard Time; PST; GMT-08:00
Z       Time zone               RFC 822 time zone   -0800 
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的表,在这个页面上真的很有帮助 (2认同)

Cha*_*ara 11

使用以下功能

/**
     * Format a time from a given format to given target format
     * 
     * @param inputFormat
     * @param inputTimeStamp
     * @param outputFormat
     * @return
     * @throws ParseException
     */
    private static String TimeStampConverter(final String inputFormat,
            String inputTimeStamp, final String outputFormat)
            throws ParseException {
        return new SimpleDateFormat(outputFormat).format(new SimpleDateFormat(
                inputFormat).parse(inputTimeStamp));
    }
Run Code Online (Sandbox Code Playgroud)

示例用法如下:

    try {
        String inputTimeStamp = "08/16/2011";

        final String inputFormat = "MM/dd/yyyy";
        final String outputFormat = "EEE MMM dd HH:mm:ss z yyyy";

        System.out.println(TimeStampConverter(inputFormat, inputTimeStamp,
                outputFormat));

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)


kan*_*arp 7

String newstr = "08/16/2011";
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format = new SimpleDateFormat("EE MMM dd hh:mm:ss z yyyy");
Calendar c = Calendar.getInstance();
c.setTime(format1.parse(newstr));
System.out.println(format.format(c.getTime()));
Run Code Online (Sandbox Code Playgroud)