Class DateTime implements interfaces IDate and ITime

Class DateTime implements interfaces IDate and ITime


  1. using System;
  2.  
  3. namespace DateTime
  4. {
  5.     interface IDate
  6.     {
  7.         int getDay();
  8.         int getMonth();
  9.         int getYear();
  10.     }
  11.     interface ITime
  12.     {
  13.         int getHour();
  14.         int getMinute();
  15.         int getSecond();
  16.     }
  17.     class DateTime : IDate, ITime        // 2 interfaces are implemented
  18.     {
  19.         private int day, month, year, hour, minute, second;
  20.  
  21.         public DateTime()
  22.         {
  23.             System.DateTime now = System.DateTime.Now;
  24.             day = now.Day;
  25.             month = now.Month;
  26.             year = now.Year;
  27.             hour = now.Hour;
  28.             minute = now.Minute;
  29.             second = now.Second;
  30.         }
  31.  
  32.         public int getDay()     // these methods have to be defined in class
  33.         {
  34.             return day;
  35.         }
  36.         public int getMonth()
  37.         {
  38.             return month;
  39.         }
  40.         public int getYear()
  41.         {
  42.             return year;
  43.         }
  44.         public int getHour()
  45.         {
  46.             return hour;
  47.         }
  48.         public int getMinute()
  49.         {
  50.             return minute;
  51.         }
  52.         public int getSecond()
  53.         {
  54.             return second;
  55.         }
  56.     }
  57.  
  58.     class Program
  59.     {
  60.         static void Main(string[] args)
  61.         {
  62.             DateTime dt = new DateTime();
  63.             Console.WriteLine("Date: {0}.{1}.{2}\tTime: {3}:{4}:{5}"
  64.                 , dt.getDay().ToString("D2"), dt.getMonth().ToString("D2"), dt.getYear().ToString("D4")
  65.                 , dt.getHour().ToString("D2"), dt.getMinute().ToString("D2"), dt.getSecond().ToString("D2"));
  66.  
  67.             Console.ReadKey();
  68.         }
  69.     }
  70. }
Last modified: Wednesday, 16 October 2019, 10:51 AM