• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date Conversion

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.text.SimpleDateFormat
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

i looked at simpledateformat but , need some more information
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you young , you are so young

keep experimenting
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by raj baig:
keep experimenting


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