更改格式字符串 android

Change format String android

我在我的应用程序中使用数据库。

我有日期选择器,我将所选日期存储为字符串并将其另存为 "yyyy-MM-dd"。我有一个 TextView,我在其中显示所选数据,但我想将其显示为 "dd.MM.yyyy"。我该怎么做?

例如:

将 2017-05-08 转换为 08.05.2017

试试这个

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();
   }
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);
}
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

尝试

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);