cancel
Showing results for 
Search instead for 
Did you mean: 

Getting the current month & year

former_member187658
Participant
0 Kudos

Hi

I want to get the current month and year. can any one suggest how to get it?

Thanks & regards,

Anupreet

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Anupreet,

Try with the following code

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

String DATE_FORMAT = "MM-dd-yy";

java.text.SimpleDateFormat sdf =new java.text.SimpleDateFormat(DATE_FORMAT);

sdf.setTimeZone(TimeZone.getDefault());

java.util.Date dt1=null;

dt1=sdf.parse(sdf.format(cal.getTime()));

Regards, Anilkumar

former_member187658
Participant
0 Kudos

Hi Anil

I tried the thing. But it gives the error that Unhandled Exception ParseException. When i try to catch the same exception, it gives error that no such exception exists. This error occurs in the last line of the code.

Former Member
0 Kudos

Hi Anupreet,

Have you tried with the code i have given?

It is short and simple. It gives you current year, month and whatever you want. Just specify in SimpleDateFormat whatever you want.

If you want Month in form of "Oct" then specify accordingly in SimpleDateFormat("<>") parameter.

Regards,

Bhavik

Message was edited by: Bhavik Devisha

former_member187658
Participant
0 Kudos

Hi Bhavik

Thanks for the help.

I got the solution with ur suggestion. Thanx a lot.

Anil, thanks to u also, ur suggestions were also quite helpful.

Answers (2)

Answers (2)

Former Member
0 Kudos

try{

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

String DATE_FORMAT = "MM-dd-yy";

java.text.SimpleDateFormat sdf =new java.text.SimpleDateFormat(DATE_FORMAT);

sdf.setTimeZone(TimeZone.getDefault());

java.util.Date dt1=null;

dt1=sdf.parse(sdf.format(cal.getTime()));

}

catch(Exception e)

{

}

Former Member
0 Kudos

Hi Anupreet,

Use following code for getting current Year and month.

String year = (new SimpleDateFormat("yyyy").format(new Date()));

String month = (new SimpleDateFormat("MM").format(new Date()));

Regards,

Bhavik

former_member187658
Participant
0 Kudos

hi Bhavik

Thanks for your suggestion. I am able to get the thing. But wanted to know one thing more. If I want to get the month in the form like "Oct" etc, how to get it. The month I am now getting in the form of numeral.