Monday, August 20, 2012

Calculating Julian date

There was this need for identifying the date in YYYYMMDD format when year and day of the year was to be taken from a property value.

I did this by creating utility in Pega, though Java coding is the last option suggested in PRPC.
Here is the code-
import java.util.GregorianCalendar;

public class Julian {
public static void main(String[] argv) {
    GregorianCalendar gc = new GregorianCalendar();
    String yr="2011"; // parameter to be passed into the function
    String day="230"; // parameter to be passed into the function
   
    int yr1 = Integer.parseInt(yr);
    int day1 = Integer.parseInt(day);
   
    gc.set(GregorianCalendar.YEAR, yr1);
    gc.set(GregorianCalendar.DAY_OF_YEAR, day1);
    int Day;
    int Month;

    Day=gc.get(GregorianCalendar.DAY_OF_MONTH);
    Month=(gc.get(GregorianCalendar.MONTH)+1);

    String mm=Integer.toString(Month);
    String dd=Integer.toString(Day);
    if(Month <10 ){
        mm=0+mm;
    }
    if (Day <10){
        dd=0+dd;
    }
    String date=yr+mm+dd;
  }
}

If anyone can suggest better approach, that would be great.

No comments:

Post a Comment