-3

I have a string like 14-9-2014,I want to convert it as gregorian calendar,I have searched and tried lot many ways but i cant get any solution,So can any one please tell me how to conver it?thank you

user3820044
  • 169
  • 1
  • 3
  • 20
  • possible duplicate of [Convert a string to a GregorianCalendar](http://stackoverflow.com/questions/2331513/convert-a-string-to-a-gregoriancalendar) – 0101100101 Sep 16 '14 at 07:42

3 Answers3

3

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

DateFormat df = new SimpleDateFormat("dd MM yyyy");
Date date = df.parse("14-9-2014");
Calendar cal = Calendar.getInstance();
cal.setTime(date);

The third line could be replaced with:

Calendar cal = new GregorianCalendar();
user229044
  • 222,134
  • 40
  • 319
  • 330
17Coder
  • 81
  • 5
2

Take a look at this link

SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");    
Calendar calendar = new GregorianCalendar(2014,9,14,12,51,56);

int year       = calendar.get(Calendar.YEAR);
int month      = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); 
int dayOfWeek  = calendar.get(Calendar.DAY_OF_WEEK);
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);

int hour       = calendar.get(Calendar.HOUR);        // 12 hour clock
int hourOfDay  = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
int minute     = calendar.get(Calendar.MINUTE);
int second     = calendar.get(Calendar.SECOND);
int millisecond= calendar.get(Calendar.MILLISECOND);

System.out.println(sdf.format(calendar.getTime()));

System.out.println("year \t\t: " + year);
System.out.println("month \t\t: " + month);
System.out.println("dayOfMonth \t: " + dayOfMonth);
System.out.println("dayOfWeek \t: " + dayOfWeek);
System.out.println("weekOfYear \t: " + weekOfYear);
System.out.println("weekOfMonth \t: " + weekOfMonth);

System.out.println("hour \t\t: " + hour);
System.out.println("hourOfDay \t: " + hourOfDay);
System.out.println("minute \t\t: " + minute);
System.out.println("second \t\t: " + second);
System.out.println("millisecond \t: " + millisecond);
Sagar Pilkhwal
  • 5,914
  • 2
  • 27
  • 78
0

Try this:

SimpleDateFormat formatter = new SimpleDateFormat("dd-M-yyyy", context.getResources().getConfiguration().locale);

Date date = formatter.parse("14-9-2014");
Calendar c = new GregorianCalendar();
c.setTime(date);
mol
  • 2,458
  • 4
  • 20
  • 39