Monday, December 22, 2008

Java GUI to convert Julian date to Gregorain and vice versa

This simple java desktop application can convert Julian dates into Gregorain and Gregorian dates to Julian dates.This tool is useful for those working with both distributed and mainframes at the same time. The dates in the distributed platforms are in Gregorian format(DD/MM/YYYY) and in Julian format (YY/DDD)in the mainframes.

The conversion makes use of the below functions to do the calculations.

public boolean isleapyear(int year){
if ((year%4!=0)||(year%4==0)&&(year%100==0)&&(year%400!=0))
return false;
else
return true;
}

public void jultogreg() {
int gdate,gmonth;
int mondays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int jyear=Integer.parseInt(jTextField1.getText());
int jdate=Integer.parseInt(jTextField2.getText());
if(jyear>=10 && jyear<100){jTextField3.setText("20"+jyear);}
else if(jyear>0 && jyear<100){jTextField3.setText("200"+jyear);}
else {JFrame mainFrame = JulianToGregorianApp.getApplication().getMainFrame(); JOptionPane.showMessageDialog(mainFrame, "The Julian Year is out of range. Enter a value greater than 1 and less than 99.", "Input Error", JOptionPane.ERROR_MESSAGE);} if(jdate<1 ||jdate >367){
JFrame mainFrame = JulianToGregorianApp.getApplication().getMainFrame(); JOptionPane.showMessageDialog(mainFrame, "The Julian Date is out of range. Enter a value greater than 1 and less than 367.", "Input Error", JOptionPane.ERROR_MESSAGE);} if((isleapyear(jyear))){mondays[1]++;}
gdate=jdate; for(gmonth=0;gmonth<12;gmonth++){
if((gdate-mondays[gmonth])<1){ gmonth++;
if ((gdate-mondays[gmonth])==0) {gdate=mondays[gmonth];} jTextField4.setText(gmonth+"");
jTextField5.setText(gdate+""); break;}
gdate-=mondays[gmonth]; } }

public void gregtojul() {
int i;
int gmondays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ggyear=Integer.parseInt(jTextField3.getText());
int ggmonth=Integer.parseInt(jTextField4.getText());
int ggdate=Integer.parseInt(jTextField5.getText());
if((isleapyear(ggyear))){gmondays[1]++;} ggmonth--;
for(i=0;i<ggmonth;i++){ ggdate+=gmondays[i]; }
jTextField2.setText(ggdate+""); }

The conversion takes into consideration if the year is a leap year or not to do the calculations. The leap year or not function is very handy in determining if a given year is leap or not.

1 comment:

Jeff said...

very new to java and would like to use this.. how do i use your script? what program would i use for this.


ty for your help