• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Convert the String to date format

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 77
Android MyEclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
go for below modified code


 
Roopa Kamath
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

damodar kumar wrote:go for below modified code


 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaDatesFaq it is
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic