File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Convert the String to date format Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Convert the String to date format" Watch "Convert the String to date format" New topic
Author

Convert the String to date format

Roopa Kamath
Greenhorn

Joined: Mar 15, 2011
Posts: 3
I have a Date in String format -'15-03-2011 15:27:53' .
When i parse this, using the Dateformat, am getting the output as 'Tue Mar 15 15:27:53 IST 2011'
But,I want the date in the same format 'dd/MM/YYYY hh:mm:ss' ('15-03-2011 15:27:53')

IS there any way, I can do this..

Here is my code :

DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

long now = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
System.out.println("now: "+ formatter.format(calendar.getTime()));

Date date = (Date)formatter.parse(formatter.format(calendar.getTime()));
System.out.println("Date:" + date);

Output:

now : 15-03-2011 15:27:53
Date:Tue Mar 15 15:27:53 IST 2011
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Actually what you're doing is, you're constructing a date object with the information you have in a String and you're then printing the Date object. The date object has a standard formatting and it is how its toString() method has been implemented internally.
But,I want the date in the same format 'dd/MM/YYYY hh:mm:ss' ('15-03-2011 15:27:53')

You can have a date in such customized format only when you convert it into a String. Like what you already did..


OCPJP 6
damodar kumar
Ranch Hand

Joined: May 19, 2008
Posts: 77

go for below modified code




<a href="http://stackoverflow.com/users/668970/user668970" rel="nofollow">
<img src="http://stackoverflow.com/users/flair/668970.png" >
</a>
Roopa Kamath
Greenhorn

Joined: Mar 15, 2011
Posts: 3
Vinoth Kumar Kannan wrote:Actually what you're doing is, you're constructing a date object with the information you have in a String and you're then printing the Date object. The date object has a standard formatting and it is how its toString() method has been implemented internally.
But,I want the date in the same format 'dd/MM/YYYY hh:mm:ss' ('15-03-2011 15:27:53')

You can have a date in such customized format only when you convert it into a String. Like what you already did..


AM getting the Date obnject, as shown in the output. But is there any way, that I can format it to my required format like dd/mm/yyyy....
Roopa Kamath
Greenhorn

Joined: Mar 15, 2011
Posts: 3
damodar kumar wrote:go for below modified code


Swastik Dey
Ranch Hand

Joined: Jan 08, 2009
Posts: 1188

I don't think the format of Date object it self can be changed. What you need to do is to get it as a String. Moreover where do you have such a requirement?


Swastik
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2925
    
  15

JavaDatesFaq it is


Mohamed Sanaulla | My Blog
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Convert the String to date format
 
Similar Threads
date format conversion to MM/dd/yyyy hh:mm a
Timezone Conversion
Code review required for converting dateformat
How do I get date and time seperate from Date Object
Convert String to Date format