BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Convert Date to String in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

Earlier we saw, how to convert String to Date in Java. This post is a continuation of that post and here we will learn Date to String conversion in Java.

Java Code: Convert Date to String in Java

After this section I have shared a complete code of Date to String conversion. The below function converts a Date to a String. In the below function I have used the format dd/MM/yyyy, however if you want the result in any other format then you can simply modify the pattern in SimpleDateFormat. You can also refer one of my post on date formats in Java.

Function:

public String convertStringToDate(Date indate)
{
   String dateString = null;
   SimpleDateFormat sdfr = new SimpleDateFormat("dd/MMM/yyyy");
   /*you can also use DateFormat reference instead of SimpleDateFormat 
    * like this: DateFormat df = new SimpleDateFormat("dd/MMM/yyyy");
    */
   try{
	dateString = sdfr.format( indate );
   }catch (Exception ex ){
	System.out.println(ex);
   }
   return dateString;
}

Complete Example program for Date to String conversion

In this example I am taking current date as an input and converting into a String. In order to get the output String in various format I have specified different-2 patterns in SimpleDateFormat.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringDemo{
   public static void main(String args[])
   {
       Date todaysDate = new Date();
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
       DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
       DateFormat df3 = new SimpleDateFormat("dd-MMM-yyyy");
       DateFormat df4 = new SimpleDateFormat("MM dd, yyyy");
       DateFormat df5 = new SimpleDateFormat("E, MMM dd yyyy");
       DateFormat df6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
       try
       {
           //format() method Formats a Date into a date/time string. 
           String testDateString = df.format(todaysDate);
           System.out.println("String in dd/MM/yyyy format is: " + testDateString);
           String str2 = df2.format(todaysDate);
           System.out.println("String in dd-MM-yyyy HH:mm:ss format is: " + str2);
           String str3 = df3.format(todaysDate);
           System.out.println("String in dd-MMM-yyyy format is: " + str3);
           String str4 = df4.format(todaysDate);
           System.out.println("String in MM dd, yyyy format is: " + str4);
           String str5 = df5.format(todaysDate);
           System.out.println("String in E, MMM dd yyyy format is: " + str5);
           String str6 = df6.format(todaysDate);
           System.out.println("String in E, E, MMM dd yyyy HH:mm:ss format is: " + str6);

       }
       catch (Exception ex ){
          System.out.println(ex);
       }
    }
}

Output:

String in dd/MM/yyyy format is: 02/01/2014
String in dd-MM-yyyy HH:mm:ss format is: 02-01-2014 22:38:35
String in dd-MMM-yyyy format is: 02-Jan-2014
String in MM dd, yyyy format is: 01 02, 2014
String in E, MMM dd yyyy format is: Thu, Jan 02 2014
String in E, E, MMM dd yyyy HH:mm:ss format is: Thu, Jan 02 2014 22:38:35

References:

  • DateFormat
  • SimpleDateFormat
  • Date – javadoc

Top Related Articles:

  1. Date validation in java
  2. Convert String to date in Java
  3. Java Access Modifiers – Public, Private, Protected & Default
  4. Compare two dates with each other in Java
  5. Java 8 – Calculate days between two dates

Tags: Java-Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap