Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string to a GregorianCalendar

How do a I take an input birthday string such as 02 26 1991 and make it into a Gregorian Calendar?

I tried parsing it first but it keeps giving me an error message so I'm not quite sure what I'm doing wrong. I also have other input data before this date. One is another string and one is a double value.

like image 205
Kat Avatar asked Feb 25 '10 03:02

Kat


People also ask

How do you convert string to Gregoriancalendar?

Use SimpleDateFormat to parse the date and then assign it to a Calendar . DateFormat df = new SimpleDateFormat("dd MM yyyy"); Date date = df. parse("02 26 1991"); Calendar cal = Calendar. getInstance(); cal.

What is Gregorian date format example?

Gregorian date. This format of date corresponds to any of the industry or IBM standard date formats supported by DB2. For example, the International Organization for Standardization (ISO) format is CCYY-MM-DD. 2013-12-14 is equivalent to the calendar date December 14 th 2013.


1 Answers

Use SimpleDateFormat to parse the date and then assign it to a Calendar.

DateFormat df = new SimpleDateFormat("dd MM yyyy"); Date date = df.parse("02 26 1991"); Calendar cal = Calendar.getInstance(); cal.setTime(date); 

The third line could be replaced with:

Calendar cal = new GregorianCalendar(); 

but I prefer the first version.

like image 148
cletus Avatar answered Sep 23 '22 16:09

cletus