0

I had a function called as setDueDate(Calendar arg0).But, the value in the field is Date of format "yyyy/MM/dd".Is there any way to convert Date object to Calendar and pass it to the function.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • 1
    duplicate of http://stackoverflow.com/questions/6185966/converting-a-date-object-to-a-calendar-object – Kaleel Apr 16 '14 at 11:18
  • Firstly,Thanks for responding.The solution given there is returning Calendar object,that is fine.but when I pass that object to my function the Date is not in specified format right...? – Snidgha Apr 16 '14 at 12:12
  • Could you post some code, that would be helpfull – Kaleel Apr 16 '14 at 12:23
  • Method for converting date to desired format: private static String convertDateToString(Date date) { SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd"); date=new Date(); String strDate=dateFormat.format(date); return strDate; } – Snidgha Apr 16 '14 at 13:40
  • Method for getting Calendar Object: 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; } – Snidgha Apr 16 '14 at 13:41
  • Now I call My Function In This Way : Date date=new Date(); String strDate=convertDateToString(date); Calendar cal=Calendar.getInstance(); cal=getCalendarFromString(strDate); activityData.setDueDate(cal); – Snidgha Apr 16 '14 at 13:41
  • ERROR: Exception in thread "main" java.text.ParseException: Unparseable date: "2014/04/16" at java.text.DateFormat.parse(Unknown Source) at com.clopay.WebService.MethodsForInsertionOfTask.getCalendarFromString(MethodsForInsertionOfTask.java:102) at com.clopay.WebService.MethodsForInsertionOfTask.insertActivityPreprocessing(MethodsForInsertionOfTask.java:51) at com.clopay.WebService.MethodsForInsertionOfTask.main(MethodsForInsertionOfTask.java:163) – Snidgha Apr 16 '14 at 13:42

1 Answers1

0

Below program is what i got from the code you provided. There is no problem with the code.

I assume you are getting error in setDueDate() function, but you did not provide it

    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; 
        }
    }
Kaleel
  • 321
  • 1
  • 6
  • setDueDate() function is an inbuilt function that is provided by oracle which accepts only calendar type argument.But I should send the Date value in the form of Calendar which is not being possible since 2 days...this is the issue I am facing – Snidgha Apr 17 '14 at 06:12
  • @Snidgha why is there an if condition to check for space (" ") in getCalendarFromString() function. Are you sure the str_date passed is in the format "yyyy/MM/dd". – Kaleel Apr 17 '14 at 07:11
  • Actually that code is not required.ye str_date is the required format.but,when I parse it to date again the format is being changed – Snidgha Apr 17 '14 at 11:59
  • @Snidgha I don't see any problem running the code from my side. My last guess would be to set Locale of SimpleDateFormat. Get your locale as Locale.getDefault() – Kaleel Apr 17 '14 at 12:35
  • Hey kaleel..issue got solved.AS I am writing client for webservice,I checked the type of the field that I want to populate in webservice.It was given as xsd:Date. I checked the timezone of xsd:date format passed the calendar object with the xsd:Date timezone.It worked...:) – Snidgha Apr 18 '14 at 07:14