Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Convert the String to date format

 
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
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • 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..
 
Ranch Hand
Posts: 77
Android MyEclipse IDE Chrome
  • 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


 
Bartender
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • 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?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaDatesFaq it is
 
You have to be odd to be #1 - Seuss. An odd little ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic