Android / java时间格式?

时间:2012-01-02 10:11:57

标签: java android simpledateformat

我有时间作为“2011-12-03 12:00:19”如何在“2011年12月2日星期五”转换它,我知道这个http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html,但是给了我错误:

 Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
    at java.text.DateFormat.format(Unknown Source)
    at java.text.Format.format(Unknown Source)
    at com.timestamp.NewTimeStamp.<init>(NewTimeStamp.java:21)
    at com.timestamp.NewTimeStamp.main(NewTimeStamp.java:35)

我的代码是::

String mytime ="2011-12-03 12:00:19";
String pattern = "EEE d MMMMM yyyy";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);

        Date date = new Date(mytime);
        String time = dateFormat.format(date);

        System.out.println("=== > " + time);

1 个答案:

答案 0 :(得分:7)

将输入转换为Date,然后格式化。

        String mytime ="2011-12-03 12:00:19";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
        Date myDate = dateFormat.parse(mytime);
        System.out.println("=== > " + myDate);
        SimpleDateFormat timeFormat = new SimpleDateFormat("EEE d MMMMM yyyy");
        String time = timeFormat.format(myDate);
        System.out.println("=== > " + time);

输出:

D:\Work\Stand alone Java classes>javac Test2.java && java Test2
=== > Wed Jan 12 12:00:19 IST 2011
=== > Wed 12 January 2011