Java format date by java printf and simple formatdate

Date formatting in Java

Java provides different ways to format dates. The java date formating can be performed by using class SimpleDateFormat. You can also use printf method to format dates in java. Both ways are explained below.

Java printf method for Date formatting

You can use printf method to format dates quite easily. Java supports date and time conversion characters that can be used with java printf method to format dates. A list of those important characters given below but first let us show you how to use printf with date.

Getting current date time with printf

To get current date and time the example below uses string format function. The ‘c’ character with %t gets current date and time and then printf method is used to display it, see below:

Experience this online


The output will be:

Current date/time: Sat Jan 15 18:48:38 PKT 2011

Current date in day, full month name year format example

The example displays date in day, full month name and year format e.g. 13 March, 2011. We use Date class object with printf java method for that.

Experience this online


The output will be:

Current date: 15 January, 2011

Printf java List of characters to format date

Find the list of characters that can be used in string format and displayed with printf java method in given link.

Examples of Java Date format with SimpleDateFormat class

The example below shows how to format date in java. We will use SimpleDateFormat class that is part of java.text package. In real time application you have to format dates as per need like date format in dd/mm/yyyy or mm/dd/yyyy or Mon, day, year etc. to make presentation of dates more readable and understandable to users of application. The java Simple DateFormat is great for parsing and java date format.


Experience this online

The output will be:

Current Date in dd/MM/yyyy format: 15/01/2011

Current Date in MM/dd/yyyy format: 01/15/2011

Current Date and time in MM/dd/yyyy format: 01/15/2011 06:07:45

 

Also see Java date

Was this article helpful?

Related Articles

Leave A Comment?