Google Answers Logo
View Question
 
Q: calendar for a particular date ( No Answer,   4 Comments )
Question  
Subject: calendar for a particular date
Category: Computers > Programming
Asked by: skatiyar-ga
List Price: $2.00
Posted: 27 Dec 2004 02:42 PST
Expires: 26 Jan 2005 02:42 PST
Question ID: 447674
CONVERT THE DAY TO THE CORRESPONDING DAY ALGORITHM.
I AM WRITIBG THE JAVA CODE FOR THE OUTPUT OF THE CORRESPONDING DAY OF
THE YEAR FOR THE GIVEN DATE.
THE CODE SHOULD BE ABLE TO GIVE THE CORRESPONDING DAY FOR THE DATE FEEDED IN .
THE ANSWER TO BE ACCEPTABLE IN THE FORM OF PUR JAVA CODe only.
Thanks and Regards
http://skatiyar.com
Answer  
There is no answer at this time.

Comments  
Subject: Re: calendar for a particular date
From: vtk-ga on 28 Dec 2004 05:10 PST
 
Hi,

Here is sample code for getting day of the year for any given date..... 

Eg: to run the code as 
   java Demo 23/10/2003

================================
public class Demo
{
	public static void main( String args[] )
	{
		if(args[0].length() == 0)
		{
			System.out.println( "Enter Date in DD/MM/YYYY format and run the code...." );
			return 1;
		}
		
		getDayOfYear(args[0]);
	}
	
	private int getDayOfYear(String x)
	{
		String[] td = x.split("/");
		if( td.length != 3 )
		{
			System.out.println( "Enter Date in DD/MM/YYYY format and run the code...." );
			return 1;
		}
		
		int month = Integer.parseInt( td[1] );
		int days = Integer.parseInt( td[0] );
		int year = Integer.parseInt( td[2] );
		
		for( int m = month - 1; i >= 1; i-- )
		{
			switch (m) 
			{
	            case 1:
	            case 3:
	            case 5:
	            case 7:				
	            case 8:
	            case 10:
	            case 12:
					days += 31;
					break;
	            case 4:
	            case 6:
	            case 9:
	            case 11:
	                days += 30;
	                break;
	            case 2:
	                if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year
% 400 == 0) )
	                    days += 29;
	                else
	                    days += 28;
	                break;
	        }
		}
		
		System.out.println( "Day of the year is:" + days );
	}
}
================================================


As i don't have java compiler in my system.. i didn't compile and test
the code... Plz rectify any syntax errors.....

Thanks,
V. Thandava Krishna
Subject: Re: calendar for a particular date
From: vtk-ga on 28 Dec 2004 05:15 PST
 
try this one plz... i rectified one typing mistake....

======================
public class Demo
{
	public static void main( String args[] )
	{
		if(args[0].length() == 0)
		{
			System.out.println( "Enter Date in DD/MM/YYYY format and run the code...." );
			return 1;
		}
		
		getDayOfYear(args[0]);
	}
	
	private int getDayOfYear(String x)
	{
		String[] td = x.split("/");
		if( td.length != 3 )
		{
			System.out.println( "Enter Date in DD/MM/YYYY format and run the code...." );
			return 1;
		}
		
		int month = Integer.parseInt( td[1] );
		int days = Integer.parseInt( td[0] );
		int year = Integer.parseInt( td[2] );
		
		for( int m = month - 1; m >= 1; m-- )
		{
			switch (m) 
			{
	            case 1:
	            case 3:
	            case 5:
	            case 7:				
	            case 8:
	            case 10:
	            case 12:
					days += 31;
					break;
	            case 4:
	            case 6:
	            case 9:
	            case 11:
	                days += 30;
	                break;
	            case 2:
	                if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year
% 400 == 0) )
	                    days += 29;
	                else
	                    days += 28;
	                break;
	        }
		}
		
		System.out.println( "Day of the year is:" + days );
	}
}
========================

Thanks,
V. Thandava Krishna
Subject: Re: calendar for a particular date
From: kishoresenji-ga on 28 Dec 2004 13:22 PST
 
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;

public class Test{
  public static void main(String[] args)
  throws Exception {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new SimpleDateFormat("MM/dd/yyyy").parse(args[0]));
    System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
  }
}
Subject: Re: calendar for a particular date
From: psrikant-ga on 04 Jan 2005 10:11 PST
 
Hey . This code might help you

import java.util.Date;

public class Test
{
  public static void main(String[] args) throws Exception
  {
    Date calendar = new
Date(Integer.parseInt(args[0])-1900,Integer.parseInt(args[1])-1,Integer.parseInt(args[2]));
    //Date calendar = new Date(d.getYear()-1900,d.getMonth()-1,d.getDate());
    int day = calendar.getDay();
    switch(day)
    {
        case 0:
            System.out.println(calendar.toString() + " is a Sunday");
            break;
        case 1:
            System.out.println(calendar.toString() + " is a Monday");
            break;
        case 2:
            System.out.println(calendar.toString() + " is a Tuesday");
            break;
        case 3:
            System.out.println(calendar.toString() + " is a Wednesday");
            break;
        case 4:
            System.out.println(calendar.toString() + " is a Thursday");
            break;
        case 5:
            System.out.println(calendar.toString() + " is a Friday");
            break;
        case 6:
            System.out.println(calendar.toString() + " is a Saturday");
            break;
        default:
            System.out.println("Not a valid date");
            break;
   }
   System.out.println(calendar.getDay());
  }
}

The sample run of this program is here.

$ java Test 2005 02 28
Mon Feb 28 00:00:00 EST 2005 is a Monday
1

Here 2005 is the year, 02 is the month and 28 is the date.

Let me know if u have any problems.
Thanks
Prashanth

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy