Question

I need a GregorianCalendar from String "day/month/year" - 1/12/2014 for example. Anyone can help? advice? Or if this is hard to do, mayby Yu have a better idea?

Was it helpful?

Solution

Try with SimpleDateFormat to parse your String to a date. Set this date to your GregorianCalendar instance:

String dateStr = "1/12/2014";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = sdf.parse(dateStr);
Calendar cal = new GregorianCalendar();
cal.setTime(date);

This can be achieved using joda-time as well:

String dateStr = "1/12/2014";
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime dateTime = formatter.parseDateTime(dateStr);
GregorianCalendar gc = dateTime.toGregorianCalendar();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top