-3

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.

I have a "yyyyMMdd"-formatted string, and I want to create a GregorianCalendar object.

I could easily parse this and call a series of .set() methods, but I was wondering if there was a slick-and-easy way.

Andrew
  • 20,756
  • 32
  • 99
  • 177
  • http://stackoverflow.com/questions/999172/how-to-parse-a-date http://stackoverflow.com/questions/4496359/how-to-parse-date-string-to-date and lots of other links show up in a Web search on `java parse date`. – CommonsWare Jan 19 '15 at 22:01
  • Your exact question has been answered here: http://stackoverflow.com/questions/2331513/convert-a-string-to-a-gregoriancalendar – Willis Jan 19 '15 at 22:06

1 Answers1

5

You can use a SimpleDateFormat like so:

SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date date = format.parse("20150119");
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
James McCracken
  • 15,488
  • 5
  • 54
  • 62