Web Analytics Made Easy -
StatCounter julian date converter - CodingForum

Announcement

Collapse
No announcement yet.

julian date converter

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • julian date converter

    so i am making a julian date converter which the program asks the user for the month, day and year and converts to julian format
    ex. 05/12/2007 --> 2007132

    it also checks if it is a leapyear, so it adds 1 however its not even display the output if i enter a leapyear such as 2008

    Code:
    import java.util.Scanner;
    public class test
    {
       public static void main (String[] args)
       {
       int mm, dd, yyyy;
       int[] Date_Array = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; 
       boolean leapyear = false;
       Scanner keyboard = new Scanner(System.in);
          System.out.println("Enter the Month");
             mm = keyboard.nextInt();
          System.out.println("Enter the Day");
             dd = keyboard.nextInt();
          System.out.println("Enter the Year");
             yyyy = keyboard.nextInt();
          int checkleap;
          checkleap = yyyy  % 4;
             if (checkleap == 0)
             {
                leapyear = true;
                System.out.println(leapyear);
             }                  
             else if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30)
                System.out.println("Error: Day cannot be greater than 30 if Month is either 4, 6, 9, or 11");
             else if ((mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) && dd > 31)
                System.out.println("Error: Day cannot be greater than 31 if Month is either 1, 3, 5, 7, 8, 10, or 12");
             else if (mm == 2 && leapyear == true && dd > 29)
                System.out.println("Error: Day cannot be greater than 29 in February on a leap year");
             else if (mm == 2 && leapyear == false && dd > 28)
                System.out.println("Error: Day cannot be greater than 28 in February on a nonleap year");
             else 
                {
                int ddd;
                long julian;
                ddd = Date_Array[mm - 1];
                ddd = ddd + dd;
                julian = yyyy * 1000;
                   if (leapyear = true)
                   {
                      julian = julian + ddd + 1;
                   }
                   else
                   {
                      julian = julian + ddd;
                   }
                System.out.println("The Julian date is " + julian);
                }   
       }
    }//end program

  • #2
    I'm not 100% sure what you're asking.

    However, if you look at your if-elseif-else block:
    You'll notice that if(checkleap == 0) then you print "true" and the programs exits. That's the way you have it programed.

    My guess is you want the if(checkleap == 0) to be separate from the rest.

    Along the lines of:
    Code:
             if (checkleap == 0)
             {
                leapyear = true;
                System.out.println(leapyear);
             }       
             //[COLOR="DarkGreen"]Separate the If's[/COLOR]  
             if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30)
                System.out.println("Error: Day cannot be greater than 30 if Month is either 4, 6, 9, or 11");
             else if ((mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) && dd > 31)
                System.out.println("Error: Day cannot be greater than 31 if Month is either 1, 3, 5, 7, 8, 10, or 12");
             else if (mm == 2 && leapyear == true && dd > 29)
                System.out.println("Error: Day cannot be greater than 29 in February on a leap year");
             else if (mm == 2 && leapyear == false && dd > 28)
                System.out.println("Error: Day cannot be greater than 28 in February on a nonleap year");
             else 
                {

    Comment


    • #3
      Thank you, that solved the problem

      Comment

      Working...
      X