In this post, we are going to learn how to convert the date format in Android or Java. Suppose the date is 2017-11-30. We will convert it into 30 Nov 2017 or DD MMM YYYY.

We are going to see how it’s done using a simple project. We are going to make an Android project. There will be only one activity and the activity will have TextViews and convert button. ¬†Below is the screenshot of our project –

date_format_android_java

Though we are going to create an Android project, you can click HERE if you only need the Java code to convert date format.

As you can see in the above image, we have different buttons for different date formats. Below is the xml code of our layout –

To convert the date we have a class called SimpleDateFormat. The SimpleDateFormat is used to format dates.

More articles on Android –

TextView Android tutorial for absolute beginners

Network connection checking – Android

Here we have to create two SimpleDateFormat objects and pass the date pattern as the parameter. See the code snippet below –

In the above code, the first line is for the input date format. The date which we want to convert. The second line is the output format. Means in which format you want the output.

Now get the date string which you want to convert.

Call the parse() method on inputSimpleDateFormat. The parse method accepts a parameter. Pass inputDateStr as parameter.

At last call format() method on outputSimpleDateFormat.

See the complete code below to convert date

More articles on Java –

Assigning object reference variable to another in Java

Sort numbers in ascending order – Java Program

Below is the complete code of our Android projects MainActivity.

That’s all. Now run the project and click on the buttons to convert dates.

If you have any query or suggestions, please comment below in the comment section.

 
Convert date format – Android – Java
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *