• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert the "Mon Oct 11 00:00:00 IST 2012" into a Date object

 
sneha patil
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to convert the "Mon Oct 11 00:00:00 IST 2012" into a Date object in yy-MM-dd format ,please help me out....
Thanks...
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest looking at the SimpleDateFormat class - that can parse most formatted date strings into a Date object.

One thing I'd like to point out, though: there's no such thing as a "Date object in yy-MM-dd format". Date objects don't have a format. What you would want to do is convert the original string to a Date object, and then, when you need to display it somewhere, convert it back to a string in whatever format you want. You can do this with another SimpleDateFormat object matching the required output format.
 
sneha patil
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply...
when i parsed string,it giving parsing error....
can you give me example so that i can get better idea??

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sneha patil wrote:Thanks for reply...
when i parsed string,it giving parsing error....
can you give me example so that i can get better idea??



It's hard to guide you toward fixing your code if you don't show that code.

I will note, however, that if you currently have a String representing a date in one particular format, and you want to change it to a String representing a date in a different format, you will need two DateFormat objects.
 
sneha patil
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


now am getting new date in yy-MM-dd format but as a string...and i want this as a date object..
please help....
 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this
Date = newDateFormat.parse("your date in String formate");
Hope this will work for you..
 
sneha patil
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply neeraj..
I tried ..but again i am getting date in Mon Oct 11 00:00:00 IST 2012 format...
 
Christian Kindler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Matthew already said above, a Date object has no format. The toString() does some formatted output for you, but it will be always the same. If you want to have your Date as a String in certain format, you have to use a DateFormat again.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sneha patil wrote:..and i want this as a date object..


You already have it as a Date object. Your first line of code is this:
This means you've already got a Date object. As I said, this doesn't have any formatting. If you print it out it will use the toString() method, and that uses a default format. But the Date object itself has no format at all.

So you should just use this Date object until you need to print it out somewhere. At this point, you decide what format you want, and you use a SimpleDateFormat to convert it to a String using that format:

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How on earth are you getting that date past it? 11th October this year is a Thursday.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic