This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert the String to date format

 
Roopa Kamath
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
damodar kumar
Ranch Hand
Posts: 77
Android Chrome MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
go for below modified code


 
Roopa Kamath
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
damodar kumar wrote:go for below modified code


 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaDatesFaq it is
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic