Outlook请求在IST以外的时区显示错误的时间

时间:2013-07-10 14:24:07

标签: java timezone icalendar

我正在使用iCalender API在JAVA中发送会议请求。目前,它适用于IST时区,但在CST / CDT或任何其他时区部署相同的应用程序时,在生成会议请求时,它在Outlook中显示错误的时间。

E.g。我正在为今天生成Outlook请求,开始时间为上午10点,结束时间为上午11点。发送Outlook请求时,它将分别显示为开始时间和结束时间的上午11:30至下午12:30。

请参阅下面的代码,了解我如何设置请求的日历内容。

private BodyPart buildCalendarPart() throws Exception {

    BodyPart calendarPart = new MimeBodyPart();

    TimeZone timezone  = TimeZone.getDefault();
    long offset =  timezone.getOffset(Calendar.ZONE_OFFSET);

    Calendar startTime =  Calendar.getInstance();  
    startTime.setTime(taskDTO.getStartDate());  

    startTime.set(Calendar.HOUR_OF_DAY, 0);
    startTime.set(Calendar.MINUTE, 0);
    startTime.set(Calendar.SECOND, 0);


    if(taskDTO.getStartTimeHrs().equals(12)) {
         startTime.set(Calendar.HOUR, 0);
    } else {
         startTime.set(Calendar.HOUR, taskDTO.getStartTimeHrs());
    }

    startTime.set(Calendar.MINUTE, taskDTO.getStartTimeMins());
    startTime.set(Calendar.SECOND, 0);
    if(taskDTO.getStartTimeampm().equalsIgnoreCase(ApplicationConstant.AM))
        startTime.set(Calendar.AM_PM,Calendar.AM);
    else
        startTime.set(Calendar.AM_PM,Calendar.PM);

    System.out.println("Start Date :"+startTime.getTime().toString());

    Calendar endTime =  Calendar.getInstance();  
    endTime.setTime(taskDTO.getDueDate());  

    endTime.set(Calendar.HOUR_OF_DAY, 0);
    endTime.set(Calendar.MINUTE, 0);
    endTime.set(Calendar.SECOND, 0);
    if(taskDTO.getEndTimeHrs().equals(12)){
        endTime.set(Calendar.HOUR, 0);
    }else{
        endTime.set(Calendar.HOUR, taskDTO.getEndTimeHrs());
    }

    endTime.set(Calendar.MINUTE, taskDTO.getEndTimeMins());
    endTime.set(Calendar.SECOND, 0);

    if(taskDTO.getEndTimeampm().equalsIgnoreCase(ApplicationConstant.AM))
        endTime.set(Calendar.AM_PM, Calendar.AM);
    else    
        endTime.set(Calendar.AM_PM, Calendar.PM);


    Date startDate = startTime.getTime();
    Date endDate = endTime.getTime();
    iCalendarDateFormat.setTimeZone(timezone);
    //check the icalendar spec in order to build a more complicated meeting request
    String calendarContent =
        "BEGIN:VCALENDAR\n" +
        "METHOD:REQUEST\n" +
        "PRODID: BCP - Meeting\n" +
        "VERSION:2.0\n" +
        "BEGIN:VEVENT\n" +
        "DTSTAMP:" + iCalendarDateFormat.format(startDate) + "\n" +
        "DTSTART:" + iCalendarDateFormat.format(startDate)+ "\n" +
        "DTEND:"  + iCalendarDateFormat.format(endDate)+ "\n" +
        "SUMMARY:Created New Task\n" +
        "UID:" + taskDTO.getTaskID() + "\n" +
        "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:"+taskDTO.getLoggedInUserEmailID()+"\n" +
        "ORGANIZER:MAILTO:"+taskDTO.getLoggedInUserEmailID()+"\n" +
        "SEQUENCE:0\n" +
        "PRIORITY:5\n" +
        "CLASS:PUBLIC\n" +
        "STATUS:CONFIRMED\n" +
        "TRANSP:OPAQUE\n" +
        "BEGIN:VALARM\n" +
        "ACTION:DISPLAY\n" +
        "DESCRIPTION:REMINDER\n" +
        "TRIGGER;RELATED=START:-PT00H15M00S\n" +
        "END:VALARM\n" +
        "END:VEVENT\n" +
        "END:VCALENDAR";

    calendarPart.addHeader("Content-Class", "urn:content-classes:calendarmessage");
    calendarPart.setContent(calendarContent, "text/calendar;method=CANCEL");

    return calendarPart;
}

如果您对同一个

有任何意见,请告诉我

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

嗯,这完全取决于您如何格式化日期和时间。您的示例代码未显示您如何定义iCalendarDateFormat变量。我怀疑它类似于“yyyyMMdd'T'HHmmss'Z'”,表示UTC时间的日期(表格#2在http://tools.ietf.org/html/rfc5545#section-3.3.5)。因此,您的上午10点IST将转换为UTC时间的4.5 am,收到后,将转换为接收客户端的当地时间。

如果您真的想说“无论用户/客户在哪里,都应该在上午10点出现此事件”,您应该使用当地时间的日期(http://tools.ietf.org/html/rfc5545#section-3.3.5处的表格#1)。换句话说,您的iCalendarDateFormat不应包含最终的'Z'。

最后,一个简单的拼写错误:您正在发送iMIP请求,但您将calendarPart Content-Type设置为“text / calendar; method = CANCEL”。有些客户可能不喜欢这样。