• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ravi Rai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the code you tried. Read => TellTheDetails
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Rai wrote:...getting Parsing exception.


Parsing exception or exception while parsing? and where which line?
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Catch the ParseException and check its error index to see the location in the String where parsing fails.
 
Ravi Rai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

Here is the code which i tried

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you guys
its solved.


 
Ravi Rai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congrats.... I used the format "E, dd MMM yyyy" and it gave me the same result.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you're missing the "Fri, " part.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic