Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.

Bin-Blog

Calendar In Java

Just finished coding a Calender in Java. Yes, you heard me right - in Java - not in JavaScript. This was an assignment for my BCA course in IGNOU. I just ripped the code from my JavaScript Calendar program. That was JavaScript, this is Java - some small changes here and there, and vola - Calendar ready. The code is given below - feel free to rip it if you find any use for it.

/************************************************************************************
Program : Calendar
Version : 1.00.A
Displays a calender for a specified month - recives the month and year from the 
command line.
*************************************************************************************/
import java.util.Date;


public class Calendar {
 public static void main(String[] Args) {
  int year = Integer.parseInt(Args[0]);
  int month= Integer.parseInt(Args[1]) - 1;
  String month_names[] = {"January","February","March","April","May","June","July","Augest","September","October","November","December"};
  int month_days[]  = {31,28,31,30,31,30,31,31,30,31,30,31};

  //Get today's date - year, month, day and date
  Date today = new Date();
  today.setMonth(month);
  today.setYear(year-1900);

  //Display the table
  System.out.println(month_names[month] + " " + year);
  System.out.println("|| Su || Mo || Tu || We || Th || Fr || Sa ||");
  
  //Get the first day of this month
  Date first_day = today;
  first_day.setDate(1);
  int start_day = first_day.getDay();
  
  int d = 1;
  boolean flag = false;

  //Create the calender
  for(int i=0;i<=5;i++) {
   System.out.print("|");
   for(int j=0;j<7;j++) {
    if(d > month_days[month]) flag=false;//If the days has overshooted the number of days in this month, stop writing
    else if(j>=start_day && !flag) flag=true;//If the first day of this month has come, start the date writing
   
    System.out.print("| ");
    if(flag) {
     if(d<10) System.out.print("0");
     System.out.print(d + " ");
     d++;
    } else {
     System.out.print("   ");
    }
    System.out.print("|");
    
   }
   System.out.println("|");
  }
 }
}

If you know how I can improve the above script, please let me know. I don't know much about java. I never liked Java - the only program that I made in Java was the Enigma Machine encryption algorithm - after doing that, I hated java so much that I never used it again.

Technorati Tags:
Del.icio.us Tags :

0 Comments: