Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

DateTime conversion Issue  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the DateTime in the following format.
2013-08-08T04:23:17-07:00

How to convert the above Date into the format like 05/28/2013 10:52:36 PM


Can anyone help on this.


Thanks in advance.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the SimpleDateFormat class
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:Use the SimpleDateFormat class


@sanjay: And don't use that format for anything except display.

The format you've received is ISO 8601 which, while not very pretty to look at, is an internationally recognised standard that works anywhere in the world.

Yours, OTOH, is a CLOCK time; and it will only work for display.

So: if you're putting it into any files - especially ones that get sent anywhere - use the format you were given.

Winston
 
sanjay kams
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the input.
This is the code that I am trying to get the PST timezone for the given Input.
In the below code, PST String doesn't give the required value(i.e., expecting to get the result like this "8/8/2013 5:40:12 AM"). I think, I did something wrong in the code. Can anyone find and show me the right way.


Thanks in Advance.
 
sanjay kams
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I am getting error for the line "String PST = df.format(Date);" as

java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(DateFormat.java:301)
at java.text.Format.format(Format.java:157)


How to avoid this error. I different in different ways. But can't remove the error. Please help me on this.
 
Bartender
Posts: 3306
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The format method takes an object of type Date, you are passing in a String.
You need to convert your initial string to a Date object and then format it to the output string you want.
 
Author and ninkuma
Marshal
Posts: 66533
160
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ALso, follow Java conventions. All variable names should start with lowercase. So Date is a very very bad variable name. No only because it doesn't follow convention, but it is easily confused with the Date class. Choose better variable names.
 
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess this is what you want to use:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!