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

date format conversion to MM/dd/yyyy hh:mm a

 
sony rao
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

i have to convert date format of "2008-03-30T05:00:00.000+02:00" to
MM/dd/yyyy hh:mm a

please help me in this

thanks in advance
 
Henrique Ordine
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SimpleDateFormat might help you with that.
Here: SimpleDateFormat
 
sony rao
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
checked SimpleDateFormat

"yyyy-MM-dd'T'HH:mm:ss.SSS" corresponds to "2001-9-11T18:15:00.000"

how do i go about with "+02:00" ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15288
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "+02:00" is the timezone.

Unfortunately SimpleDateFormat does not have many options to format the time zone. You can use the capital letter 'Z' in the format string to specify the timezone, but this will format it as "+0200" (without the colon). So what you can do is use 'Z' and then insert the colon yourself afterwards:
 
sony rao
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have given my code that converts date in "yyyy-MM-dd'T'HH:mm:ss.SSS" format to
"MM/dd/yyyy hh:mm a" format.

try{
String s1 = "2001-9-11T15:30:00.000";
Date d = (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")).parse(s1);
String s2 = (new SimpleDateFormat("MM/dd/yyyy hh:mm a")).format(d);
System.out.println("s2 "+s2);
} catch(Exception e) {
e.printStackTrace();
}

but i dont know how to convert if a date is of
"2008-03-30T05:00:00.000+02:00" format


please guide me
 
Henrique Ordine
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried using the Z element like Jesper suggested?
 
sony rao
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Henrique

i tried what Jesper said..

Date now = new Date();
System.out.println("date--> " + now);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String s = df.format(now);
String result = s.substring(0, 26) + ":" + s.substring(27);
System.out.println("result--> " + result);

output:

date--> Fri Mar 14 10:27:31 IST 2008
result--> 2008-03-14T10:27:31.197+05:0

"2008-03-14T10:27:31.197+05:0" is not the format i want my result to be.
it should be in MM/dd/yyyy hh:mm a format
 
Laxman Guru
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sony,
Try this....


Date-->Fri Mar 14 13:50:07 SGT 2008
New Date--->03/14/2008 01:50 PM
[ March 13, 2008: Message edited by: Laxman Guru ]
 
sony rao
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi laxman

thanks for responding
source date format is "Fri Mar 14 13:50:07 SGT 2008" as per your code

but in my case source date is in "2008-03-14T11:53:31.470+05:00" format
which i need to format it to MM/dd/yyyy hh:mm a
[ March 14, 2008: Message edited by: sony rao ]
 
Laxman Guru
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sony,
This is the solution as far as i now...here i hard coded the values...
Try a better solution(if it isn't helpful!!!)....

OldDate-->2008-03-14T11:53:31.200+05:00
NewDate-->03/14/2008 02:53 PM
[ March 14, 2008: Message edited by: Laxman Guru ]
 
sony rao
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi laxman

thanks for your response.

as of now iam using this code

SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String s1 = "2008-03-30T15:30:00.000+02:00";
s1 = s1.substring(0, s1.indexOf('.'));
try {
System.out.println("Result==> "+sdf1.format(sdf2.parse(s1)));
} catch (Exception e) {
e.printStackTrace();
}
 
Laxman Guru
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the same input(2008-03-30T15:30:00.000+02:00)
Your code yields "Result==> 03/30/2008 03:30 PM"
and mine gives "NewDate-->03/30/2008 09:30 PM"....Now i m wondering How i m going to learn java?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15288
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sony rao:
hi Henrique

i tried what Jesper said..

Date now = new Date();
System.out.println("date--> " + now);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String s = df.format(now);
String result = s.substring(0, 26) + ":" + s.substring(27);
System.out.println("result--> " + result);

output:

date--> Fri Mar 14 10:27:31 IST 2008
result--> 2008-03-14T10:27:31.197+05:0

"2008-03-14T10:27:31.197+05:0" is not the format i want my result to be.
it should be in MM/dd/yyyy hh:mm a format

So I made a little mistake. It should have been:

String result = s.substring(0, 26) + ":" + s.substring(26);

But what do you really want? First you said you wanted a format like 2008-03-14T10:27:31.197+05:00 and now you're talking about MM/dd/yyyy hh:mm.

Do you mean you want to parse a date in a format like 2008-03-14T10:27:31.197+05:00, and then format it into MM/dd/yyyy hh:mm? You can do that like this:

1. Manually remove the ":" from the input string using substring().
2. Use a SimpleDateFormat object with the format "yyyy-MM-dd'T'HH:mm:ss.SSSZ" and call parse() on it to parse the input string into a Date object.
3. Use another SimpleDateFormat object with the format "MM/dd/yyyy hh:mm" and call format() on it to format the Date object into the output string.
[ March 14, 2008: Message edited by: Jesper Young ]
 
sony rao
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jesper

my very first statement was .....

i have to convert date format of "2008-03-30T05:00:00.000+02:00" to
MM/dd/yyyy hh:mm a

i think this clearly explains that i want to convert datetime in "2008-03-30T05:00:00.000+02:00" format to MM/dd/yyyy hh:mm a format.
[ March 14, 2008: Message edited by: sony rao ]
 
Pawan Kalyan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I just need a small favor.

I have a date in "2009-12-01T00:00:00-05:00"

I need that in MM/dd/yyyy format.

Can someone help me in this.


Thanks.
Pawan
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the point of this? Learning dates or getting a good solution to the issue? The answer is different :P
 
Pawan Kalyan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would greatly appreciate if someone posts a solution for this.

And i am trying to get the return type as Date in the format MM/dd/yyyy.


Thanks again.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawan Kalyan wrote:I would greatly appreciate if someone posts a solution for this.

And i am trying to get the return type as Date in the format MM/dd/yyyy.


Thanks again.


Have you looked over this thread and written anything yet? No one here will provide you with code to do what you are asking, but we are more than willing to help you with problems/issues you encounter while writing it.
 
Pawan Kalyan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
W. Joe Smith wrote

No one here will provide you with code to do what you are asking


i am having a problem with parsing the date using simpledateformatter.

Thats why i have posted and if someone could guide me then that would be really great.
I dont see a point why someone should not help me in giving the syntax or code.

I dont think you will have any problem if someone wants to give the solution.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawan Kalyan wrote:
W. Joe Smith wrote

No one here will provide you with code to do what you are asking


i am having a problem with parsing the date using simpledateformatter.

Thats why i have posted and if someone could guide me then that would be really great.
I dont see a point why someone should not help me in giving the syntax or code.

I dont think you will have any problem if someone wants to give the solution.


If someone wants to give you some sample code I don't think anyone will have a problem with it, but NotACodeMill is a standard policy. I'm not personally objecting to someone giving you guidance using code, but providing the whole code to do what you ask would be a little much. I'm sorry if I misread your message and you are in fact asking for guidance and not a pre-written code solution.
 
rahul gurubhai
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello Jesper.

I have a String which gives me date in the following format ----"2010-05-06 17:02:00.0"

i want to convert it in "06-05-2010 17:02:00.0"

Please help me in this regard..

Thanks


Jesper Young wrote:The "+02:00" is the timezone.

Unfortunately SimpleDateFormat does not have many options to format the time zone. You can use the capital letter 'Z' in the format string to specify the timezone, but this will format it as "+0200" (without the colon). So what you can do is use 'Z' and then insert the colon yourself afterwards:
 
Gopi Chella
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the same Thread you can find the answer, posted by Laxman Guru.However as per your request i have modified his code for you.



The output is :

 
Campbell Ritchie
Sheriff
Pie
Posts: 48980
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Rahul Gurubhai
 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a similar problem. I get my date in the format yyyy-mm-dd hh:mm:ss.
I want to convert it into the format DDD MMM dd HH:mm:ss yyyy.
Example: Fri Jul 02 17:40:22 2010
Is there any methods by which i can set it to the desired format?
Thanks in advance.!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic