wood burning stoves*
The moose likes Beginning Java and the fly likes Doubt in formatting date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in formatting date" Watch "Doubt in formatting date" New topic
Author

Doubt in formatting date

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

There's a convenient formatting class called SimpleDateFormat. Try to figure out how you can use it to format your date.


[My Blog]
All roads lead to JavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
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

Joined: Dec 18, 2007
Posts: 45

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

Joined: Oct 06, 2008
Posts: 338
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Jul 26, 2007
Posts: 9993
    
    7

Did you check the API for SimpleDateFormat as Christophe had suggested?
Did you find anything for a 24 hour time pattern?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Thanks I've found it out & it's K instead of h


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


Thanks & Regards.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Oct 06, 2008
Posts: 338
no idea yet.
Regards
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13883
    
  10

What about HH instead of hh? Note, this is all documented in the API documentation of class SimpleDateFormat. Please look at the documentation.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Yeah Thanks Jesper young it's working now.
Thanks for the input !
Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in formatting date
 
Similar Threads
java.rmi.ConnectException
A challenge while formatting date value from the database
Wrong Hour/Timezone
how to get the date in required timezone?
Change Timezone from GMT to EST, the best way to accomplish?