我在 Android 应用程序中使用区域设置设置日语日期(例如 21 11 月 2016)。但是,我想将该日期以“dd-MM-yyyy”(例如 21-11-2016)格式发送到我的 API。
当我尝试这样做时,出现以下异常:
java.lang.IllegalArgumentException: Bad class: class java.lang.String
下面是我的代码:
public String convertDate(String date) {
System.out.println("......ORIGINAL DATE....... " + date); // ......ORIGINAL DATE....... 21 11月 2016
String myFormat = "dd-MM-yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, new Locale(getMyLocale())); //Japanese locale is set but tried english too
System.out.println("......CONVERTED DATE....... " + sdf.format(date)); //should be 21-11-2016 but getting above error
return sdf.format(new Date(date));
}
此外,对于美国/英语语言环境,它工作正常,但对于日语,我收到此错误。
最佳答案
您需要两个格式化程序,如下所示
String in = "2016年11月 21日"; // really a space here?
SimpleDateFormat sdf1 = new SimpleDateFormat ("yyyy年MM月 dd日");
Date d1 = sdf1.parse(in);
SimpleDateFormat sdf2 = new SimpleDateFormat ("dd-MM-yyyy");
System.out.println(sdf2.format(d1));
关于java - 如何将日文日期转换为英文日期格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40713911/