How to set TimeZone in calendar

Hi team,
I am wroking on Java. I am using Aspose 18.6. I am exporting calendar into pst. Can you please help for how to set the timezone in calendar.

@Rajmohammad,

Thank you for contacting Aspose Support.
You may change the TimeZone in the calendar using the code snippet given below.

MapiCalendarTimeZoneRule daylightTransitionTime = new MapiCalendarTimeZoneRule();
daylightTransitionTime.setHour(0);
daylightTransitionTime.setMilliseconds(0);
daylightTransitionTime.setMinute(0);
daylightTransitionTime.setMonth(0);
daylightTransitionTime.setSeconds(0);
daylightTransitionTime.setYear(0);
daylightTransitionTime.setDayOfWeek(DayOfWeek.Sunday);
daylightTransitionTime.setPosition(DayPosition.None);

MapiCalendarTimeZoneRule standardTransitionTime = new MapiCalendarTimeZoneRule();
standardTransitionTime.setHour(0);
standardTransitionTime.setMilliseconds(0);
standardTransitionTime.setMinute(0);
standardTransitionTime.setMonth(0);
standardTransitionTime.setSeconds(0);
standardTransitionTime.setYear(0);
standardTransitionTime.setDayOfWeek(DayOfWeek.Sunday);
standardTransitionTime.setPosition(DayPosition.None);

MapiCalendarTimeZoneInfo mapiCalendarTimeZoneInfo = new MapiCalendarTimeZoneInfo();
mapiCalendarTimeZoneInfo.setBias(660);
mapiCalendarTimeZoneInfo.setStandardBias(0);
mapiCalendarTimeZoneInfo.setDaylightBias(0);
mapiCalendarTimeZoneInfo.setDaylightDate(daylightTransitionTime);
mapiCalendarTimeZoneInfo.setStandardDate(standardTransitionTime);
mapiCalendarTimeZoneInfo.setYear(0);
mapiCalendarTimeZoneInfo.setTimeZoneFlags(MapiCalendarTimeZoneFlags.TZRULE_FLAG_RECUR_CURRENT_TZREG | MapiCalendarTimeZoneFlags.TZRULE_FLAG_EFFECTIVE_TZREG);

MapiCalendarTimeZone timeZone = new MapiCalendarTimeZone();
timeZone.setKeyName("UTC-11");
MapiCalendarTimeZoneInfoCollection timeZoneInfos = new MapiCalendarTimeZoneInfoCollection();
timeZoneInfos.add(mapiCalendarTimeZoneInfo);
timeZone.setTimeZoneRules(timeZoneInfos);

mapiCalendar.setStartDateTimeZone(timeZone);
mapiCalendar.setEndDateTimeZone(timeZone);

We hope that this answered your question. Please feel free to reach us if additional information is required.

Hi,
I have created an PST file for some calendars of Russian Standard Time
But It shows only on Mac OS Outlook and in windows outlook 2007.
not shows proper time on windows outlook 2010 and above version
Please help me to fix this issue
Here is my code
MapiCalendarTimeZoneRule daylightTransitionTime = new MapiCalendarTimeZoneRule();
daylightTransitionTime.setHour(2);
daylightTransitionTime.setMilliseconds(0);
daylightTransitionTime.setMinute(0);
daylightTransitionTime.setMonth(3);
daylightTransitionTime.setSeconds(0);
daylightTransitionTime.setYear(0);
daylightTransitionTime.setDayOfWeek(DayOfWeek.Sunday);
daylightTransitionTime.setPosition(DayPosition.Last);

				MapiCalendarTimeZoneRule standardTransitionTime = new MapiCalendarTimeZoneRule();
				standardTransitionTime.setHour(2);
				standardTransitionTime.setMilliseconds(0);
				standardTransitionTime.setMinute(0);
				standardTransitionTime.setMonth(1);
				standardTransitionTime.setSeconds(0);
				standardTransitionTime.setYear(0);
				standardTransitionTime.setDayOfWeek(DayOfWeek.Sunday);
				standardTransitionTime.setPosition(DayPosition.Last);

				MapiCalendarTimeZoneInfo mapiCalendarTimeZoneInfo = new MapiCalendarTimeZoneInfo();
				mapiCalendarTimeZoneInfo.setBias(0);
				mapiCalendarTimeZoneInfo.setStandardBias(0);
				mapiCalendarTimeZoneInfo.setDaylightBias(0);
				mapiCalendarTimeZoneInfo.setDaylightDate(daylightTransitionTime);
				mapiCalendarTimeZoneInfo.setStandardDate(standardTransitionTime);
				mapiCalendarTimeZoneInfo.setYear(2015);
				mapiCalendarTimeZoneInfo.setTimeZoneFlags(MapiCalendarTimeZoneFlags.TZRULE_FLAG_EFFECTIVE_TZREG);
			
				MapiCalendarTimeZoneInfoCollection coll = new MapiCalendarTimeZoneInfoCollection();
				coll.addItem(mapiCalendarTimeZoneInfo);

