Android 更改字符串格式
我在应用程序中使用数据库 我有一个日期选择器,将所选日期存储为字符串并保存为yyyy-MM-dd。我有一个文本视图,其中显示所选数据,但我想将其显示为dd.MM.yyyy。我该怎么做 例如: 要将2017-05-08转换为2017年05月08日,请尝试以下操作Android 更改字符串格式,android,date,date-formatting,Android,Date,Date Formatting,我在应用程序中使用数据库 我有一个日期选择器,将所选日期存储为字符串并保存为yyyy-MM-dd。我有一个文本视图,其中显示所选数据,但我想将其显示为dd.MM.yyyy。我该怎么做 例如: 要将2017-05-08转换为2017年05月08日,请尝试以下操作 String date = getConvertDate("yyyy-MM-dd","dd.MM.yyyy","2017-05-08"); getConvertDate方法 public static String getConvert
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;
}
您可以替换为。如果需要在单个文本视图中设置,如
String newDate = oldDateString.replace("-", ".");
要更改订单,可以使用拆分选项
String[] dates = oldDateString.split("-"); // here, dates[0] is year, dates[1] is month and dates[2] is day
现在,
或更改格式
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
try {
Date date = formatter.parse(oldDateString);
newDate = formatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
试试这个
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);
}
现在,您的新字符串将如下所示:
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);
以下是2017年5月8日的样本输出
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 在此处传递您当前的日期格式和所需的日期格式:
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告诉您了什么吗?我如何更改订单?因为这一天必须从现在开始,而不是结束。当我完成你给我的第三行代码时,我有一种出界的感觉。。
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");
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);
select strftime('%d.%m.%Y', date);