Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateFormat in Java

Status
Not open for further replies.

moonoo

Programmer
May 17, 2000
62
0
0
US
Hi ,
can soembody help in finding the month in String(e.g Jan , Feb) in Java when the input date format supplied in
"DD/MM/YYYY" format . I am using the SimpleDateFormat like
the code below . But i am able to get only the month on Integer . Any help is appreceated .
SimpleDateFormat df = new SimpleDateFormat("dd/Mon/yyyy");
Calendar cal = Calendar.getInstance();
try
{
java.util.Date date = df.parse(businessdate);
cal.setTime(date);
}
catch(ParseException ex)
{
}
int month = cal.get(Calendar.MONTH);

 
If you're wanting to avoid checking the int to find out what month you've got ie 11 if statements and 1 else or however you want to handle the 12 months you could do something like this....

java.util.Date date = new java.util.Date();
DateFormat df = DateFormat.getDateTimeInstance();
SimpleDateFormat sdf=(SimpleDateFormat)df;
sdf.applyPattern("dd MMM yyyy");
String dateStamp=sdf.format(date);
StringTokenizer st=new StringTokenizer(dateStamp);
st.nextToken();
String month=st.nextToken();

System.out.println(""+month);


Then the tokenizer splits the String, one of the default delimeters is a space. The dateStamp string is "03 Oct 2003
" so you know you want the second token. So get the first one do nothing, the second is going to be your string month.
Just one idea,
BD
 
Why so complicated?
Code:
    DateFormat sdf = new SimpleDateFormat("MMM");
    System.out.println(sdf.format(new Date()));
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top