MapiCalendarTimeZone f_CalendarTimeZone = new MapiCalendarTimeZone();
f_CalendarTimeZone.setKeyName(“Russian Standard Time”);
f_CalendarTimeZone.setTimeZoneRules(coll);

				f_MapiCalendarObj.setStartDateTimeZone(f_CalendarTimeZone);
				f_MapiCalendarObj.setEndDateTimeZone(f_CalendarTimeZone);

@chetan.m,

There are no rules for Russian Standard Time at present.
Therefore, your code should be as follows:

MapiCalendarTimeZone f_CalendarTimeZone = new MapiCalendarTimeZone();
f_CalendarTimeZone.setKeyName(“Russian Standard Time”);
f_MapiCalendarObj.setStartDateTimeZone(f_CalendarTimeZone);
f_MapiCalendarObj.setEndDateTimeZone(f_CalendarTimeZone);

Thank you.

Hi, I tried With the same.
But still exported PST File is shows Correct time Only in Mac Outlooks and Windows outlook 2007.

windows outlook 2010 and above shows 3 hours before

@chetan.m,

Can you send us full code sample and output pst? Thanks.

I have provided all the code to you.
the details of mapiCalendar is

First Calendar:
f_MapiCalendarObj.getStartDate() = Sun Jan 15 15:30:00 IST 2023
f_MapiCalendarObj.getEndDate() = Sun Jan 15 16:00:00 IST 2023

Second Calendar:
f_MapiCalendarObj.getStartDate() = Thu Jan 12 17:00:00 IST 2023
f_MapiCalendarObj.getEndDate() = Wed Jan 18 17:30:00 IST 2023

and output PST file is here
Moscow.zip (9.6 KB)Screenshot 2023-01-12 at 6.26.25 PM.png (20.8 KB)
Screenshot 2023-01-12 at 6.27.23 PM.png (23.6 KB)
and there are the original date set in the screen shots

@chetan.m,

Thank you for the info provided. I have created the EMAILJAVA-35135 ticket.

Hello @chetan.m,

Can you initialize MapiCalendarTimeZone with timezone ID. The rules will be created automatically.

MapiCalendarTimeZone timeZone = new MapiCalendarTimeZone("Russian Standard Time");

Hi @sergey.vivsiuk

I Just tried with the way you are suggested but result was same.

Hi @chetan.m,

Could you check the test on your side?

TimeZone tz = TimeZone.getTimeZone("Europe/Moscow");
Calendar c = Calendar.getInstance(tz);
c.set(2023, 0, 15, 15, 30, 0);
Date startDate = c.getTime();
c.set(2023, 0, 15, 16, 0, 0);
Date endDate = c.getTime();

MapiCalendar meeting = new MapiCalendar("test location", "timezone sample", "test descr",
        startDate, endDate, "", new MapiRecipientCollection());

