Skip to content

kiwi.time.Calendar

Nikos Siatras edited this page Oct 8, 2022 · 19 revisions

The Calendar object provides methods for converting between a specific instant in time and a set of fields calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from midnight (00:00:00) January 1, 1970 00:00:00.000 Coordinated Universal Time (UTC)

Calendar.getTime()

function getTime() as DateTime

Returns a Date object representing this Calendar's current time value.

Example:
This example will print the system's current time

#include once "kiwi\kiwi.bi"
#include once "kiwi\time.bi" 

Dim myCalendar as Calendar = Calendar()
print myCalendar.getTime().toString()

Output:
The following output is an example and will not be the same when you run the example code.

Sat Oct 08 2022 21:48:08 UTC +2

Calendar.getTimeInMillis()

function Calendar.getTimeInMillis() as LongInt

Returns this Calendar's current time value in milliseconds.

Calendar.createTime()

function Calendar.createTime(timeInUTCMillis as LongInt) as DateTime

Creates and return's a DateTime object according to the given time in milliseconds elapsed since midnight (00:00:00), January 1st, 1970, Coordinated Universal Time (UTC) and the Calendar's TimeZone.

Example:
The following example will create a DateTime object from the System.currentTimeMillis() value

#include once "kiwi\kiwi.bi"
#include once "kiwi\time.bi" 

Dim myCalendar as Calendar = Calendar()

Dim currentDateTime as DateTime = myCalendar.createTime(System.currentTimeMillis())
print currentDateTime.toString()

Output:
The following output is an example and will not be the same when you run the example code.

Sat Oct 08 2022 21:52:32 UTC +2

Calendar.createTime()

function Calendar.createTime(dYear as Integer, dMonth as Integer, dDay as Integer, dHour as Integer, dMinute as Integer, dSecond as Integer) as DateTime

Creates and return's a DateTime object according to the given year, month, day, hour, minute and seconds

Example:

#include once "kiwi\kiwi.bi"
#include once "kiwi\time.bi" 

' Initialize a Calendar using the UTC+2 TimeZone
Dim myCalendar as Calendar = Calendar(+2)

' Create a new DateTime
Dim dt as DateTime = myCalendar.createTime(1940, 10, 28, 05, 15, 00)
print dt.toString()

Output:

Mon Oct 28 1940 05:15:00 UTC +2

Calendar Examples

The following simple examples will try to explain the use of Kiwi's Calendar object.

Example - Get System's Current Date Time using a Calendar

The following example code initializes a Calendar using the default time zone and prints the system's current time to console.

#include once "kiwi\kiwi.bi"
#include once "kiwi\time.bi" 

' Initialize a Calendar using the default/system's time zone
Dim systemCalendar as Calendar = Calendar()

' Initialize a new DateTime,  assign the system's current DateTime 
' to it and print time to console
Dim computerTime as DateTime = systemCalendar.getTime()
print "System's Time: " & computerTime.toString()

Example - Get Time from different Time Zones

The following code example initializes calendars for different time zones and prints the time of those zones to the console.

#include once "kiwi\kiwi.bi"
#include once "kiwi\time.bi" 

' Prints System's current date time
Dim systemCalendar as Calendar = Calendar()
Dim computerTime as DateTime = systemCalendar.getTime()
print "Your computer's Date Time is      " & computerTime.toString()

' Prints current date time in Athens/Greece
Dim athensGreeceCalendar as Calendar = Calendar(+3) '+3 UTC Time Zone
Dim timeInGreece as DateTime = athensGreeceCalendar.getTime()
print "Date Time in Athens/Greece is     " & timeInGreece.toString()

' Prints current date time in Berlin/Germany
Dim londonCalendar as Calendar = Calendar(+2) '+2 UTC Time Zone
Dim timeInLondon as DateTime = londonCalendar.getTime()
print "Date Time in Berlin/Germany is    " & timeInLondon.toString()

Example - Convert UTC timestamp to DateTime for different TimeZones

The following code example initializes calendars for different time zones and converts a UTC Timestamp to their Time Zone.

#include once "kiwi\kiwi.bi"
#include once "kiwi\time.bi" 

' The following timestamp is from Wed Sep 28 2022 06:40:39 UTC +0
Dim timeStampUTC as LongInt = 1664347239101

Dim utc0Calendar as Calendar = Calendar(0)
Dim utcTime as DateTime = utc0Calendar.createTime(timeStampUTC)
print "Timestamp to UTC +0 TimeZone:         " & utcTime.toString()

' Convert the timeStampUTC to your computer's timezone
Dim systemCalendar as Calendar = Calendar()
Dim computerTime as DateTime = systemCalendar.createTime(timeStampUTC)
print "Timestamp to System's TimeZone:       " & computerTime.toString()

' Convert the timeStampUTC to Athens/Greece timezone
Dim athensGreeceCalendar as Calendar = Calendar(+3) '+3 UTC Time Zone
Dim timeInGreece as DateTime = athensGreeceCalendar.createTime(timeStampUTC)
print "Timestamp to Athens/Greece TimeZone:  " & timeInGreece.toString()

' Convert the timeStampUTC to Berlin/Germany timezone
Dim londonCalendar as Calendar = Calendar(+2) '+2 UTC Time Zone
Dim timeInLondon as DateTime = londonCalendar.createTime(timeStampUTC)
print "Timestamp to Berlin/Germany TimeZone: " & timeInLondon.toString()