2013-05-02 146 views
0

我想表明我的應用程序是這樣的當前日期: 星期四,2013年5月2日格式當前日期顯示星期

我已經有了下面的代碼獲取當前日期

Calendar c = Calendar.getInstance(); 

Time time = new Time(); 
time.set(c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH), 
      c.get(Calendar.YEAR)); 

如何將此Time對象格式化爲我需要的字符串?

+0

嘗試使用的SimpleDateFormat格式=新的SimpleDateFormat( 「YYYY-MM-DD」);有關值的詳細說明,請使用[此鏈接](http://download.java.net/jdk7/archive/b123/docs/api/java/util/Formatter.html) – 2013-05-02 11:47:52

回答

2

這你想要做什麼

Calendar cal = Calendar.getInstance(); 
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy"); 
String strDate = sdf.format(cal.getTime()); 
System.out.println("Current date in String Format: " + strDate); 

哪裏strDate可以顯示在你的TextView或任何

0

使用此

SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM dd,yyyy"); 
String formattedDate = formatter.format(new Date(System.currentTimeMillis()));    
Log.e("formattedDate",formattedDate); 
2

也許你可以使用它。

本示例在DateFormatSymbols()。DateFormatSymbols類的getWeekdays()方法的幫助下以短格式顯示星期幾的名稱。

import java.text.*; 
import java.util.*; 
public class Main { 
    public static void main(String[] args) { 
     Date dt = new Date(1000000000000L); 

     DateFormat[] dtformat = new DateFormat[6]; 
     dtformat[0] = DateFormat.getInstance(); 
     dtformat[1] = DateFormat.getDateInstance(); 
     dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM); 
     dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL); 
     dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG); 
     dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT); 

     for(DateFormat dateform : dtformat) 
     System.out.println(dateform.format(dt)); 
    } 
} 

輸出:

9/9/01 7:16 AM 
Sep 9, 2001 
Sep 9, 2001 
Sunday, September 9, 2001 
September 9, 2001 
9/9/01 

Source

0
SimpleDateFormat dateformat= new SimpleDateFormat("dd,MM,yyyy"); 

String strdate = dateformat.format(new Date(System.currentTimeMillis())); 

哎呀,我有點慢。

0

我會建議使用java.text.SimpleDateFormat來代替。

public static void main(String[] args) { 
    Date date=new Date(); 
    String format = new SimpleDateFormat("EEE,MMM d,yyyy ").format(date); 
    System.out.println(format); 
}