MapiCalendarTimeZone timeZone = new MapiCalendarTimeZone("Russian Standard Time");
meeting.setStartDateTimeZone(timeZone);
meeting.setEndDateTimeZone(timeZone);

try (PersonalStorage pst = PersonalStorage.create("test.pst", FileFormatVersion.Unicode)) {
    FolderInfo calendarFolder = pst.createPredefinedFolder("Calendar", StandardIpmFolder.Appointments);
    calendarFolder.addMapiMessageItem(meeting);
}

Hi @sergey.vivsiuk
using the above code mac Outlook shows 3 hours before and windows outlook shows 6 hours before the actual time

my previous code is
TimeZone tz = TimeZone.getTimeZone(“GMT”);
Calendar c = Calendar.getInstance(tz);

It was showing proper time in Mac outlook and 3hours before in windows outlook

Hi @chetan.m,

I tested the issue on a Windows machine with Outlook 2211, current timezone IST(+5:30).
The java.util.Date objects do not contain any timezone information.
If we need to set the time to 15:30 in Moscow(+3) tz, we need to convert Moscow time 15:30 to current timezone (IST)

TimeZone tz = TimeZone.getTimeZone("Europe/Moscow");
Calendar c = Calendar.getInstance(tz);
c.set(2023, 0, 15, 15, 30, 0);
Date startDate = c.getTime();
System.out.println(startDate); // Sun Jan 15 18:00:00 IST 2023 (+2:30 from Moscow)
c.set(2023, 0, 15, 16, 0, 0);
Date endDate = c.getTime();
System.out.println(endDate); // Sun Jan 15 18:30:00 IST 2023 (+2:30 from Moscow)

If we open the PST with Outlook we will see 18:00 - 18:30 in the current IST tz.
With a different timezone settings Outlook will show us a different time according to the current timezone, but after opening a meeting we will always see 15:30 - 16:00 Moscow time

0.png (11.5 KB)

Hi @sergey.vivsiuk
I have checked the PST in both mac and windows outlook 2016
please check the attached screenshot of windows outlook which shows 6 hours difference in the meeting
I we set
TimeZone tz = TimeZone.getTimeZone(“Europe/Moscow”);
Calendar c = Calendar.getInstance(tz);
MicrosoftTeams-image (1).png (120.6 KB)

Hi @chetan.m,

To ensure a timely and accurate response, please share code sample to reproduce the issue:

  • MapiCalendar object creation code with “start date” and “end date”

Also I need information about:

  • Current timezone on the machine where PST is created and timezone on the machine where PST is opened.
  • What date should be displayed on the Outlook calendar (in Moscow timezone)

Please find the information here

  1. Current timezone on the machine where PST is created
    -> India Standard Time
  2. timezone on the machine where PST is opened
    -> India Standard Time
  3. date should be set in the Calendar (I am expected time should be visible on both mac and windows outlook properly)
    -> Start 15-Jan-2023 1:00 PM
    End 15-Jan-2023. 1.30 PM
  4. date I am setting here to MapiCalendar object are
    start = “Sun Jan 15 15:30:00 IST 2023”;
    end = “Sun Jan 15 16:00:00 IST 2023”

MapiCalendarTimeZone f_CalendarTimeZone = new MapiCalendarTimeZone();
f_CalendarTimeZone.setKeyName(“Russian Standard Time”);
f_MapiCalendarObj.setStartDateTimeZone(f_CalendarTimeZone);
f_MapiCalendarObj.setEndDateTimeZone(f_CalendarTimeZone);

				f_FolderInfoObj.addMapiMessageItem(f_MapiCalendarObj);

Please let me know if you required other details

@chetan.m, the MapiCalendar creation code is also required:

  • MapiCalendar object creation code with “start date” and “end date”

f_objMapiCalendar = new MapiCalendar(f_Location,subject,"", startDate, endByDate);

Hi @chetan.m,

