Android 日期到字符串的转换问题
Android date to string conversion issue
我想将 : Wed Apr 06 09:37:00 GMT+03:00 2016 转换为 02/02/2012。
我尝试了什么
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = sdf.parse(mydate);
和
String mydate = "Wed Apr 06 09:37:00 GMT+03:00 2016";
SimpleDateFormat src = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
SimpleDateFormat dest = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;
try {
date = src.parse(mydate);
} catch (ParseException e) {
Log.d("deneme",e.getMessage());
}
String result = dest.format(date);
但它给出错误无法解析的日期:"Wed Apr 06 09:37:00 GMT+03:00 2016"(在偏移量 0 处)知道如何解析它吗?
我认为您正在尝试格式化英语语言环境日期,但您的系统语言环境不是英语。因此,当您创建 SimpleDateFormat
对象时,请明确指定 Locale
。
试试这个代码,
String mydate = "Wed Apr 06 09:37:00 GMT+03:00 2016";
SimpleDateFormat src = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
SimpleDateFormat dest = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
Date date = null;
try {
date = src.parse(mydate);
} catch (ParseException e) {
Log.d("Exception",e.getMessage());
}
String result = dest.format(date);
Log.d("result", result);
我想将 : Wed Apr 06 09:37:00 GMT+03:00 2016 转换为 02/02/2012。 我尝试了什么
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = sdf.parse(mydate);
和
String mydate = "Wed Apr 06 09:37:00 GMT+03:00 2016";
SimpleDateFormat src = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
SimpleDateFormat dest = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;
try {
date = src.parse(mydate);
} catch (ParseException e) {
Log.d("deneme",e.getMessage());
}
String result = dest.format(date);
但它给出错误无法解析的日期:"Wed Apr 06 09:37:00 GMT+03:00 2016"(在偏移量 0 处)知道如何解析它吗?
我认为您正在尝试格式化英语语言环境日期,但您的系统语言环境不是英语。因此,当您创建 SimpleDateFormat
对象时,请明确指定 Locale
。
试试这个代码,
String mydate = "Wed Apr 06 09:37:00 GMT+03:00 2016";
SimpleDateFormat src = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
SimpleDateFormat dest = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
Date date = null;
try {
date = src.parse(mydate);
} catch (ParseException e) {
Log.d("Exception",e.getMessage());
}
String result = dest.format(date);
Log.d("result", result);