简单的方法来转换" YYYYMMDD"字符串格式化的成GregorianCalendar对象 [英] Simple way to convert "yyyyMMdd"-formatted string into a GregorianCalendar object
本文介绍了简单的方法来转换" YYYYMMDD"字符串格式化的成GregorianCalendar对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到很多采取的GregorianCalendar或Date对象,并打印年月日格式化的字符串的问题。
I see plenty of questions for taking a GregorianCalendar or Date object and printing a "yyyyMMdd"-formatted string.
不过,我想去相反的方向。
However, I want to go the opposite direction.
我有年月日格式化的字符串,我想创建一个GregorianCalendar的对象。
I have a "yyyyMMdd"-formatted string, and I want to create a GregorianCalendar object.
我可以轻松地分析这个并调用了一系列的的.set()
方法,但我不知道是否有一个油滑和容易的方式。
I could easily parse this and call a series of .set()
methods, but I was wondering if there was a slick-and-easy way.
推荐答案
您可以使用的SimpleDateFormat
像这样:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date date =format.parse("20150119");
Calendar calendar =GregorianCalendar.getInstance();
calendar.setTime(date);
这篇关于简单的方法来转换" YYYYMMDD"字符串格式化的成GregorianCalendar对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文