Reputation: 548
I am having string date = "2014-09-11"
.
i want to set this string to calendar object.
The string value is already having "-" in it. So how to use date formatting?
Upvotes: 1
Views: 5183
Reputation: 1206
You should use a date format to parse your string
String dateStr = "2014-09-11";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
DateTime date = format.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Upvotes: 6
Reputation: 35569
try this
String date = "2014-09-11";
String dt[]=date.split("-");
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DATE, Integer.parseInt(dt[2]));
cal.add(Calendar.MONTH,Integer.parseInt(dt[1]));
cal.add(Calendar.YEAR,Integer.parseInt(dt[0]));
System.out.print(cal.getTime().toString());
Upvotes: 0
Reputation: 3627
try this code:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateInString = "2014-09-11";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Upvotes: 0