Quick Reach
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class date_example {
public static void main(String args[]) {
Date currdate = new Date();
String strdate;
strdate = String.format(“Current date/time: %tc”, currdate);
System.out.printf(strdate);
}
}
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class date_example {
public static void main(String args[]) {
Date currdate = new Date();
String strdate;
strdate = String.format(“Current date: %1$td %1$tB, %1$tY”, currdate);
System.out.printf(strdate);
}
}
|
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import java.util.Date;
import java.text.*;
public class date_example {
public static void main(String args[]) {
Date currdate = new Date();
SimpleDateFormat formatdate = new SimpleDateFormat(“dd/MM/yyyy”);
SimpleDateFormat formatdate2 = new SimpleDateFormat(“MM/dd/yyyy”);
SimpleDateFormat formatdate3 = new SimpleDateFormat(“MM/dd/yyyy hh:mm:ss”);
System.out.println(“Current Date in dd/MM/yyyy format: “ + formatdate.format(currdate));
System.out.println(“Current Date in MM/dd/yyyy format: “ + formatdate2.format(currdate));
System.out.println(“Current Date and time in MM/dd/yyyy format: “ + formatdate3.format(currdate));
}
}
|
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
Leave A Comment?