Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in formatting date

 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
Currently I'm doing date formatting with DateFormat class.


This is my output:
C:\sai>javac DateFormatting.java

C:\sai>java DateFormatting
The current date is : 12/9/08
The current time is : 1:23:58 PM


I want to display the date in
dd/mm/yy format: 09/12/08.

Time in 24 hr format:
In this case 13:23:58 hrs

How this can be done?
Regards.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a convenient formatting class called SimpleDateFormat. Try to figure out how you can use it to format your date.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are also options in the printf method; the details are in the Java Tutorials and the Formatter class javadoc.

Don't know, but you may be able to use a Locale object to change 12/9/2008 to 9/12/2008; you would have to look through the DateFormat documentation for uses of Locale.

[edit]Corrected broken link.[/edit]
[ December 09, 2008: Message edited by: Campbell Ritchie ]
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Campbell how are you. i think the link you provided for Java Tutorials is not working. please verify it.

There are also options in the printf method; the details are in the Java Tutorials and the Formatter class javadoc.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to print the time in 24 hr format.
So when i code


output:

But i want it to be 14:27:22 hrs
How to do it?
Regards.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of "hh", don't you see anything else interesting in SimpleDateFormat's API ? You'll see that there is at least four different ways to format the hour. Try to find the good one
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check the API for SimpleDateFormat as Christophe had suggested?
Did you find anything for a 24 hour time pattern?
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I've found it out & it's K instead of h


output:
09/12/2008 14:38:02


Thanks & Regards.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by PUNEET MITTAL:
hi Campbell how are you. i think the link you provided for Java Tutorials is not working. please verify it.

Thank you for noticing the error. ( ) I shall go back and correct it.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I've found it out & it's K instead of h

Are you sure ? 0:00 will become 24:00. If that's what you were looking for, no problem then
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no idea yet.
Regards
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about HH instead of hh? Note, this is all documented in the API documentation of class SimpleDateFormat. Please look at the documentation.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Thanks Jesper young it's working now.
Thanks for the input !
Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic