aspose file tools*
The moose likes Beginning Java and the fly likes date format conversion to MM/dd/yyyy hh:mm a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "date format conversion to MM/dd/yyyy hh:mm a" Watch "date format conversion to MM/dd/yyyy hh:mm a" New topic
Author

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

sony rao
Greenhorn

Joined: Jun 18, 2007
Posts: 20
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

Joined: Sep 03, 2004
Posts: 127
SimpleDateFormat might help you with that.
Here: SimpleDateFormat


J2EE Architect/Developer
sony rao
Greenhorn

Joined: Jun 18, 2007
Posts: 20
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
sony rao
Greenhorn

Joined: Jun 18, 2007
Posts: 20
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

Joined: Sep 03, 2004
Posts: 127
Have you tried using the Z element like Jesper suggested?
sony rao
Greenhorn

Joined: Jun 18, 2007
Posts: 20
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

Joined: Feb 26, 2008
Posts: 17
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 ]

Laxman
sony rao
Greenhorn

Joined: Jun 18, 2007
Posts: 20
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

Joined: Feb 26, 2008
Posts: 17
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

Joined: Jun 18, 2007
Posts: 20
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

Joined: Feb 26, 2008
Posts: 17
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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

Joined: Jun 18, 2007
Posts: 20
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

Joined: Jun 18, 2009
Posts: 34
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

Joined: Apr 08, 2009
Posts: 298
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

Joined: Jun 18, 2009
Posts: 34
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

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Pawan Kalyan
Ranch Hand

Joined: Jun 18, 2009
Posts: 34
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

Joined: Feb 10, 2009
Posts: 710
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

Joined: May 06, 2010
Posts: 1

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

Joined: Apr 26, 2010
Posts: 53
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 :



SCJP 1.5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Welcome to the Ranch Rahul Gurubhai
Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 79
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.!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: date format conversion to MM/dd/yyyy hh:mm a