Android的SimpleDateFormat的,如何使用它?使用它、Android、SimpleDateFormat

2023-09-13 00:27:22 作者:以后属于你

我试图使用Android的SimpleDateFormat是这样的:

I am trying to use the Android SimpledateFormat like this:

        String _Date = "2010-09-29 08:45:22"
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date = fmt.parse(_Date);
            return fmt.format(date);
        }
        catch(ParseException pe) {

            return "Date";    
        }
}

结果是好的,我有:2010-09-29

The result is good and i have: 2010-09-29

但是,如果我改变的SimpleDateFormat到

But if i change the SimpleDateFormat to

SimpleDateFormat("dd-MM-yyyy");

问题是,我会得到03-03-0035 !!!!

the problem is that I will got 03-03-0035 !!!!

为什么和如何得到像DD-MM-YYYY格式

Why and how to get the format like dd-MM-yyyy

推荐答案

我想你想扭转日期格式?

I assume you would like to reverse the date format?

的SimpleDateFormat可用于解析和格式化。 你只需要两种格式,一种解析字符串和其他返回所需的打印输出:

SimpleDateFormat can be used for parsing and formatting. You just need two formats, one that parses the string and the other that returns the desired print out:

SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date date = fmt.parse(dateString);

SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy");
return fmtOut.format(date);