Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date Conversion

 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9280
10
Linux Mac OS X Windows
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i looked at simpledateformat but , need some more information
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15286
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15286
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you young , you are so young

keep experimenting
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15286
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by raj baig:
keep experimenting

Now it's your turn.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic