将字符串转换为日历。最简单的方法是什么?

3
什么是将格式化字符串转换为日历的最简单和最容易的方法呢?例如,将“dd.MM.yyyy”转换为日历?

2
你尝试过谷歌搜索或使用这里的搜索功能吗? - Hans Hohenfeld
当然啦。但是我没有找到简单的答案。谢谢你的帮助... - Ksice
3个回答

35
DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
Calendar cal  = Calendar.getInstance();
cal.setTime(df.parse(stringInstanceRepresentingDate));

2

尝试使用这段代码:

String dateStr = "04/05/2010"; 

SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 
Date dateObj = curFormater.parse(dateStr); 
Calendar calendar = Calendar.getInstance();
calendar .setTime(dateObj)

1

尝试:

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Calendar calendarDate = Calendar.getInstance();
if (calendarDate!= null) {
strdate = sdf.format(calendarDate.getTime());
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接