public class DateDemo
{
public static void main(String[] args)
{
//Create scanner object to obtain input from user
Scanner input = new Scanner (System.in);
int MonthNum; //To hold the month from user input
int Year; //To hold the year
int numDays;
String Month;
System.out.print("Please enter the Month #");
MonthNum = input.nextInt();
System.out.print("Please enter the Year");
Year = input.nextInt();
if (MonthNum == 2)
{
if ( (Year % 4 == 0) && (Year % 400 == 0)
&& !(Year % 100 == 0) )
numDays = 29;
else
numDays = 28;
}
else if (MonthNum == 1 || MonthNum == 3 || MonthNum == 5 || MonthNum == 7 || MonthNum == 8
|| MonthNum == 10 || MonthNum == 12)
numDays = 31;
else
numDays = 30;
if (MonthNum == 1)
Month = "January";
else if (MonthNum == 2)
Month = "Feburary";
else if (MonthNum == 3)
Month = "March";
else if (MonthNum == 4)
Month = "April";
else if (MonthNum == 5)
Month = "May";
else if (MonthNum == 6)
Month = "June";
else if (MonthNum == 7)
Month = "July";
else if (MonthNum == 8)
Month = "August";
else if (MonthNum == 9)
Month = "September";
else if (MonthNum == 10)
Month = "October";
else if (MonthNum == 11)
Month = "November";
else if (MonthNum == 12)
Month = "December";
System.out.println(Month + " " + Year " has " + numDays "." );
System.out.println(Month);
System.out.println(numDays);
}
}
Difference Between Two Days
package
org.kodejava.example.java.util;
02.
03.
import
java.util.Calendar;
04.
05.
public
class
DateDifferentExample
06.
{
07.
public
static
void
main(String[] args)
08.
{
09.
// Creates two calendars instances
10.
Calendar cal1 = Calendar.getInstance();
11.
Calendar cal2 = Calendar.getInstance();
12.
13.
// Set the date for both of the calendar instance
14.
cal1.set(
2006
,
12
,
30
);
15.
cal2.set(
2007
,
05
,
03
);
16.
17.
// Get the represented date in milliseconds
18.
long
milis1 = cal1.getTimeInMillis();
19.
long
milis2 = cal2.getTimeInMillis();
20.
21.
// Calculate difference in milliseconds
22.
long
diff = milis2 - milis1;
23.
24.
// Calculate difference in seconds
25.
long
diffSeconds = diff /
1000
;
26.
27.
// Calculate difference in minutes
28.
long
diffMinutes = diff / (
60
*
1000
);
29.
30.
// Calculate difference in hours
31.
long
diffHours = diff / (
60
*
60
*
1000
);
32.
33.
// Calculate difference in days
34.
long
diffDays = diff / (
24
*
60
*
60
*
1000
);
35.
36.
System.out.println(
"In milliseconds: "
+ diff +
" milliseconds."
);
37.
System.out.println(
"In seconds: "
+ diffSeconds +
" seconds."
);
38.
System.out.println(
"In minutes: "
+ diffMinutes +
" minutes."
);
39.
System.out.println(
"In hours: "
+ diffHours +
" hours."
);
40.
System.out.println(
"In days: "
+ diffDays +
" days."
);
41.
}
42.
}
Responses
0 Respones to "java coding For getting no of Days in a month"
Post a Comment