File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to Convert String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to Convert String "Friday, 27 January 2012" into the Date format." Watch "How to Convert String "Friday, 27 January 2012" into the Date format." New topic
Author

How to Convert String "Friday, 27 January 2012" into the Date format.

Ravi Rai
Greenhorn

Joined: Apr 15, 2011
Posts: 13

Hi Guys,

can anybody let me know if is there any way we can convert String "Friday, 27 January 2012" into Java Date format.
i know we can convert String like "2010-08-05" or "2010/08/05" but not able to convert like this "Friday, 27 January 2012" getting Parsing exception.

Thanks in advance.
Ravi
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Please post the code you tried. Read => TellTheDetails
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Use the letter E for parsing day in the week - Read http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1511
Ravi Rai wrote:...getting Parsing exception.


Parsing exception or exception while parsing? and where which line?


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Catch the ParseException and check its error index to see the location in the String where parsing fails.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ravi Rai
Greenhorn

Joined: Apr 15, 2011
Posts: 13
Hi John,

Here is the code which i tried

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
If modifying the source to "Friday, 27 January 2012" you will receive the ParseException - Modify the pattern to include E for the day of week.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Rob's code to see error indices -

http://www.coderanch.com/t/562290/java/java/someone-please-help-me-parse#2552890

http://www.coderanch.com/t/563056/java/java/Help-converting-date-string#2556459

Should be helpful while debugging.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

Ravi Rai wrote:Hi John,

Here is the code which i tried

Please UseCodeTags to post your code. I have edited your post and added the CODE tags. It preserves the indentation you had added and looks crisp


Mohamed Sanaulla | My Blog
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1511
Mohamed Sanaulla wrote:...It preserves the indentation you had added and looks crisp


Plus the 'line-numbers' in CODE tags help responders in indicating directly/easily where to look for error or a fix.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Akhilesh Trivedi wrote:
Mohamed Sanaulla wrote:...It preserves the indentation you had added and looks crisp


Plus the 'line-numbers' in CODE tags help responders in indicating directly/easily where to look for error or a fix.

Agree... it alleviates the need of posting the code again to point out the error. Rather the line number can be easily pointed out for possible errors.
Madhavi Subramaniam
Ranch Hand

Joined: Nov 24, 2011
Posts: 33
Hi,

I think, the input for parse method should be in particular format.

eg. "07/10/96 4:5 PM, PDT".

So using build in method for your input string surely give exception. So please try to do user defined method.

Here i attached method for date format yyyy-mm-dd for your inpur string.


public static String parseString(String source) throws ParseException {
StringTokenizer stringTokenizer = new StringTokenizer(source, " ");

StringTokenizer stk = new StringTokenizer(source, " ");
ArrayList<String> emails = new ArrayList();

while (stk.hasMoreTokens()) {
emails.add(stk.nextToken());
}
Date date = new SimpleDateFormat("MMM", Locale.ENGLISH).parse(emails.get(1));
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH) + 1;

return emails.get(2).concat("-").concat(String.valueOf(month)).concat("-").concat(emails.get(0));

}


Try this method. It may satisfy your expection.

Thanks.
Ravi Rai
Greenhorn

Joined: Apr 15, 2011
Posts: 13
Thanks you guys
its solved.


Ravi Rai
Greenhorn

Joined: Apr 15, 2011
Posts: 13
John Jai wrote:If modifying the source to "Friday, 27 January 2012" you will receive the ParseException - Modify the pattern to include E for the day of week.


Thanks John it works
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Congrats.... I used the format "E, dd MMM yyyy" and it gave me the same result.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Madhavi Subramaniam wrote:Hi,

I think, the input for parse method should be in particular format.

eg. "07/10/96 4:5 PM, PDT".


No, it can be in pretty much any format, as long as it matches the format for the DateFormat you use for input.

So please try to do user defined method.


No, the right way to do it is using two DateFormat objects--one to parse the original format into a Date, and one to format that Date into a String in the desired format. There's no need for StringTokenizer or manually splitting up the String.

Madhavi Subramaniam
Ranch Hand

Joined: Nov 24, 2011
Posts: 33
Hi Jeff,

I thought that Ravi's input source should be in "27 January 2012". So only i suggest user defined method.

And I've tried solved code with the input source "27 January 2012", I'm getting ParseException.



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

That's because you're missing the "Fri, " part.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Convert String "Friday, 27 January 2012" into the Date format.
 
Similar Threads
Java Calendar Dates
How to convert date in one locale to another locale
date format conversion to MM/dd/yyyy hh:mm a
how do i compare row by row
Java Calendar Dates