Tuesday, December 27, 2011

Get the Month Number from a Date


<< Previous Table of Categories Next >>


 
   /**
   * Feel free to copy and use this. 
   * If you have any comments or better solutions, please paste on my blog: 
   * http://blog.yanmingyu.com or email me feedback.andrewyu@gmail.com 
 */ 
  public static int getMonthNumberFromDate(Date date)
  {
    int month = 0;
    try
    {
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      month = cal.get(Calendar.MONTH)+1;
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return month;
  }

No comments:

Post a Comment