Title: Example of COBOL using Java classes to get date and time zone information
The following is an example of a COBOL program that returns information about a date and time using the java.util.GregorianCalendar and other related classes. You can save this file as calendar.cbl and the compile and run it as follows:

iscc calendar.cbl
java CALENDAR

Getting information about Jan 26, 2010
ERA: 1
YEAR: 2010
MONTH: 0
WEEK_OF_YEAR: 5
WEEK_OF_MONTH: 5
DATE: 26
DAY_OF_MONTH: 26
DAY_OF_YEAR: 26
DAY_OF_WEEK: 3
DAY_OF_WEEK_IN_MONTH: 4
AM_PM: 1
HOUR: 2
HOUR_OF_DAY: 14
MINUTE: 54
SECOND: 55
MILLISECOND: 115
ZONE_OFFSET: -8
DST_OFFSET: 0

The following is based on the example at http://java.sun.com/javase/6/docs/api/java/util/GregorianCalendar.html

       program-id. calendar.
       configuration section.
       repository.                 
           class JTimeZone       as "java.util.TimeZone"
           class JStringArray    as "java.lang.String[]"
           class JSimpleTimeZone as "java.util.SimpleTimeZone"
           class JCalendar       as "java.util.Calendar"
           class JGregorianCalendar  as "java.util.GregorianCalendar"
           .    
              
       data division.
       working-storage section.
       
       77 ids             object reference JStringArray.
       77 pdt             object reference JSimpleTimeZone.
       77 calendar        object reference JGregorianCalendar.
       77 zone-offset     signed-long.
       77 dst-offset      signed-long.


       procedure division.
       main.
      * get the supported ids for GMT-8 (Pacific Standard Time)
            set ids to JTimeZone:>getAvailableIDs(-28800000).
            if ids:>length = 0 then stop run.
      
      * create a Pacific Standard Time time zone
            set pdt to JSimpleTimeZone:>new(-28800000, ids(0)).
      
      * set up rules for daylight savings time
	    pdt:>setStartRule(JCalendar:>MARCH, 8, JCalendar:>SUNDAY, 
	        7200000).
	    pdt:>setEndRule(JCalendar:>NOVEMBER, 1, JCalendar:>SUNDAY, 
	        7200000).
      
      * create a GregorianCalendar 
            set calendar to JGregorianCalendar:>new(pdt).
      
            display "Getting information about Jan 26, 2010".
            calendar:>set(2010, 0, 26).
            display "ERA: " calendar:>get(JCalendar:>ERA).
            display "YEAR: " calendar:>get(JCalendar:>YEAR).
            display "MONTH: " calendar:>get(JCalendar:>MONTH).
            display "WEEK_OF_YEAR: " 
                calendar:>get(JCalendar:>WEEK_OF_YEAR).
            display "WEEK_OF_MONTH: " 
                calendar:>get(JCalendar:>WEEK_OF_MONTH).
            display "DATE: " calendar:>get(JCalendar:>DATE).
            display "DAY_OF_MONTH: " 
                calendar:>get(JCalendar:>DAY_OF_MONTH).
            display "DAY_OF_YEAR: " 
                calendar:>get(JCalendar:>DAY_OF_YEAR).
            display "DAY_OF_WEEK: " 
                calendar:>get(JCalendar:>DAY_OF_WEEK).
            display "DAY_OF_WEEK_IN_MONTH: "
                    calendar:>get(JCalendar:>DAY_OF_WEEK_IN_MONTH).
            display "AM_PM: " calendar:>get(JCalendar:>AM_PM).
            display "HOUR: " calendar:>get(JCalendar:>HOUR).
            display "HOUR_OF_DAY: " 
                calendar:>get(JCalendar:>HOUR_OF_DAY).
            display "MINUTE: " calendar:>get(JCalendar:>MINUTE).
            display "SECOND: " calendar:>get(JCalendar:>SECOND).
            display "MILLISECOND: " 
                calendar:>get(JCalendar:>MILLISECOND).
            set zone-offset to calendar:>get(JCalendar:>ZONE_OFFSET).
            divide zone-offset by 3600000 giving zone-offset.
            display "ZONE_OFFSET: " zone-offset.
            set dst-offset to calendar:>get(JCalendar:>DST_OFFSET).
            divide dst-offset by 3600000 giving dst-offset.
            display "DST_OFFSET: " dst-offset.
            stop run.

For comparison, here is the Java source code for an identical program:


import java.util.*;

public class jcalendar {
	public static void main(String argv[]) {
		// get the supported ids for GMT-8 (Pacific Standard Time)
		String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
		if (ids.length == 0)
			System.exit(0);

		// create a Pacific Standard Time time zone
		SimpleTimeZone pdt = 
			new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);

		// set up rules for daylight savings time
		pdt.setStartRule(Calendar.MARCH, 8, Calendar.SUNDAY,
				2 * 60 * 60 * 1000);
		pdt.setEndRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY,
				2 * 60 * 60 * 1000);

		// create a GregorianCalendar 
		Calendar calendar = new GregorianCalendar(pdt);

		System.out.println("Getting information about Jan 26, 2010");
		calendar.set(2010, 0, 26);

		System.out.println("ERA: " + calendar.get(Calendar.ERA));
		System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
		System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
		System.out.println("WEEK_OF_YEAR: "
				+ calendar.get(Calendar.WEEK_OF_YEAR));
		System.out.println("WEEK_OF_MONTH: "
				+ calendar.get(Calendar.WEEK_OF_MONTH));
		System.out.println("DATE: " + calendar.get(Calendar.DATE));
		System.out.println("DAY_OF_MONTH: "
				+ calendar.get(Calendar.DAY_OF_MONTH));
		System.out.println("DAY_OF_YEAR: " 
				+ calendar.get(Calendar.DAY_OF_YEAR));
		System.out.println("DAY_OF_WEEK: " 
				+ calendar.get(Calendar.DAY_OF_WEEK));
		System.out.println("DAY_OF_WEEK_IN_MONTH: "
				+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
		System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
		System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
		System.out.println("HOUR_OF_DAY: " 
				+ calendar.get(Calendar.HOUR_OF_DAY));
		System.out.println("MINUTE: " + 
				calendar.get(Calendar.MINUTE));
		System.out.println("SECOND: " + 
				calendar.get(Calendar.SECOND));
		System.out.println("MILLISECOND: " 
				+ calendar.get(Calendar.MILLISECOND));
		System.out.println("ZONE_OFFSET: "
				+ (calendar.get(Calendar.ZONE_OFFSET) / 
						(60 * 60 * 1000)));
		System.out.println("DST_OFFSET: "
				+ (calendar.get(Calendar.DST_OFFSET) / 
						(60 * 60 * 1000)));
	}
}

Authored by: Veryant Support on Fri, Jan 29th, 2010 at 7:00 PM
This question has been viewed 7382 times so far.
Online URL: http://support.veryant.com/support/phpkb/question.php?ID=119

Powered by PHPKB Knowledge Base Software