Java Program to Implement Date format in Class Date | IT2305 - Java Programming Lab

AIM:
To write a Java program for the Implementation of Date format in Class Date in IT2305 - Java Programming Laboratory.

SOURCE CODE:
/**
@author    sourcecodesonline.blogspot.com
@version  1.6.0
*/
import java.lang.*;
import java.util.*;
class Daty1
{
      /** day is my private which indicates the day*/
        private int day;
      /** month is my private which indicates the month*/
        private int month;
      /** year is my private which indicates the year*/
        private int year;
      /** hr is my private which indicates the hour*/
        private int hr;
      /** min is my private which indicates the minute*/
        private int min;
      /** sec is my private which indeicates the second*/
        private int sec;
        /**
         objective        : tis is daty1 constructor used to initialize all the         fiels
         @param no para for tis constructor
        */ 
        public Daty1()
        {

                day=month=year=hr=min=sec=0;
        }
       /**
       objective        : tis is daty1 constructor used to get the values of the        fields
       @param date d is the parameter
       */ 
        public Daty1(Date d)
        {
                day=d.getDay();
                month=d.getMonth();
                year=d.getYear();
                hr=d.getHours();
                min=d.getMinutes();
                sec=d.getSeconds();
        }
        /**
    objective        : tis is displayTime  method used to display the time
  @param ch is the parameter which is the choice to display the date in various
    formats
  @return doesnt return anything 
  */ 
        public void displayTime(int ch)
        {  
            Date s1=new Date();
                switch(ch)
                {
                        case 1:
                                System.out.printf("%d:%d:%d\n",hr,min,sec);
                               System.out.printf("%tR\n",s1);
                                                  break;
                        case 2:
                                if(hr>12)
                                        System.out.printf("%d:%d:%d\n",hr-12,min        ,sec);
                                        System.out.printf("%tr\n",s1);
                                        /* else
                                        System.out.printf("%d:%d:%d\n",hr,min,se         c);*/
                                break;
                        default:
                                System.out.println("Enter correct choice");
                                break;
                 }
        }
      
 /**
  objective        : tis is displayDate  method used to display date
  @param ch is the parameter
  @return doesnt return anything 
  */ 
        public void displayDate(int ch)
        {
              Date s=new Date();
          String []monthName={"jan","feb","mar","apr","mar","may","jun","jul","a  ug","sep","oct","nov","dec"};
                switch(ch)
                {
                        case 1:
                                System.out.printf("%2d/%2d/%4d\n",day+1,month+1,   year+1900);
                               System.out.printf("%td/%tm/%ty\n",s,s,s);

                                    break;
                        case 2:
                                System.out.printf("%2d:%2d:%4d\n",day+1,month+1,   year+1900);
                                System.out.printf("%td:%tm:%ty\n",s,s,s);

                                                break;
                        case 3:
                                System.out.printf("%4d/%2d/%2d\n",year+1900,month+1,day+1);
                               System.out.printf("%ty/%tm/%td\n",s,s,s);

                                  break;
                        case 4:
                                System.out.printf("%4d:%2d:%2d\n",year+1900,month+1,day+1);
                                     System.out.printf("%ty:%tm:%td\n",s,s,s);

                                break;
                        case 5:
                                System.out.printf("%4d-%2d-%2d\n",year+1900,month+1,day+1);
                               System.out.printf("%ty-%tm-%td\n",s,s,s);

                                  break;
                        case 6:
                                System.out.printf("%d-%s-%d\n",day+1,monthName[month],year+1900);
                                System.out.printf("%td-%ta-%ty\n",s,s,s);

                                 break;
                        default:
                                System.out.println("Enter Choice 1 and 2");
                                break;
                }
        }
}
public class Daty
{
        public static void main(String args[])
        {
                int ch ;
                Date d=new Date();
                Daty1 d1=new Daty1(d);
                Scanner sin=new Scanner(System.in);
              do{
         System.out.println("****************************");
                System.out.println("\n1.date/mon/year\n 2.date:mon:year\n 3.date/mon/year\n 4.year:mon:date\n 5.year-mon-date\n 6.date-mon in string-year\n");
               System.out.println("****************************");
               System.out.print("Enter ur choice:");
                ch=sin.nextInt();
                d1.displayDate(ch);
               }while(ch<=6);
              do{
                  System.out.println("****************************");

                 System.out.println("1.RAILWAY TIME\n2.STANDARD TIME");
                System.out.println("****************************");
                 System.out.print("Enter ur choice:");
                ch=sin.nextInt();
         
               d1.displayTime(ch);
               }while(ch<=1);
    }
}
Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: