Monday, July 29, 2013

Java Get Date Time

 /**
  * @author Nazmus Sakib
 */

  //  Print Today's Date time

public String todays_datetime()
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                java.util.Date date = new java.util.Date();
                // System.out.println(dateFormat.format(date));
                return (dateFormat.format(date));

        }

Output is : 2013-07-30 10:55:05


// Print Next Month
public String next_month_year()
    {
            Calendar cal = GregorianCalendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("MMMM yyyy");

            Date currentMonth = new Date();
            cal.setTime(currentMonth);

            // current month
            String currentMonthAsSting = df.format(cal.getTime());

            // Add next month
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1);
            //cal.add(Calendar.MONTH, 1);
            String nextMonthAsString = df.format(cal.getTime());

            System.out.println(nextMonthAsString);     
                return (nextMonthAsString);

        }

No comments:

Post a Comment