aspose file tools*
The moose likes Java in General and the fly likes Date Conversion 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 "Date Conversion" Watch "Date Conversion" New topic
Author

Date Conversion

raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
hi buddy,

i am getting a date from javascript as :

Tue Nov 14 00:00:00 GMT+0530 2006


now i want to convert it into the format : 2006-11-14

how can i convert it.


thangs.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8707
    
    6

java.text.SimpleDateFormat


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
hi,

i looked at simpledateformat but , need some more information
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
Try


Entia non sunt multiplicanda praeter necessitatem
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

This is what you should do:

1. Create a SimpleDateFormat object with a date pattern that matches the input string.
2. Parse the input string into a java.util.Date object using that SimpleDateFormat object.
3. Create a SimpleDateFormat object with a date pattern for the output.
4. Format the Date object that you got in step 2 into a string with that SimpleDateFormat.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
thank you for reply

i searched for the date pattern of Tue Nov 14 00:00:00 GMT+0530 2006
in SimpleDateFormat.But could n't find it.

i tried like : SDF sdf=new SDF("eee mmm d ----- yyyy")
but it is giving error like : can't parse the date

can you tell me the pattern to include in the input of SimpleDateFormat.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

First of all, note that in the date pattern string, case is important. So "eee" is not the same as "EEE". The lower-case letter "m" is for minutes, the upper case letter "M" is for month. So it doesn't work if you specify "mmm" while you mean "MMM".

I experimented with it a bit for you. If the names of the days and months are in English, you have to set the locale of the SimpleDateFormat that you are using to parse the first date correctly (if the default language of your system isn't English), otherwise it won't understand the English day and month names.

While experimenting I had trouble with parsing the timezone information "GMT+0530". This format isn't a standard timezone string format as Java accepts it. Java accepts either "GMT+05:30" (with a colon in it) or "+0530" with the following pattern:

I hope this helps.
raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
Thank you young , you are so young

keep experimenting
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

Originally posted by raj baig:
keep experimenting

Now it's your turn.
 
Consider Paul's rocket mass heater.
 
subject: Date Conversion
 
Similar Threads
java date format stored as varchar - need to convert it into date
Date Formatting
Date Problems
add 10 days to currentDate in java.util.Date or cast to Calendar?
Convert date format to timestamp