我想这样使用Android SimpleDateFormat

 String _Date = "2010-09-29 08:45:22" SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = fmt.parse(_Date); return fmt.format(date); } catch(ParseException pe) { return "Date"; } 






Solutions Collecting From Web of "Android的SimpleDateFormat,如何使用它?"


SimpleDateFormat可用于parsing和格式化。 您只需要两种格式,一种parsingstring,另一种则返回所需的打印输出:

 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date date = fmt.parse(dateString); SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy"); return fmtOut.format(date); 


 Symbol Meaning Kind Example D day in year Number 189 E day of week Text E/EE/EEE:Tue, EEEE:Tuesday, EEEEE:T F day of week in month Number 2 (2nd Wed in July) G era designator Text AD H hour in day (0-23) Number 0 K hour in am/pm (0-11) Number 0 L stand-alone month Text L:1 LL:01 LLL:Jan LLLL:January LLLLL:J M month in year Text M:1 MM:01 MMM:Jan MMMM:January MMMMM:J S fractional seconds Number 978 W week in month Number 2 Z time zone (RFC 822) Time Zone Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00 a am/pm marker Text PM c stand-alone day of week Text c/cc/ccc:Tue, cccc:Tuesday, ccccc:T d day in month Number 10 h hour in am/pm (1-12) Number 12 k hour in day (1-24) Number 24 m minute in hour Number 30 s second in minute Number 55 w week in year Number 27 G era designator Text AD y year Number yy:10 y/yyy/yyyy:2010 z time zone Time Zone z/zz/zzz:PST zzzz:Pacific Standard 



  Date date = Calendar.getInstance().getTime(); // // Display a date in day, month, year format // DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); String today = formatter.format(date); System.out.println("Today : " + today); 
 String _Date = "2010-09-29 08:45:22" SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat fmt2 = new SimpleDateFormat("dd-MM-yyyy"); try { Date date = fmt.parse(_Date); return fmt2.format(date); } catch(ParseException pe) { return "Date"; } 



 @SuppressLint("SimpleDateFormat") private void setTheDate() { long msTime = System.currentTimeMillis(); Date curDateTime = new Date(msTime); SimpleDateFormat formatter = new SimpleDateFormat("MM'/'dd'/'y hh:mm"); curDate = formatter.format(curDateTime); mDateText.setText("" + curDate); } 

这花了很多努力。 我做了很多打击和试用,最后我得到了解决scheme。 我曾用“MMM”表示月份:JAN