![](/img/trans.png)
[英]Android parsing String to Date time with SimpleDateFormat
[英]Android parsing String to Date with SimpleDateFormat
我有String dateOrder ='2014-09-28',我想使用SimpleDateFormat更改格式。 但這無法格式化..如何解決?
這是我的代碼
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yy");
String dateOrder = fill_order_in.get(Variabel.KEY_DATE_ORDER);
try {
Date d = date.parse(dateOrder);
dateOrder = date.format(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
嘗試將第一行更改為
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
您可以將其用於parse
。 然后,您需要另一個SimpleDateFormat("dd/MM/yy")
來格式化新版本。
關鍵是解析需要使用一種模式來完成,而格式化則需要使用另一種模式來完成。 因此,您不僅需要創建一個SimpleDateFormat
實例,還需要兩種具有不同模式的模板:一種用於解析,另一種用於格式化。
我相信您想要這樣的東西:
//first create an object that will parse your date as you have it ie 2014-09-28
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
String dateOrder = fill_order_in.get(Variabel.KEY_DATE_ORDER);
try {
Date d = date.parse(dateOrder);
//then use a different object to format the date as you want it ie 9/28/2014
dateOrder = new SimpleDateFormat("dd/MM/yy").format(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.