Display Month of year using Java Calendar

  1. /*
  2.   Display Month of year using Java Calendar
  3.   This example shows how to display a month like January, February etc.
  4.   using Java Calendar class.
  5. */
  6.  
  7. import java.util.Calendar;
  8.  
  9.  
  10. public class DisplayMonthOfYear {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. //create Calendar instance
  15. Calendar now = Calendar.getInstance();
  16.  
  17. System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
  18. + "-"
  19. + now.get(Calendar.DATE)
  20. + "-"
  21. + now.get(Calendar.YEAR));
  22.  
  23. //create an array of months
  24. String[] strMonths = new String[]{
  25. "Jan",
  26. "Feb",
  27. "Mar",
  28. "Apr",
  29. "May",
  30. "Jun",
  31. "Jul",
  32. "Aug",
  33. "Sep",
  34. "Oct",
  35. "Nov",
  36. "Dec"
  37. };
  38.  
  39. System.out.println("Current month is : " +
  40. strMonths[now.get(Calendar.MONTH)]
  41. );
  42.  
  43.  
  44. }
  45.  
  46. }
  47.  
  48. /*
  49. Typical output would be
  50. Current date : 12-25-2007
  51. Current month is : Dec
  52. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

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..


Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!