/*
Display Month of year using Java Calendar
This example shows how to display a month like January, February etc.
using Java Calendar class.
*/
import java.util.Calendar;
public class DisplayMonthOfYear {
public static void main(String[] args) {
//create Calendar instance
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
//create an array of months
String[] strMonths = new String[]{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
System.out.println("Current month is : " +
strMonths[now.get(Calendar.MONTH)]
);
}
}
/*
Typical output would be
Current date : 12-25-2007
Current month is : Dec
*/
Bookmark/Search this post with:
Help!
I have to make a code that lets the user in put a number equivalent to a month and a year..
then it will print the month together with its days.. it will also print the previous and next month to it.. i still couldn't do it with all my efforts.. here's my code, yet..
import java.util.GregorianCalendar;
class Calendar{
public static void main(String[] args) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(year, motnh-1, 1);
int x = calendar.get(GregoriaanCalendar.DAY_OF_WEEK);
int year = Integer.parseInt(args[1]);
int month = Integer.parseInt(args[0]);
int numDays = 0;
switch (month) {
case 1: System.out.println("January\t" + year);
case 3: System.out.println("March\t" + year);
case 5: System.out.println("May\t" + year);
case 7: System.out.println("July\t" + year);
case 8: System.out.println("August\t" + year);
case 10: System.out.println("October\t" + year);
case 12: System.out.println("December\t" + year);
numDays = 31;
break;
case 4: System.out.println("April\t" + year);
case 6: System.out.println("June\t" + year);
case 9: System.out.println("September\t" + year);
case 11: System.out.println("November\t" + year);
numDays = 30;
case 2: System.out.println("February\t" + year);
if ( ((year % 4 == 0) && !(year & 100 == 0)) || (year % 400 == 0) )
numdays = 29;
else
numDays = 28;
break;
default : System.out.println("Invalid Month.");
break;
System.out.prinln("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
is it wrong? what part?? please guide me..
thanks and more power..
Post new comment