在android中将字符串转换为日期格式

TRS*_*TRS 15 java datetime android datetime-format

我正在尝试将字符串转换为日期格式.我尝试了很多方法来做到这一点.但是没有成功.我的字符串是"2012年1月17日".我想将其转换为"2011-10-17".有人可以告诉我这样做的方法吗?如果您有任何实例,那将是一个真正的帮助!

Sam*_*iya 31

public class Utils {

    public static void main(String[] args) {

        String mytime="Jan 17, 2012";
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "MMM dd, yyyy");

        Date myDate = null;
        try {
            myDate = dateFormat.parse(mytime);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
        String finalDate = timeFormat.format(myDate);

        System.out.println(finalDate);
    }
}
Run Code Online (Sandbox Code Playgroud)


use*_*305 5

   String format = "yyyy-MM-dd";
   SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
   sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
Run Code Online (Sandbox Code Playgroud)

在PDT时区中运行时产生此输出:

                 yyyy-MM-dd 1969-12-31
                 yyyy-MM-dd 1970-01-01
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息,请点击此处