• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date Format

 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here is part of my code for date:
Date today=new Date();
submittedDate = today.toString();
Result is: Mon Dec 10 14:05:54 PST 2001
Question: how can I change the format to have date like this 2001-12-05
Please help.
Thanks,
Elham
 
Steve Campbell
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the SimpleDateFormat class.
 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
SubmittedDate in my database is like this and I defined it as a string "Wed Dec 12 12:08:56 PST 2001"
but I don't know why I couldn't get output I mean when I wanted to print it nothing pass but the otherside I don't have any problem to pass and print FixedDate which is like this
"2001-12-05".
Please help.
Thanks,
Elaheh
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The SimpleDateFormat class allows you to choose among several formats for Date objects. Good Luck!
-Jason
 
Matt O'Keefe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should always use the Calendar class so that you can deal with timezones properly. With that in mind, try something like this:
Calendar now = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setCalendar(now);
String nowStr = format.format(now.getTime());
You can also do other cool stuff like parse a string to create a Date.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic