如何在不使用任何已弃用的类的情况下将日期从一种格式转换为另一种格式的另一种日期对象? [英] How do I convert the date from one format to another date object in another format without using any deprecated classes?
本文介绍了如何在不使用任何已弃用的类的情况下将日期从一种格式转换为另一种格式的另一种日期对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将date1格式的日期转换为date2格式的日期对象。
I'd like to convert a date in date1 format to a date object in date2 format.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd, yyyy");
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.set(2012, 8, 21);
Date date = cal.getTime();
Date date1 = simpleDateFormat.parse(date);
Date date2 = simpleDateFormat.parse(date1);
println date1
println date2
推荐答案
DateFormat originalFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyyMMdd");
Date date = originalFormat.parse("August 21, 2012");
String formattedDate = targetFormat.format(date); // 20120821
另请注意解析
需要a String
,而不是 Date
对象,已经解析。
Also note that parse
takes a String
, not a Date
object, which is already parsed.
这篇关于如何在不使用任何已弃用的类的情况下将日期从一种格式转换为另一种格式的另一种日期对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文