I have prepared a PST and tested it with Outlook on Windows and Mac OS.
All Calendar event data displayed in Outlook as expected(screenshots attached)
How can we reproduce the issue?

Code to generate test PST:
(test PST generated on Windows machine with IST timezone)

TimeZone tz = TimeZone.getTimeZone("Europe/Moscow");
Calendar c = Calendar.getInstance(tz);
c.set(2023, 0, 15, 13, 0, 0);
Date startDate = c.getTime();
System.out.println(startDate);
c.set(2023, 0, 15, 13, 30, 0);
Date endDate = c.getTime();
System.out.println(endDate);

MapiCalendar meeting = new MapiCalendar("test location", "timezone sample", "test descr",
        startDate, endDate, "", new MapiRecipientCollection());

MapiCalendarTimeZone timeZone = new MapiCalendarTimeZone("Russian Standard Time");
meeting.setStartDateTimeZone(timeZone);
meeting.setEndDateTimeZone(timeZone);

try (PersonalStorage pst = PersonalStorage.create("test.pst", FileFormatVersion.Unicode)) {
    FolderInfo calendarFolder = pst.createPredefinedFolder("Calendar", StandardIpmFolder.Appointments);
    pst.createPredefinedFolder("Inbox", StandardIpmFolder.Inbox);
    calendarFolder.addMapiMessageItem(meeting);
}

Attachments:
test.pst - PST generated in my local env
w2016_0.png/w2016_1.png - Outlook 2016/Windows 10/IST timezone
w2211_0.png/w2211_1.png - Outlook 2211/Windows 10/IST timezone
mac_0.png/mac_1.png/mac_2.png - Outlook 15.6/MacOS Sierra/IST timezone

test.zip (6.3 KB)
mac_0.png (35.0 KB)
mac_1.png (57.4 KB)
mac_2.png (57.2 KB)
w2016_0.png (17.2 KB)
w2016_1.png (37.9 KB)
w2211_0.png (19.2 KB)
w2211_1.png (22.9 KB)

Hi @sergey.vivsiuk
I have checked your PST and It is worked with both Windows and Mac Outlook.
but If I tried the same code which you provided is not showing the proper timezone on windows outlook 16.
I have attached here a test.pst.zip (6.5 KB)
MicrosoftTeams-image.png (74.3 KB)
screenshot of windows outlook 16 for your reference
please check my code also
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.aspose.email.FileFormatVersion;
import com.aspose.email.FolderInfo;
import com.aspose.email.MapiCalendar;
import com.aspose.email.MapiCalendarTimeZone;
import com.aspose.email.MapiRecipientCollection;
import com.aspose.email.PersonalStorage;
import com.aspose.email.StandardIpmFolder;

public class Main {

public static void main(String[] args)
{
	TimeZone tz = TimeZone.getTimeZone("Europe/Moscow");
	Calendar c = Calendar.getInstance(tz);
	c.set(2023, 0, 15, 13, 0, 0);
	Date startDate = c.getTime();
	System.out.println(startDate);
	c.set(2023, 0, 15, 13, 30, 0);
	Date endDate = c.getTime();
	System.out.println(endDate);

	MapiCalendar meeting = new MapiCalendar("test location", "timezone sample", "test descr",
			startDate, endDate, "", new MapiRecipientCollection());

	MapiCalendarTimeZone timeZone = new MapiCalendarTimeZone("Russian Standard Time");
	meeting.setStartDateTimeZone(timeZone);
	meeting.setEndDateTimeZone(timeZone);
	
	PersonalStorage pst = PersonalStorage.create("/Users/user37/Desktop/test.pst", FileFormatVersion.Unicode);
			FolderInfo calendarFolder = pst.createPredefinedFolder("Calendar", StandardIpmFolder.Appointments);
	pst.createPredefinedFolder("Inbox", StandardIpmFolder.Inbox);
	calendarFolder.addMapiMessageItem(meeting);
}

}

I am using aspose.Email 18.6. Can you please verify the version you are using