1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
/* 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 */ |
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” + year);
case 3: System.out.println(“March” + year);
case 5: System.out.println(“May” + year);
case 7: System.out.println(“July” + year);
case 8: System.out.println(“August” + year);
case 10: System.out.println(“October” + year);
case 12: System.out.println(“December” + year);
numDays = 31;
break;
case 4: System.out.println(“April” + year);
case 6: System.out.println(“June” + year);
case 9: System.out.println(“September” + year);
case 11: System.out.println(“November” + year);
numDays = 30;
case 2: System.out.println(“February” + 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(“SunMonTueWedThuFriSat”);
is it wrong? what part?? please guide me..
thanks and more power..
System.out.print(“Current month is : ” +
now.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US)
);
See my comment to “Display Day of Week using Java Calendar”