我有一个名为 setDueDate(Calendar arg0) 的函数。但是,该字段中的值是格式为“yyyy/MM/dd”的日期。有没有办法将 Date 对象转换为 Calendar 并将其传递给函数。
问问题
192 次
1 回答
0
下面的程序是我从你提供的代码中得到的。代码没有问题。
我假设您在setDueDate()函数中遇到错误,但您没有提供它
public class ConvertDateToCalender
{
public static void main(String[] args)
{
try
{
Date date = new Date();
String strDate = convertDateToString(date);
Calendar cal = Calendar.getInstance();
cal = getCalendarFromString(strDate);
//activityData.setDueDate(cal);
}
catch (Exception ex)
{
}
}
private static String convertDateToString(Date date)
{
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd");
date=new Date();
String strDate=dateFormat.format(date);
return strDate;
}
private static Calendar getCalendarFromString(String str_date) throws ParseException
{
DateFormat formatter;
Date date;
if (str_date.contains(" "))
str_date = str_date.substring(0, str_date.indexOf(" "));
formatter = new SimpleDateFormat("yyyy/MM/dd");
date = (Date) formatter.parse(str_date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}
}
于 2014-04-17T05:16:04.167 回答