Java 我想更改给定时区的日期格式。我用SimpleDataFormat尝试过,但仍然失败
日期是Java 我想更改给定时区的日期格式。我用SimpleDataFormat尝试过,但仍然失败,java,android,date,date-format,date-conversion,Java,Android,Date,Date Format,Date Conversion,日期是Wed Jun 21 14:14:23 GMT+2017年8:00,我想将其转换为2017-06-21 这是我的密码: String date = "Wed Jun 21 14:14:23 GMT+08:00 2017"; SimpleDateFormat sdf = new SimpleDateFormat("EEE MMMM dd HH:mm:ss 'Z'"); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); SimpleDateFormat
Wed Jun 21 14:14:23 GMT+2017年8:00
,我想将其转换为2017-06-21
这是我的密码:
String date = "Wed Jun 21 14:14:23 GMT+08:00 2017";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMMM dd HH:mm:ss 'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat outputDate = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try {
d = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
试试这个吧,完美无瑕
String formatDate = "Wed Jun 21 14:14:23 GMT+08:00 2017";
java.text.SimpleDateFormat dateTimeFormat = new java.text.SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy" , Locale.ENGLISH);
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);
Date date = null;
try {
date = dateTimeFormat.parse(formatDate);
} catch (ParseException e) {
e.printStackTrace();
}
String format = dateFormat.format(date);
Log.d("===========>>>>>",format);
试试这个,你想怎么做就怎么做
String input_date = "Wed Jun 21 14:14:23 GMT+08:00 2017";
SimpleDateFormat fromthis = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
SimpleDateFormat tothis = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = null;
try {
date = fromthis.parse(input_date);
} catch (ParseException e) {
e.printStackTrace();
}
String result = tothis.format(date);
System.out.println(result);
输入:2017年6月21日星期三14:14:23 GMT+08:00
产出:2017-06-21
谢谢。首先,我建议您停止使用旧的
Date
和SimpleDateFormat
类。它们已经过时,正在被新的日期/时间API所取代
如果使用<强> java 8 < /强>,请考虑使用.< /P>
如果您使用的是Java,那么以哪种方式失败了。。向我们展示您迄今为止尝试过的代码。检查此问题感谢您的快速响应@rameshbab让我检查此链接伟大的人这正是我想要的@拉梅什巴布:谢谢你的回答。以便对其他人有所帮助。。
String date = "Wed Jun 21 14:14:23 GMT+08:00 2017";
// create formatter (using English locale to make sure weekdays and month names are parsed correctly)
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
// parse local date
LocalDate dt = LocalDate.parse(date, fmt);
System.out.println(dt.toString()); // 2017-06-21