我在我的應用程序中使用數據庫。更改格式字符串android
我有datepickers在那裏我選擇日期作爲一個字符串存儲並保存爲「yyyy-MM-dd」。我有一個TextView,顯示所選數據,但我想將其顯示爲「dd.MM.yyyy」。我怎樣才能做到這一點?
例如:
爲2017年5月8日轉換爲2017年5月8日
我在我的應用程序中使用數據庫。更改格式字符串android
我有datepickers在那裏我選擇日期作爲一個字符串存儲並保存爲「yyyy-MM-dd」。我有一個TextView,顯示所選數據,但我想將其顯示爲「dd.MM.yyyy」。我怎樣才能做到這一點?
例如:
爲2017年5月8日轉換爲2017年5月8日
String mainString="2017-05-08";
String[] separated = CurrentString.split("-");
現在你的新路線是這樣的:
String mainString="2017-05-08";
String[] separated = mainString.split("-");
String newString="";
for(int index=separated.length-1;index>=0;index--){
newString=newString+""+separated[index]+".";
}
newString=newString.split(0,newString.length()-1);
下面是示例輸出08.05.2017
我得到一個OutOfBoundsException當我做第三行代碼你給我.. – David
請檢查。我更新了我的答案。 –
試試這個
String date = getConvertDate("yyyy-MM-dd","dd.MM.yyyy","2017-05-08");
getConvertDate方法
public static String getConvertDate(String sourceFormat, String destFormat, String strDate) {
String finalDate = "";
try {
DateFormat srcDf = new SimpleDateFormat(sourceFormat);
// parse the date string into Date object
Date date = srcDf.parse(strDate);
DateFormat destDf = new SimpleDateFormat(destFormat);
// format the date into another format
finalDate = destDf.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return finalDate;
}
您可以替換 「 - 」至 」。」如果你需要在一個單一的TextView像設置,
String newDate = oldDateString.replace("-", ".");
要更改順序,您可以使用拆分選項,
String[] dates = oldDateString.split("-"); // here, dates[0] is year, dates[1] is month and dates[2] is day
現在,
String newDate = dates[2] + "." + dates[1] + "." + dates[0]
或者更改格式,
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
try {
Date date = formatter.parse(oldDateString);
newDate = formatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
以及如何更改訂單?因爲這一天必須在現在開始,而不是在最後 – David
DateFormat srcFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dstFormat = new SimpleDateFormat("dd.MM.yyyy");
Date date = srcFormat.parse("2017-05-08");
String result = dstFormat.format(date);
試試這個
public static String getFormattedDate(String date){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Date date1 = null;
Calendar temp = Calendar.getInstance();
try {
date1 = dateFormat.parse(date);
temp.setTime(date1);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat newFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
return newFormat.format(date1);
}
嘗試
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2017-05-08");
textView.setText(new SimpleDateFormat("dd.MM.yyyy").format(date));
你可以試試這個代碼
String date = null;
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat d = new SimpleDateFormat("dd.MM.yyyy");
try {
Date convertedDate = inputFormat.parse(selectdate);
date = d.format(convertedDate);
Log.e("Check Value", "Check Value" + date);
} catch (ParseException e) {
e.printStackTrace();
}
看看這個代碼,並在任何地方重複使用它:
DateConv.java
public static String convertIntoDateFormat(String aSelectedDate, String aConversionFormat, String aCurrentFormat) {
if(aSelectedDate.length() == 0) return "";
String aDate = "";
try {
DateFormat curFormater = new SimpleDateFormat (aCurrentFormat);
Date dateObj = null;
try {
dateObj = curFormater.parse (aSelectedDate.toString());
} catch(ParseException e) {
e.printStackTrace();
Log.e (TAG, e.getMessage().toString());
}
DateFormat postFormater = new SimpleDateFormat (aConversionFormat);
aDate = postFormater.format (dateObj);
} catch(Exception e) {
e.printStackTrace();
}
return aDate;
}
通過你當前的日期格式和所需的日期格式在這裏:
aCurrentDate = DateConv.convertIntoDateFormat(aData[i], "yyyy-MM-dd", "dd MMMM yyyy");
您可以輕鬆地顯示日期格式爲喲你描述過。遵循以下兩個步驟:
1)將字符串日期轉換爲日期變量,方法是將其解析爲「yyyy-MM-dd」格式。
2)現在將上面的輸出日期轉換爲格式「dd.MM.yyyy」。
例子:
Date String2date,TextViewDate;
SimpleDateFormat DB_dateFormate = new SimpleDateFormat("yyyy-MM-dd");
String2date = DB_dateFormate.parse(DB_date);
SimpleDateFormat TextViewDateFormate = new SimpleDateFormat("dd.MM.yyyy");
TextViewDate = TextViewDateFormate.format(String2date);
Log.d("OutPut Date :: ", TextViewDate);
但是,變換可以完全內的SQLite本身來完成:
select strftime('%d.%m.%Y', date);
你嘗試過什麼到目前爲止? –
'SimpleDateFormat'告訴你什麼? –