Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中将日期“2016-06-03”转换为“2016年6月3日”_Android_Date_Datetime_Simpledateformat - Fatal编程技术网

在Android中将日期“2016-06-03”转换为“2016年6月3日”

在Android中将日期“2016-06-03”转换为“2016年6月3日”,android,date,datetime,simpledateformat,Android,Date,Datetime,Simpledateformat,我的日期格式为2016-06-03,我必须将其转换为以下格式: 2016年6月3日 我尝试了以下方法: SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH); String newFormat = formatter.format("2016-06-03"); 但是,下面是一个错误: Invalid Arguments Exception 请帮我解决这个问题。谢谢。试试这个转换日

我的日期格式为2016-06-03,我必须将其转换为以下格式:

2016年6月3日

我尝试了以下方法:

 SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);
 String newFormat = formatter.format("2016-06-03");
但是,下面是一个错误:

Invalid Arguments Exception

请帮我解决这个问题。谢谢。

试试这个转换日期格式的常用功能

public static SimpleDateFormat targetFormat = new SimpleDateFormat();
public static SimpleDateFormat originalFormat = new SimpleDateFormat();
public static String formattedDate = "";

public static String getFormattedDate(String targetPattern,
                                      String existingPattern, String existingValue) {
    formattedDate = existingValue;
    targetFormat.applyPattern(targetPattern);
    DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault());
    symbols.setAmPmStrings(new String[] { "AM", "PM" });
    targetFormat.setDateFormatSymbols(symbols);
    originalFormat.applyPattern(existingPattern);
    try {
        formattedDate = targetFormat.format(originalFormat
                .parse(existingValue));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return formattedDate;
}
就像

 String txtdate = getFormattedDate("dd MMMM yyyy","yyyy-MM-dd","2016-06-03");

您可能希望使用DateFormat而不是SimpleDateFormat:

//Should print something like June 27, 2016
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ENGLISH);
String format = df.format(yourDate);

我已经编辑了你的代码从这里复制它现在工作正常

SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);
        String newFormat = null;
        try {
            newFormat = formatter.format(new SimpleDateFormat("yyyy-MM-dd").parse("2016-06-03"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Log.d("Date", ": " + newFormat); 
输出


如果你在android中从日历中获取月份,你通常会获取月份号而不是名称。但如果你想知道它的名字,你可以通过两种方式得到它

获取全名-

Calendar cal = Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
String month_name = month_date.format(cal.getTime());
获取月份的简称-

Calendar cal = Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMM");
String month_name = month_date.format(cal.getTime());

有关SimpleDateFormat类的更多信息,请查看-。

尝试使用月号开关,您将得到您想要的。可能,但不是一个好主意。。!!谢谢,只是用MMMM代替MMM。
Calendar cal = Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
String month_name = month_date.format(cal.getTime());
Calendar cal = Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMM");
String month_name = month_date.format(cal.getTime());