Need help with Beginner java code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: Need help with Beginner java code

Hybrid View

  1. #1
    Join Date
    Sep 2012

    Question Need help with Beginner java code

    Hi guys i am currently taking a java class and have never programmed before. i need to write a program that takes 3 parameters (month, day, year). Then outputs the day of the week, the date, whether or not the year is a leap year and the number of days left in the year. I have everything working except for the day of the week and the number of days left in the year.
    Here is my code:
    import java.util.*;
    import java.util.GregorianCalendar;
    import java.text.DateFormat;
     public class leapYear {
     public static void main(String[] args) throws Exception {
    GregorianCalendar newCal = new GregorianCalendar();
    	int m = Integer.parseInt (args[0]);
    	int d = Integer.parseInt (args[1]);
    	int y = Integer.parseInt (args[2]);
        newCal.set(y, m, d);
        String[] months = new String[]{
    	System.out.println( (newCal.get(Calendar.DAY_OF_WEEK)) + " - " + (months[newCal.get(Calendar.MONTH) - 1]) + " " + d + ", " + y);
    	if(newCal.isLeapYear(y) == true)
    		System.out.println(y + " is a leap year");
    		System.out.println(y + " is Not a leap year");
    int dayOfYear = newCal.get(newCal.DAY_OF_YEAR);
    int lastDayOfYear = newCal.getActualMaximum(Calendar.DAY_OF_YEAR);
    int diff = lastDayOfYear - dayOfYear;
    System.out.println("There are " + diff + " days left in the year.");
    i would really appreciate if someone can help me out.
    Thank You.

  2. #2
    Join Date
    May 2006

    Re: Need help with Beginner java code

    Your problem is down to the strange design of the Calendar class. For some unknown reason everything is 1 based except for month which is 0 based, so when you create your Calendar you need to subtract 1 from the month. Note you will need to remove the -1 you've mistakenly added in the month name look up code presumably to get the right month name to print out.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a survey!

HTML5 Development Center