• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in formatting date

 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Thanks Jesper young it's working now.
Thanks for the input !
Regards
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic