[How-To] Print Date/Time in a Given Format in Java
Often, we wish to Format the Date and Time information into a given format, either for displaying the same to the end user or for further processing. Java contains formatting classes which can be used to format a date into a desired format. The most commonly used class for formatting dates is the SimpleDateFormat class.
The SimpleDateFormat class takes format string as input to its constructor and returns a format object which can then be used to format Date objects. Calling the format() method of the SimpleDateFormat object will return a string that contains the formatted representation of the Date that is passed into the method as a parameter.
The table below contains Time and Date Format Codes which can be used to format the Date Object.
| Letter | Date or Time Component | Presentation | Examples |
|---|---|---|---|
G |
Era designator | Text | AD |
y |
Year | Year | 1996; 96 |
M |
Month in year | Month | July; Jul; 07 |
w |
Week in year | Number | 27 |
W |
Week in month | Number | 2 |
D |
Day in year | Number | 189 |
d |
Day in month | Number | 10 |
F |
Day of week in month | Number | 2 |
E |
Day in week | Text | Tuesday; Tue |
a |
Am/pm marker | Text | PM |
H |
Hour in day (0-23) | Number | 0 |
k |
Hour in day (1-24) | Number | 24 |
K |
Hour in am/pm (0-11) | Number | 0 |
h |
Hour in am/pm (1-12) | Number | 12 |
m |
Minute in hour | Number | 30 |
s |
Second in minute | Number | 55 |
S |
Millisecond | Number | 978 |
z |
Time zone | General time zone | Pacific Standard Time; PST; GMT-08:00 |
Z |
Time zone | RFC 822 time zone | -0800 |
Lets try understanding this better using this code example. The code below formats today’s date.
package testapplicationsfortechnofriends;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
*
* @author Vaibhav
*/
public class Main {/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Date todaysDate = new java.util.Date();
SimpleDateFormat formatter = new SimpleDateFormat(“EEE, dd-MMM-yyyy HH:mm:ss”);String formattedDate = formatter.format(todaysDate);
System.out.println(“Today’s date and Time is:”+formattedDate);}
}
Running this program, gives the following output
Today’s date and Time is:Thu, 01-May-2008 14:39:45
For further information on this, follow the Sun Java Documentation on DateFormat and SImpleDateFormat class.
Also read:Understanding Polymorphism in Java : Part 1
Understanding Polymorphism in Java : Part 2
What is the difference between JDK and JRE?
An introduction to JDBC Drivers
You can follow me on Twitter at http://twitter.com/vaibhav1981
Do stay tuned to Technofriends for more, one of the best ways of doing so is by subscribing to our feeds. You can subscribe to Technofriends feed by clicking here.
Cheers
Vaibhav
Related posts:
- [Java] Reading a Webpage through URL
- Java: Lets revisit the past
- Polymorphism in Java : Part 2
- Polymorphism in Java : Part 1
- Java Tutorials: Its here
Good to know. Do you know of API to convert int to hex in Java 1.4.2?
Hi!!
To convert int to hex try using
String java.lang.Integer.toHexString( int )
Cheers
Vaibhav
Thanks that works.
I was trying ..parseInt(String,16)…this seems to be giving an awkward output. Know why?
To be exact I used Integer.parseInt(numberInteger.toString, 16);
Hi Kiran,
Am not very sure but if you could share your program and the output, i could have a look and let you know.
Cheers
Vaibhav
[...] Also read:[How-To] Print Date/Time in a Given Format in Java [...]
[...] contains formatting classes which can be used to format a date into a desired format. The most commohttp://technofriends.in/2008/05/01/how-to-print-datetime-in-a-given-format-in-java/Web Development of Busienss Process Management… web application for specialized dating website [...]
really good, thank you. this is what i am searching and i got it.