This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Doubt in formatting date

 
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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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.
 
Marshal
Posts: 65062
247
  • 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 ]
 
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 VI Editor Ubuntu
  • 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
 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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
Marshal
Posts: 65062
247
  • 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 VI Editor Ubuntu
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!