0

Hi Iam retrieving the events based on the datetime . When iam passing the datetime as query for getting events from google calender getting below exception.

 private static void dateRangeQuery(CalendarService service) throws ServiceException,
              IOException {
          DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm");
           //get current date time with Date()
           Date date = new Date();
           System.out.println(dateFormat.format(date));

           //get current date time with Calendar()
           Date dt = Calendar.getInstance().getTime();
           //System.out.println(dateFormat.format(cal.getTime()));
          // System.out.println(cal.getTime());
          DateTime startTime =  DateTime.parseDateTime(dateFormat.format(dt));
          Calendar cal2 = Calendar.getInstance();
          cal2.add(Calendar.MINUTE, 20);
           System.out.println(dateFormat.format(cal2.getTime()));
           System.out.println(cal2.getTime());
          DateTime endTime =  DateTime.parseDate(dateFormat.format(cal2.getTime()));

            CalendarQuery myQuery = new CalendarQuery(eventFeedUrl);
            myQuery.setMinimumStartTime(startTime);
            myQuery.setMaximumStartTime(endTime);

            // Send the request and receive the response:
            CalendarEventFeed resultFeed = service.query(myQuery,
                CalendarEventFeed.class);

            //System.out.println("Events from " + startTime.toString() + " to "
            //    + endTime.toString() + ":");
            System.out.println();
            for (int i = 0; i < resultFeed.getEntries().size(); i++) {
              CalendarEventEntry entry = resultFeed.getEntries().get(i);
              System.out.println("\t" + entry.getTitle().getPlainText());
            }
            System.out.println();
          }

Exception below:

Exception in thread "main" java.lang.NumberFormatException: Invalid date/time format.
    at com.google.gdata.data.DateTime.parseDateTime(DateTime.java:303)
    at GoogleCalender.dateRangeQuery(GoogleCalender.java:185)
    at GoogleCalender.main(GoogleCalender.java:115)

please can anyone suggest how to resolve this issue

1 Answer 1

2

The XML dateTime pattern, expected by DateTime.parseDateTime(), is [-]CCYY-MM-DDThh:mm:ss[Z|(+|-)hh:mm].

So, the date should be formatted using the SimpleDateFormat yyyy-MM-dd'T'HH:mm:ss pattern (not tested).

But an even simpler way would be to simply use the DateTime constructor which takes a java.util.Date as argument, instead of transforming the Date to a String, and then thuis Date to a DateTime.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.